Skip to content

Commit

Permalink
fixed #4
Browse files Browse the repository at this point in the history
  • Loading branch information
hrbrmstr committed May 22, 2017
1 parent a4787e5 commit 3b69849
Show file tree
Hide file tree
Showing 28 changed files with 316 additions and 279 deletions.
9 changes: 4 additions & 5 deletions DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
Package: omdbapi
Title: Tools to Access Movie, Television and Game Data from the 'Open Movie Database'
Version: 0.2.0.9000
Authors@R: c(person("Bob", "Rudis", email = "bob@rudis.net", role = c("aut", "cre")))
Version: 0.3.0
Authors@R: c(person("Bob", "Rudis", email = "bob@rud.is", role = c("aut", "cre")))
Description: Provides API access to the 'Open Movie Database' which maintains metadata
about movies, games and television shows through a public API.
Depends: R (>= 3.1.0)
License: MIT + file LICENSE
LazyData: true
Suggests: testthat
Imports: httr,
dplyr,
stringr
Imports: httr, dplyr, stringr
RoxygenNote: 6.0.1
4 changes: 2 additions & 2 deletions NAMESPACE
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
# 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)
export(find_by_title)
export(get_actors)
export(get_countries)
export(get_directors)
export(get_genres)
export(get_writers)
export(omdb_api_key)
export(search_by_title)
import(dplyr)
import(httr)
Expand Down
8 changes: 8 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# omdbapi 0.3.0

* Added support for new, required API keys.
* Also moved to use https vs http since API keys are now required and cleartext is bad.
* Added a `NEWS.md` file to track changes to the package.



71 changes: 36 additions & 35 deletions R/omdb_api_key.R
Original file line number Diff line number Diff line change
@@ -1,35 +1,36 @@
# #' Get or set OMDBAPI_KEY value
# #'
# #' The API wrapper functions in this package all rely on a omdb API
# #' key residing in the environment variable \code{OMDBAPI_KEY}. The
# #' easiest way to accomplish this is to set it in the `\code{.Renviron}` file in your
# #' home directory.
# #'
# #' @param force force setting a new omdb API key for the current environment?
# #' @return atomic character vector containing the omdb API key
# #' @note an omdb API key is only necessary for "poster" access
# #' @export
# omdb_api_key <- function(force = FALSE) {
#
# env <- Sys.getenv('OMDBAPI_KEY')
# if (!identical(env, "") && !force) return(env)
#
# if (!interactive()) {
# stop("Please set env var OMDBAPI_KEY to your omdb API key",
# call. = FALSE)
# }
#
# message("Couldn't find env var OMDBAPI_KEY See ?omdb_api_key for more details.")
# message("Please enter your API key and press enter:")
# pat <- readline(": ")
#
# if (identical(pat, "")) {
# stop("PassiveTotal API key entry failed", call. = FALSE)
# }
#
# message("Updating OMDBAPI_KEY env var to PAT")
# Sys.setenv(OMDBAPI_KEY = pat)
#
# pat
#
# }
#' Get or set OMDB_API_KEY value
#'
#' The API wrapper functions in this package all rely on a OMDB API
#' key residing in the environment variable `OMDB_API_KEY`. The
#' easiest way to accomplish this is to set it in the `.Renviron` file in your
#' home directory.
#'
#' @md
#' @param force force setting a new OMDB API key for the current environment?
#' @return atomic character vector containing the OMDB API key
#' @note The "poster" API requires a separate key
#' @export
omdb_api_key <- function(force = FALSE) {

env <- Sys.getenv('OMDB_API_KEY')
if (!identical(env, "") && !force) return(env)

if (!interactive()) {
stop("Please set env var OMDB_API_KEY to your OMDB API key",
call. = FALSE)
}

message("Couldn't find env var OMDB_API_KEY See ?omdb_api_key for more details.")
message("Please enter your API key and press enter:")
pat <- readline(": ")

if (identical(pat, "")) {
stop("OMDB API key entry failed", call. = FALSE)
}

message("Updating OMDB_API_KEY env var to PAT")
Sys.setenv(OMDB_API_KEY = pat)

pat

}
6 changes: 3 additions & 3 deletions R/omdbapi-package.r
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
#' A package to access movie, television and game metadat via the Open Movie Database API
#'
#' @md
#' @name omdbapi
#' @docType package
#' @author Bob Rudis (@@hrbrmstr)
#' @import httr dplyr
#' @importFrom stringr str_pad
#' @importFrom stringr str_wrap
#' @importFrom stringr str_split
#' @importFrom stringr str_pad str_wrap str_split
NULL
79 changes: 47 additions & 32 deletions R/title_or_id.R
Original file line number Diff line number Diff line change
@@ -1,21 +1,24 @@
#' Retrieve OMDB info by title search
#'
#' @md
#' @param title Movie title to search for.
#' @param type Type of result to return. One of \code{[movie|series|episode|game]}.
#' @param type Type of result to return. One of `[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
#' @param season if `type` is \code{series} or \code{episode} then it possible
#' to search within a \code{season} AND \code{epispde} (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)
#' @param plot Return \code{short} or \code{full} plot.
#' @param include_tomatoes Include Rotten Tomatoes ratings.
#' @param api_key OMDB API key. See [omdb_api_key()] & <https://www.patreon.com/bePatron?u=5038490> for more information.
#' @seealso \href{omdbAPI documentation}{http://www.omdbapi.com/}
#' @return \code{tbl_df}/\code{data.frame} of search results (an empty one if none found). Also
#' classes as an \code{omdb} object for prettier printing
#' @note The API's search results limit to 10.
#' @export
find_by_title <- function(title, type=NULL, season=NULL, episode=NULL,
year_of_release=NULL, plot="short", include_tomatoes=FALSE) {
year_of_release=NULL, plot="short", include_tomatoes=FALSE,
api_key=omdb_api_key()) {

if (!is.null(type)) {
if (!type %in% c("movie", "series", "episode", "game")) {
Expand All @@ -25,7 +28,7 @@ find_by_title <- function(title, type=NULL, season=NULL, episode=NULL,
}

params <- list(t=title, type=type, y=year_of_release,
plot=plot, r="json", tomatoes=include_tomatoes)
plot=plot, r="json", tomatoes=include_tomatoes, apikey=api_key)

if (!is.null(season) & is.null(episode) |
!is.null(episode) & is.null(season)) {
Expand All @@ -36,17 +39,16 @@ find_by_title <- function(title, type=NULL, season=NULL, episode=NULL,
params["Episode"] <- episode
}


resp <- GET(OMDB_BASE_URL, query=params)
stop_for_status(resp)
tmp <- content(resp, as="parsed")
resp <- httr::GET(OMDB_BASE_URL, query=params)
httr::stop_for_status(resp)
tmp <- httr::content(resp, as="parsed")

if (tmp$Response == "False") {
message(tmp$Error)
return(data_frame())
}

ret <- as_data_frame(tmp)
ret <- dplyr::as_data_frame(tmp)
tmp[ tmp == "N/A" ] <- NA
class(ret) <- c("omdb", class(ret))

Expand All @@ -56,32 +58,35 @@ find_by_title <- function(title, type=NULL, season=NULL, episode=NULL,

#' Retrieve OMDB info by IMDB ID search
#'
#' @md
#' @param id A valid IMDb ID (e.g. \code{tt1285016})
#' @param type Type of result to return. One of \code{[movie|series|episode|game]}.
#' @param year_of_release Year of release.
#' @param plot Return \code{short} or \code{full} plot.
#' @param include_tomatoes Include Rotten Tomatoes ratings.
#' @param api_key OMDB API key. See [omdb_api_key()] & <https://www.patreon.com/bePatron?u=5038490> for more information.
#' @seealso \href{omdbAPI documentation}{http://www.omdbapi.com/}
#' @return \code{tbl_df}/\code{data.frame} of search results (an empty one if none found). Also
#' classes as an \code{omdb} object for prettier printing
#' @note The API's search results limit to 10.
#' @export
find_by_id <- function(id, type=NULL, year_of_release=NULL,
plot="short", include_tomatoes=FALSE) {
plot="short", include_tomatoes=FALSE,
api_key=omdb_api_key()) {


params <- list(i=id, type=type, y=year_of_release,
plot=plot, r="json", tomatoes=include_tomatoes)
resp <- GET(OMDB_BASE_URL, query=params)
stop_for_status(resp)
tmp <- content(resp, as="parsed")
plot=plot, r="json", tomatoes=include_tomatoes, apikey=api_key)
resp <- httr::GET(OMDB_BASE_URL, query=params)
httr::stop_for_status(resp)
tmp <- httr::content(resp, as="parsed")

if (tmp$Response == "False") {
message(tmp$Error)
return(data_frame())
}

ret <- as_data_frame(tmp)
ret <- dplyr::as_data_frame(tmp)
tmp[ tmp == "N/A" ] <- NA
class(ret) <- c("omdb", class(ret))

Expand All @@ -91,41 +96,43 @@ find_by_id <- function(id, type=NULL, year_of_release=NULL,

#' Lightweight omdb title search
#'
#' @md
#' @param term Movie title to search for.
#' @param type Type of result to return. One of \code{[movie|series|episode|game]}.
#' @param year_of_release Year of release.
#' @param page 1 for first 10 results, 2 for next ten, etc...
#' @param api_key OMDB API key. See [omdb_api_key()] & <https://www.patreon.com/bePatron?u=5038490> for more information.
#' @seealso \href{omdbAPI documentation}{http://www.omdbapi.com/}
#' @return \code{tbl_df}/\code{data.frame} of search results (an empty one if none found)
#' @note The API's search results limit to 10 at a time (see argument page).
#' @export
search_by_title <- function(term, type=NULL, year_of_release=NULL, page = 1) {

search_by_title <- function(term, type=NULL, year_of_release=NULL, page = 1,
api_key=omdb_api_key()) {

params <- list(s=term, type=type, y=year_of_release, page = page, r="json")
resp <- GET(OMDB_BASE_URL, query=params)
stop_for_status(resp)
tmp <- content(resp, as="parsed")
params <- list(s=term, type=type, y=year_of_release, page = page, r="json", apikey=api_key)
resp <- httr::GET(OMDB_BASE_URL, query=params)
httr::stop_for_status(resp)
tmp <- httr::content(resp, as="parsed")

if (!("Search" %in% names(tmp))) {
message(tmp$Error)
return(data_frame())
}

bind_rows(lapply(tmp$Search, as.data.frame, stringsAsFactors=FALSE))
dplyr::bind_rows(lapply(tmp$Search, as.data.frame, stringsAsFactors=FALSE))

}

#' Print an omdb result
#'
#' If either \code{find_} function finds an omdb entry, the API only returns
#' a single record. That makes it possible to make a nice \code{print} routine
#' for it so the output is easier to read in interactive mode.
#'
#' @param x omdb object
#' @param \dots ignored
#' @method print omdb
#' @export
# Print an omdb result
#
# If either \code{find_} function finds an omdb entry, the API only returns
# a single record. That makes it possible to make a nice \code{print} routine
# for it so the output is easier to read in interactive mode.
#
# @param x omdb object
# @param \dots ignored
# @method print omdb
# @export
print.omdb <- function(x, ...) {

x <- as.data.frame(x, stringsAsFactors=FALSE)
Expand All @@ -134,6 +141,14 @@ print.omdb <- function(x, ...) {

# all possible API returns

all_cols <- c("Title", "Year", "Rated", "Released", "Runtime", "Genre", "Director",
"Writer", "Actors", "Plot", "Language", "Country", "Awards",
"Poster", "Ratings", "Metascore", "imdbRating", "imdbVotes",
"imdbID", "Type", "tomatoMeter", "tomatoImage", "tomatoRating",
"tomatoReviews", "tomatoFresh", "tomatoRotten", "tomatoConsensus",
"tomatoUserMeter", "tomatoUserRating", "tomatoUserReviews", "tomatoURL",
"DVD", "BoxOffice", "Production", "Website", "Response")

all_cols <- c("Title", "Year", "Rated", "Released", "Runtime", "Genre", "Director",
"Writer", "Actors", "Plot", "Language", "Country", "Awards",
"Poster", "Metascore", "imdbRating", "imdbVotes", "imdbID", "Type",
Expand Down
2 changes: 1 addition & 1 deletion R/utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ fix_omdb <- function(x) {
}

ID_download <- function(ID){
bind_rows(lapply(ID, function(x) {
dplyr::bind_rows(lapply(ID, function(x) {
find_by_id(x, include_tomatoes = TRUE)
}))
}
2 changes: 1 addition & 1 deletion R/zzz.R
Original file line number Diff line number Diff line change
@@ -1 +1 @@
OMDB_BASE_URL <- "http://www.omdbapi.com/"
OMDB_BASE_URL <- "https://www.omdbapi.com/"
Binary file added README-unnamed-chunk-1-1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added README-unnamed-chunk-2-1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading

0 comments on commit 3b69849

Please sign in to comment.