R/get_headlines.R

## In the same source file (to remind you that you did it) add:
if (getRversion() >= "2.15.1") utils::globalVariables(c("."))
#' Get news based on sources category, language and country
#' @param country String category
#' @param category String language
#' @param sources String country
#' @param q Keywords to search for.
#' @param apiKey String apiKey
#' @param endpoint String of endpoints

#' @return dataframe

#' @importFrom curl has_internet
#' @importFrom httr http_error GET content status_code
#' @importFrom purrr map
#' @importFrom plyr rbind.fill
#' @importFrom magrittr %>%
#' @importFrom utils globalVariables

#' @examples
#'\dontrun{get_headlines(category = 'general',country = 'de')}
#' @export
get_headlines <- function(country, category, sources, q, apiKey = newsapi_key(),
  endpoint = "TOP-HEADLINES") {
  argg <- as.list(environment())

  if ((curl::has_internet())) {

    if (missing(country) & missing(category) & missing(sources) & missing(q)) {
      stop("Please pass at least one of the following sources: country, category, sources or q",
        call. = FALSE)
    }
    if (all(names(argg) %in% c("country", "category", "sources"))) {
      stop("you can't mix this param with the country or category params",
        call. = FALSE)
    }

    if (!missing(country)) {
      if (!all(country %in% c("ae", "at", "be", "bg", "ch", "co", "cu", "cz", "eg", "de", "gr", "hu",
                              "id", "il", "jp", "kr", "lt", "lv", "ma", "mx", "my", "ng", "nz",
                              "ph", "pl", "pt", "ro", "rs", "sa", "se", "sg", "si", "sk", "th", "tr",
                              "tw", "ua", "us", "ve", "za"))) {
        stop("country must be one of these: ae, at, be, bg, ch, co, cu, cz, de, eg, gr, hu,
                                            id, il, jp, kr, lt, lv, ma, mx, my, ng, nz, ph
                                            pl, pt, ro, rs, sa, se, sg, si, sk, th, tr, tw, ua, us, ve, za")
      }
    }

    if (!missing(category)) {
      if (!all(category %in% c("business", "entertainment", "gaming", "general",
        "health-and-medical", "music", "politics", "science-and-nature",
        "sport", "technology"))) {
        stop("category must be one of these: business, entertainment, gaming, general, health-and-medical, music, politics, science-and-nature, sport, technology")
      }
    }

    resp <- httr::GET(create_url(endpoint, argg))
    if (httr::http_error(resp)) {
      stop("Newsapi request failed ", httr::status_code(resp))
      call. = FALSE
    } else {
      cat("dowloading...\n")
      source_list <- jsonlite::fromJSON(httr::content(resp, "text"))
      cat("finished dowloading...\n")
      source_list <- source_list["articles"]
    }
  } else {
    stop("No local internet connection available", call. = FALSE)
  }

  source_df <- lapply(source_list, function(x) {
    source <- x$source
    x$source <- NULL
    list(x, source)
  }) %>% purrr::map(~data.frame(.)) %>% do.call(plyr::rbind.fill, .)
  return(source_df)
}
data-atelier/newsExploreR documentation built on May 22, 2019, 11:51 p.m.