A trakt.tv API wrapper in R https://jemus42.github.io/tRakt
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

5.7 KiB

---
output: github_document
---

<!-- README.md is generated from README.Rmd. Please edit that file -->

```{r setup, include = FALSE}
knitr::opts_chunk$set(
cache = TRUE,
collapse = TRUE,
comment = "#>",
fig.path = "man/figures/README-",
out.width = "100%",
message = FALSE, warning = FALSE, error = FALSE
)
```

# tRakt <img src="https://jemus42.github.io/tRakt/reference/figures/logo.png" align="right" height="120"/>

<!-- badges: start -->
[![Travis (.org)](https://img.shields.io/travis/jemus42/tRakt.svg?logo=travis)](https://travis-ci.org/jemus42/tRakt)
[![R build status](https://github.com/jemus42/tRakt/workflows/R-CMD-check/badge.svg)](https://github.com/jemus42/tRakt/actions)
[![Coverage status](https://codecov.io/gh/jemus42/tRakt/branch/master/graph/badge.svg)](https://codecov.io/github/jemus42/tRakt?branch=master)
[![CRAN status](https://www.r-pkg.org/badges/version/tRakt)](https://cran.r-project.org/package=tRakt)
[![GitHub release](https://img.shields.io/github/release/jemus42/tRakt.svg?logo=GitHub)](https://github.com/jemus42/tRakt/releases)
<!-- badges: end -->

`tRakt` helps you to retrieve data from [trakt.tv](https://trakt.tv/), a site similiar to [IMDb](https://imdb.com) with a wider focus, yet smaller user base. The site also enables media-center integration, so you can automatically sync your collection and watch progress, as well as scrobble playback and ratings via [Plex](https://www.plex.tv/), [Kodi](https://kodi.tv/) and the likes.
And, most importantly, [trakt.tv has a publicly available API](https://trakt.docs.apiary.io) – which makes this package possible and allows you to collect all that nice data people have contributed.

Please note that while this package is *basically* an API-client, it is a little more opinionated and might deliver results that do not exactly match the data delivered by the API. The primary motivation for this package is to retrieve data that is easily processable for data analysis and display, which is why it tries hard to coerce most data into tabular form instead of using nested lists, which is what the direct translation of the API results would look like.

## Installation

Get it from GitHub:

```r
if (!("remotes" %in% installed.packages())) {
install.packages("remotes")
}
remotes::install_github("jemus42/tRakt")

library("tRakt")
```

## Usage

```{r}
library(dplyr) # for convenience
library(tRakt)
```

Search for a show, get basic info:

```{r}
show_info <- search_query("Utopia", type = "show")
glimpse(show_info)
```

Get season information for the show using its trakt ID:

```{r}
seasons_summary(show_info$trakt, extended = "full") %>%
glimpse()
```

Get episode data for the first season, this time using the show's URL slug:

```{r}
seasons_season("utopia", seasons = 1, extended = "full") %>%
glimpse()
```

You cann also get episode data for all seasons, but note that episodes will be included as a list-column and need further unpacking:

```{r}
seasons_summary("utopia", episodes = TRUE, extended = "full") %>%
pull(episodes) %>%
bind_rows() %>%
glimpse()
```


Or alternatively, get the [trending shows](https://trakt.tv/shows/trending):

```{r}
shows_trending()
```

Maybe you just want to know how long it would take you to binge through these shows:

```{r}
shows_trending(extended = "full") %>%
transmute(
show = glue::glue("{title} ({year})"),
runtime_hms = hms::hms(minutes = runtime),
aired_episodes = aired_episodes,
runtime_aired = hms::hms(minutes = runtime * aired_episodes)
) %>%
knitr::kable(
col.names = c("Show", "Episode Runtime", "Aired Episodes", "Total Runtime (aired)")
)
```

Please note though that episode runtime data may be inaccurate. In my experience, recent shows have fairly accurate runtimes, which might not be case for older shows.

## Credentials

The API requires at least a `client id` for the API calls.
Loading the package (or calling its functions via `tRakt::` wil automatically set the app's client id (see `?trakt_credentials()`) – for extended use you should set your own credentials via environment variables in your `.Renviron` like this:

```sh
# tRakt
trakt_client_id=12fc1de7[...]3d629afdf2
trakt_client_secret=justabunchofstuffhere
trakt_username=jemus42
```

* `trakt_client_id` **Required**. It's used in the HTTP headers for the API calls, which is kind of a biggie.
* `trakt_client_secret`: **Optional**(ish). This is only required if you intend to make an authenticated request, which is only required by a [small number of implemented API methods](http://jemus42.github.io/tRakt/articles/Implemented-API-methods.html). You can use this package perfectly fine for basic data collection without registering an application on trakt.tv.
* `trakt_username` **Optional**. For functions that retrieve a user's watched shows or stats, this just sets the default value so you don't have to keep supplying it in individual function calls when you're just looking at your own data anyway.

Please not that there's no client secret listed – that's because this package doesn't implement any authenticated methods. Maybe in the future.

To get your credentials, [you have to have an (approved) app over at trakt.tv](http://trakt.tv/oauth/applications).
Don't worry, it's really easy to set up. Even I did it.

You theoretically never need to supply your own credentials. However, if you want to actually use this package for some project, I do not recommend relying on my credentials.
That would make me a sad panda.
As of now, the trakt.tv API does not have any rate-limiting, but it's not guaranteed to stay like this in the future.

# Code of Conduct

Please note that the **tRakt** project is released with a [Contributor Code of Conduct](.github/CODE_OF_CONDUCT.md). By contributing to this project, you agree to abide by its terms.