Skip to content

Instantly share code, notes, and snippets.

@ganzuul
Last active November 3, 2019 12:52
Show Gist options
  • Save ganzuul/fe95abc1ea2f4f5e97d050c1694671c9 to your computer and use it in GitHub Desktop.
Save ganzuul/fe95abc1ea2f4f5e97d050c1694671c9 to your computer and use it in GitHub Desktop.
edges case is broken
#install.packages("httr")
#install.packages("jsonlite")
#install.packages("tidyverse")
#install.packages("stringr")
require(httr)
require(jsonlite)
require(tidyverse)
require(stringr)
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(base,v, sep="")}
getLh <- 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) -> urls
map(urls, function(v){v %>% map(getLh)}) -> df1
#edges <- function(v){ v %>% map(~select(.x,c(4,7)))}; df <- map(df1,edges) #broken
#edges <- function(v){ v %>% map(~pluck(.x,7))}; df <- map(df1,edges) #works but single-column
e <- function(v){select(v,c(4,7))}; df <- map(df1[[1]],e)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment