-
Notifications
You must be signed in to change notification settings - Fork 13
Adding get_tv_data() #2
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Stan125
wants to merge
4
commits into
hrbrmstr:master
Choose a base branch
from
Stan125:master
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 3 commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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")) | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
why is the
r = "json"sitting outside thelist()?