R/random.R

#' Randomly select Wikipedia pages
#' 
#' @param nb The number of pages to select
#' @param domain The domain where the wiki is located
#' @param namespace The namespace in which to select pages.
#'
#' @importFrom httr GET content
#' @export

random <- function(nb, domain = "fr", namespace = "0") {
  quotient <- nb%/%500
  reste <- nb%%500
  
  result <- data.frame(matrix(ncol=3))
  
  if (quotient > 0) {
    for (i in 1:quotient) {
      query <- list(action = "query", list = "random", format = "json", rnlimit = "max", rnnamespace = namespace, redirects = "")
      
      exec <- GET(paste("https://", domain, ".wikipedia.org/w/api.php", sep = ""), query = query)
      content <- content(exec, "parsed")
      content <- content[["query"]][[1]]
      content <- data.frame(matrix(unlist(content),ncol=3,byrow=T))
      result <- rbind(result, content)
    }
  }
  
  if (reste != 0) {
    query <- list(action = "query", list = "random", format = "json", rnlimit = reste, rnnamespace = namespace, redirects = "")
    
    exec <- GET(paste("https://", domain, ".wikipedia.org/w/api.php", sep = ""), query = query)
    content <- content(exec, "parsed")
    content <- content[["query"]][[1]]
    content <- data.frame(matrix(unlist(content),ncol=3,byrow=T))
    result <- rbind(result, content)
    result <- unique(result)
  }
  
  result <- result[-1,]
  rownames(result) <- NULL
  return(result)
} 
leojoubert/WikiSocio documentation built on May 21, 2019, 5:08 a.m.