diff --git a/NAMESPACE b/NAMESPACE index 2e379be..3507e73 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -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) @@ -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) diff --git a/R/get_imdb.R b/R/get_imdb.R new file mode 100644 index 0000000..7040ff7 --- /dev/null +++ b/R/get_imdb.R @@ -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")) +} \ No newline at end of file diff --git a/R/title_or_id.R b/R/title_or_id.R index 47f50e4..a099bf2 100644 --- a/R/title_or_id.R +++ b/R/title_or_id.R @@ -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/} diff --git a/README-usage-1.png b/README-usage-1.png index 33705de..c95b35f 100644 Binary files a/README-usage-1.png and b/README-usage-1.png differ diff --git a/README.Rmd b/README.Rmd index 1a93887..be79538 100644 --- a/README.Rmd +++ b/README.Rmd @@ -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 @@ -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 @@ -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)) diff --git a/README.md b/README.md index 304790c..b13dd93 100644 --- a/README.md +++ b/README.md @@ -1,46 +1,35 @@ ---- -output: - md_document: - variant: markdown_github ---- - - - - 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) #> @@ -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") @@ -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 @@ -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" @@ -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" @@ -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 ================================================================ @@ -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. diff --git a/man/get_tv_data.Rd b/man/get_tv_data.Rd new file mode 100644 index 0000000..09c9986 --- /dev/null +++ b/man/get_tv_data.Rd @@ -0,0 +1,24 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/get_imdb.R +\name{get_tv_data} +\alias{get_tv_data} +\title{Retrieve IMDB ratings for all Episodes of a TV series} +\usage{ +get_tv_data(name) +} +\arguments{ +\item{name}{TV series for which to obtain ratings for. Has to be of IMDB type "TV series".} +} +\value{ +\code{data.frame} +} +\description{ +Retrieve IMDB ratings for all Episodes of a TV series. +} +\note{ +Can be quite slow, depending on your internet connection. +} +\seealso{ +\href{omdbAPI documentation}{http://www.omdbapi.com/} +} +