|
| 1 | +#' Search for Setlist |
| 2 | +#' @param apikey apikey |
| 3 | +#' @param setlist the show setlist in YYYY-MM-DD format |
| 4 | +#' |
| 5 | +#' @importFrom attempt stop_if_all |
| 6 | +#' @importFrom purrr compact |
| 7 | +#' @importFrom jsonlite fromJSON |
| 8 | +#' @importFrom httr GET |
| 9 | +#' @export |
| 10 | +#' @import jsonlite |
| 11 | +#' @importFrom textreadr read_html |
| 12 | +#' @import httr |
| 13 | +#' @importFrom dplyr filter |
| 14 | +#' @importFrom stringr str_split_fixed |
| 15 | +#' @importFrom zoo na.locf |
| 16 | +#' @rdname getsetlist |
| 17 | +#' |
| 18 | +#' @return the results from the search |
| 19 | +#' @examples |
| 20 | +#' \dontrun{ |
| 21 | +#' get_setlist(apikey = "<apikey>", showdate = "1999-12-31") |
| 22 | +#' } |
| 23 | + |
| 24 | +get_setlist <- function(apikey = NULL, |
| 25 | + showdate = NULL){ |
| 26 | + |
| 27 | + args <- list(apikey = apikey, showdate = showdate) |
| 28 | + |
| 29 | + # Check that at least one argument is not null |
| 30 | + # stop_if_all(apikey, is.null, "You need to specify the API key!") |
| 31 | + # Chek for internet |
| 32 | + check_internet() |
| 33 | + # Create the |
| 34 | + res <- GET( |
| 35 | + paste0( |
| 36 | + base_url, |
| 37 | + "setlists/get?apikey=", |
| 38 | + apikey, |
| 39 | + "&showdate=", |
| 40 | + showdate, |
| 41 | + sep="")) |
| 42 | + |
| 43 | + # Check the result |
| 44 | + check_status(res) |
| 45 | + cont <- content(res) |
| 46 | + |
| 47 | + # Get the content and return it as a data.frame |
| 48 | + # using an if statements lets deal with the shows that do have data |
| 49 | + |
| 50 | + set <- list() |
| 51 | + |
| 52 | + if (length(cont$response$data) > 0) { |
| 53 | + |
| 54 | + # cont$response$data[[1]]$setlistdata |
| 55 | + # contains the setlist data in html which is a hot mess |
| 56 | + # cont$response$data[[1]]$setlistdata |
| 57 | + # contains the setlist data in html which is a hot mess |
| 58 | + set_html <- textreadr::read_html(cont$response$data[[1]]$setlistdata) |
| 59 | + |
| 60 | + # combine the colon with the previous line |
| 61 | + # because colons are found with "Set 1:", "Set 2:" etc. |
| 62 | + ind.colon <- which(set_html == ":") |
| 63 | + ind.set <- ind.colon - 1 |
| 64 | + |
| 65 | + # paste the colon and the prior line together |
| 66 | + # to concatenate Set 1 and : |
| 67 | + set_html[ind.set] <- paste(set_html[ind.set], set_html[ind.colon], sep = "") |
| 68 | + |
| 69 | + # make into data frame |
| 70 | + set_html <- data.frame(set_html) |
| 71 | + # call the song column Text |
| 72 | + set_html$Text <- as.character(set_html$set_html) |
| 73 | + set_html$set_html <- NULL |
| 74 | + |
| 75 | + # remove cells containing brackets, this is a vestage of show notes |
| 76 | + # remove cells only containing special characters |
| 77 | + set_html <- filter(set_html, !grepl("\\[",set_html$Text)) |
| 78 | + set_html <- filter(set_html, !grepl("^:$",set_html$Text)) |
| 79 | + |
| 80 | + # create a character string for song names |
| 81 | + # the easiest filter is that they should include all alphabetical characters |
| 82 | + # but then we also need to search for the songs that are numbers |
| 83 | + # call others false |
| 84 | + boo <- c("[a-zA-Z]+", "1999", "555", "5:15") |
| 85 | + |
| 86 | + # now we can set our songs to true and the rest to false |
| 87 | + set_html$Boolian <- grepl(paste(boo, collapse = "|"), set_html$Text) |
| 88 | + |
| 89 | + # now we can take the cells that were returned as false |
| 90 | + # and put those into a new column for segues |
| 91 | + ind.FALSE <- which(set_html$Boolian == FALSE) |
| 92 | + ind.CAT <- ind.FALSE - 1 |
| 93 | + |
| 94 | + set_html$Text[ind.CAT] <- paste(set_html$Text[ind.CAT], set_html$Text[ind.FALSE], sep = "__") |
| 95 | + set_html <- filter(set_html, set_html$Boolian == TRUE) |
| 96 | + set_html <- str_split_fixed(set_html$Text, "__", 2) |
| 97 | + colnames(set_html) <- c("Text", "Segue") |
| 98 | + |
| 99 | + set_html <- data.frame(set_html) |
| 100 | + |
| 101 | + # now we can remove the rows containing the set |
| 102 | + # and use that data to populate which set each show belongs in |
| 103 | + # we will use a combination of a colon and the colon being |
| 104 | + # the last character in the string (because of RS song 5:15) |
| 105 | + |
| 106 | + substrRight <- function(x, n){ |
| 107 | + substr(x, nchar(x)-n+1, nchar(x)) |
| 108 | + } |
| 109 | + |
| 110 | + set_html$Set <- ifelse(substrRight(as.character(set_html$Text), 1) == ":", paste(set_html$Text), NA) |
| 111 | + set_html$Set <- na.locf(set_html$Set) |
| 112 | + set_html <- filter(set_html, set_html$Text != set_html$Set) |
| 113 | + |
| 114 | + |
| 115 | + } else { |
| 116 | + # in the case that the show had no data set that list element to NA |
| 117 | + set_html <- NA |
| 118 | + } |
| 119 | + return(set_html) |
| 120 | +} |
| 121 | + |
| 122 | +#' @export |
| 123 | +#' @rdname getsetlist |
0 commit comments