R/main.R

#' Title
#'
#' @param ...
#' @param token
#'
#' @return
#' @export
#'
#' @examples
genius_GET <- function(..., token = genius_get_token()){
    http_resp <- GET(
        "https://api.genius.com",
        ... = ...,
        add_headers(Authorization = paste("Bearer", token))
    )
    stop_for_status(http_resp)
    content(http_resp, "parsed", "application/json")$response
}

#' Title
#'
#' @param query seach query
#' @param per_page number of search results per page
#' @param token Genius API client access token. By default it will try to read the token
#' from the environment variable.
#'
#' @return a data.frame - one row per search result
#' @export
#'
#' @examples
genius_search <- function(query, per_page = 10, token = genius_get_token()){
    resp <- genius_GET(
        path = "search",
        query = list(q = query, per_page = per_page),
        token = token
    )
    hits <- resp$hits
    hits <- hits %>%
        map(flatten) %>%
        map(flatten_preserve_parent_names) %>%
        map(data.frame, stringsAsFactors = FALSE)
    do.call(dplyr::bind_rows, hits)
}


#' Title
#'
#' @param id artists's id on genius
#' @inheritParams genius_search
#'
#' @return ???
#' @export
#'
#' @examples
genius_artist <- function(id, token = genius_get_token()){
    #check that id a valid numeric
    resp <- genius_GET(
        path = paste0("artists/", id),
        token = token
    )
    resp
}
idmn/genius documentation built on May 27, 2019, 7:26 a.m.