Skip to content

Instantly share code, notes, and snippets.

@ganzuul
Last active November 10, 2019 19:53
Show Gist options
  • Save ganzuul/2cae364c0b9f7ae7beae5de44bacc328 to your computer and use it in GitHub Desktop.
Save ganzuul/2cae364c0b9f7ae7beae5de44bacc328 to your computer and use it in GitHub Desktop.
en.wikipedia.org/wiki/Special:WhatLinksHere to R via REST API
#install.packages("httr") #install.packages("jsonlite") #install.packages("tidyverse")
#install.packages("stringr") #install.packages("furrr") #install.packages("igraph")
require(httr)
require(jsonlite)
require(tidyverse)
require(stringr)
#require(furrr) #require(igraph)
url_base <- "https://en.wikipedia.org/w/api.php?action=query&format=json&prop=linkshere&lhlimit=500"
articles <- c("Maximal and minimal elements", "Maxima and minima")
titles <- function(v) {paste("&titles=", v, sep = "")}
ids <- function(v) {paste("&pageids=", v, sep = "")}
query <- function(v) {paste(url_base, v, sep = "")}
get_lh <- function(v) {as.data.frame(fromJSON(content(GET(v), "text"), flatten = FALSE))}
map(articles, URLencode) %>%
map(titles) %>%
map(query) %>%
map(getLh) %>%
map(~select(.x, c(4, 5, 7, 8))) %>%
map(~as_tibble(.x, .name_repair = "minimal")) -> tibs
map(tibs, function(v) {filter(v, complete.cases(v))}) %>% # Convenient NAs in data.
map(~select(.x, c(2))) %>%
map(flatten_chr) %>%
map(ids) %>%
map(as.list) %>%
map(query) %>%
map(as.list) %>%
map(function(v) {v %>% map(get_lh)}) -> df1
df1 %>%
map(function(v) {v %>% map(~select(.x, c(ends_with(".pageid"), ends_with(".title"))))}) -> tibs2
map(df1, function(v) {v %>% map(~select(.x, ends_with(".title")))}) %>%
map(function(v) {v %>% map(~as.matrix(.x))}) -> g1
g1[[1]] %>%
map(graph_from_edgelist) -> g
#map(g1, function(v){v %>% map(~graph_from_edgelist(.x))}) -> g
#truncCol <- function(v){names(v) %>% map(~str_trunc(.x, str_length(.x)-12, side = c("left"),ellipsis=""))}
#tib0 <- map(tibs, function(v){names(v) <- truncCol(v);return(v)}) # TO-DO Why is this return special?
#df <- graph.edgelist(matrix
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment