Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion NAMESPACE
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Generated by roxygen2 (4.1.1): do not edit by hand
# Generated by roxygen2: do not edit by hand

S3method(print,omdb)
export(find_by_id)
Expand All @@ -7,6 +7,7 @@ export(get_actors)
export(get_countries)
export(get_directors)
export(get_genres)
export(get_tv_data)
export(get_writers)
export(search_by_title)
import(dplyr)
Expand Down
115 changes: 115 additions & 0 deletions R/get_imdb.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
#' Retrieve IMDB ratings for all Episodes of a TV series
#'
#' @param name TV series for which to obtain ratings for. Has to be of IMDB type "TV series".
#' @seealso \href{omdbAPI documentation}{http://www.omdbapi.com/}
#' @return \code{data.frame}
#' @note Can be quite slow, depending on your internet connection.
#' @export

get_tv_data <- function(name) {
# Stop if argument is missing
if (missing(name))
stop("Need to specify name of TV series")

# Get some info about the name
query <- GET("http://www.omdbapi.com/",
query = list(t = name,
r = "json"))
query_parsed <- content(query, as = "parsed")

if (query_parsed$Type != "series")
stop("This is not a TV series")

# Make containers
object <- data.frame(Season = NA,
Episode = NA,
imdbRating = NA,
episode.no = NA)
result <- matrix(nrow = 1)
ep.no <- 0

# Starting values
i <- 1
j <- 1

# Tolerance parameter
s <- 0

# Pull data
while (!is.null(result)) {
if (i > 1 & j == 1) {
break
}

# Starting values
j <- 1

while (!is.null(result)) {
# Get data from OMDB
query <- GET("http://www.omdbapi.com/",
query = list(t = name,
Season = i,
Episode = j,
r = "json"))

# Parse content
query_parsed <- content(query, as = "parsed")

# Store content
result <- query_parsed$imdbRating

if (!is.null(result) && result == "N/A") {
result <- NA
}

# Next season if no more episodes
if (is.null(result) && s == 1) {
result <- 0
break
}

# Skip once, but set tolerance parameter to 1
if (is.null(result)) {
result <- 0
s <- 1
j <- j + 1
next
}

# Make episode no higher
ep.no <- ep.no + 1

# Fill container
object[ep.no, ] <- c(i, j, result, ep.no)

# Next iteration, reset tolerance parameter
j <- j + 1
s <- 0
}

if (j - 1 != 0)
cat(paste("Last episode of Season", i, "was episode", j - 2, "\n"))

# Next iteration
i <- i + 1
}

# Necessary transformations
object <- sapply(object, FUN = as.numeric)
object <- as.data.frame(object)

# Delete Season if only NA's
for (season in unique(object$Season)) {
if (all(is.na(object[object$Season == season, "imdbRating"]))) {
object <- object[object$Season != season, ]
warning("Season ", season, " was deleted, because all Ratings were NA \n")
}
}

# Make factors
object$Season <- as.factor(object$Season)
object$Episode <- as.factor(object$Episode)

return(object)
rm(list = c("object", "query", "query_parsed", "result", "season", "ep.no", "i", "j"))
}
4 changes: 2 additions & 2 deletions R/title_or_id.R
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@
#' @param type Type of result to return. One of \code{[movie|series|episode|game]}.
#' @param year_of_release Year of release.
#' @param season if \code{type} is \code{series} or \code{episode} then it possible
#' to search within a \code{season} AND \code{epispde} (both required)
#' to search within a \code{season} AND \code{episode} (both required)
#' @param episode if \code{type} is \code{series} or \code{episode} then it possible
#' to search within a \code{season} AND \code{epispde} (both required)
#' to search within a \code{season} AND \code{episode} (both required)
#' @param plot Return \code{short} or \code{full} plot.
#' @param include_tomatoes Include Rotten Tomatoes ratings.
#' @seealso \href{omdbAPI documentation}{http://www.omdbapi.com/}
Expand Down
Binary file modified README-usage-1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
4 changes: 4 additions & 0 deletions README.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ The following functions are implemented:

- `find_by_id`: Retrieve OMDB info by IMDB ID search
- `find_by_title`: Retrieve OMDB info by title search
- `get_tv_data`: Retrieve IMDB ratings for all episodes of a TV series
- `get_actors`: Get actors from an omdb object as a vector
- `get_countries`: Get countries from an omdb object as a vector
- `get_directors`: Get directors from an omdb object as a vector
Expand All @@ -33,6 +34,7 @@ The following functions are implemented:
- ibartomeus adds pagination option.
- Version `0.1.0.9000` released
- Version `0.2.0.9000` released - better types in the data frames and `get_` methods to split the fields with multiple entries
- Stan125 adds function to obtain IMDB ratings for a TV series

### Installation

Expand Down Expand Up @@ -67,6 +69,8 @@ find_by_title(games$Title[1])

find_by_title("Game of Thrones", type="series", season=1, episode=1)

get_tv_data("True Detective")

get_genres(find_by_title("Star Trek: Deep Space Nine", season=5, episode=7))

get_writers(find_by_title("Star Trek: Deep Space Nine", season=4, episode=6))
Expand Down
86 changes: 47 additions & 39 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,46 +1,35 @@
---
output:
md_document:
variant: markdown_github
---

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



omdbapi is an R package wrapper for the [Open Movie Database API](http://www.omdbapi.com/)

The following functions are implemented:

- `find_by_id`: Retrieve OMDB info by IMDB ID search
- `find_by_title`: Retrieve OMDB info by title search
- `get_actors`: Get actors from an omdb object as a vector
- `get_countries`: Get countries from an omdb object as a vector
- `get_directors`: Get directors from an omdb object as a vector
- `get_genres`: Get genres from an omdb object as a vector
- `get_writers`: Get writers from an omdb object as a vector
- `print.omdb`: Print an omdb result
- `search_by_title`: Lightweight omdb title search
- `find_by_id`: Retrieve OMDB info by IMDB ID search
- `find_by_title`: Retrieve OMDB info by title search
- `get_tv_data`: Retrieve IMDB ratings for all episodes of a TV series
- `get_actors`: Get actors from an omdb object as a vector
- `get_countries`: Get countries from an omdb object as a vector
- `get_directors`: Get directors from an omdb object as a vector
- `get_genres`: Get genres from an omdb object as a vector
- `get_writers`: Get writers from an omdb object as a vector
- `print.omdb`: Print an omdb result
- `search_by_title`: Lightweight omdb title search

### News

- ibartomeus adds pagination option.
- Version `0.1.0.9000` released
- Version `0.2.0.9000` released - better types in the data frames and `get_` methods to split the fields with multiple entries
- ibartomeus adds pagination option.
- Version `0.1.0.9000` released
- Version `0.2.0.9000` released - better types in the data frames and `get_` methods to split the fields with multiple entries
- Stan125 adds function to obtain IMDB ratings for a TV series

### Installation


```r
``` r
devtools::install_github("hrbrmstr/omdbapi")
```



### Usage


```r
``` r
library(omdbapi)
library(dplyr)
#>
Expand All @@ -52,7 +41,7 @@ library(dplyr)
#>
#> intersect, setdiff, setequal, union
library(pbapply)
#> Error in library(pbapply): there is no package called 'pbapply'
#> Warning: package 'pbapply' was built under R version 3.2.4

# current verison
packageVersion("omdbapi")
Expand Down Expand Up @@ -80,8 +69,8 @@ search_by_title("Captain America", page = 2)
#>
#> Title Year
#> (chr) (chr)
#> 1 Captain America and the Avengers 1991
#> 2 Marvel's Captain America: 75 Heroic Years 2016
#> 1 Marvel's Captain America: 75 Heroic Years 2016
#> 2 Captain America and the Avengers 1991
#> 3 A Look Back at 'Captain America' 2013
#> 4 Captain America XXX: An Axel Braun Parody 2014
#> 5 Captain America XXX: An Extreme Comixxx Parody 2011
Expand Down Expand Up @@ -154,10 +143,31 @@ find_by_title("Game of Thrones", type="series", season=1, episode=1)
#> MV5BMTk5MDU3OTkzMF5BMl5BanBnXkFtZTcwOTc0ODg5NA@@._V1_SX300.jpg
#> Metascore: N/A
#> imdbRating: 8.9
#> imdbVotes: 13914
#> imdbVotes: 14119
#> imdbID: tt1480055
#> Type: episode

get_tv_data("True Detective")
#> Last episode of Season 1 was episode 8
#> Last episode of Season 2 was episode 8
#> Season Episode imdbRating episode.no
#> 1 1 1 9.0 1
#> 2 1 2 8.9 2
#> 3 1 3 9.2 3
#> 4 1 4 9.7 4
#> 5 1 5 9.6 5
#> 6 1 6 9.1 6
#> 7 1 7 9.2 7
#> 8 1 8 9.5 8
#> 9 2 1 8.0 9
#> 10 2 2 8.1 10
#> 11 2 3 7.7 11
#> 12 2 4 8.5 12
#> 13 2 5 8.1 13
#> 14 2 6 8.7 14
#> 15 2 7 9.1 15
#> 16 2 8 8.4 16

get_genres(find_by_title("Star Trek: Deep Space Nine", season=5, episode=7))
#> [1] "Action" "Adventure" "Drama"

Expand All @@ -169,7 +179,7 @@ get_writers(find_by_title("Star Trek: Deep Space Nine", season=4, episode=6))
#> [5] "John J. Ordover"

get_directors(find_by_id("tt1371111"))
#> [1] "Tom Tykwer" "Andy Wachowski" "Lana Wachowski"
#> [1] "Tom Tykwer" "Lana Wachowski" "Lilly Wachowski"

get_countries(find_by_title("The Blind Swordsman: Zatoichi"))
#> [1] "Japan"
Expand All @@ -185,21 +195,20 @@ boxplot(zato$imdbRating, horizontal=TRUE, main="IMDB Rating", ylim=c(0, 10))
boxplot(zato$tomatoUserRating, horizontal=TRUE, main="Tomato User Rating", ylim=c(0, 5))
```

![plot of chunk usage](README-usage-1.png)
![](README-usage-1.png)<!-- -->

```r
``` r
par(mfrow=c(1,1))
```

### Test Results


```r
``` r
library(omdbapi)
library(testthat)

date()
#> [1] "Sat Feb 13 21:47:32 2016"
#> [1] "Thu Mar 17 14:27:05 2016"

test_dir("tests/")
#> testthat results ================================================================
Expand All @@ -208,5 +217,4 @@ test_dir("tests/")

### Code of Conduct

Please note that this project is released with a [Contributor Code of Conduct](CONDUCT.md).
By participating in this project you agree to abide by its terms.
Please note that this project is released with a [Contributor Code of Conduct](CONDUCT.md). By participating in this project you agree to abide by its terms.
24 changes: 24 additions & 0 deletions man/get_tv_data.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.