Skip to content

Instantly share code, notes, and snippets.

@nevrome
Created May 18, 2017 19:23
Show Gist options
  • Save nevrome/47864c990903e3ea9fa431bb4d7df56e to your computer and use it in GitHub Desktop.
Save nevrome/47864c990903e3ea9fa431bb4d7df56e to your computer and use it in GitHub Desktop.
drafts of wikimedia API R functions
library(httr)
library(magrittr)
library(tidyverse)
clientlogin <- function(user, pw) {
# get login token
token <- httr::modify_url(
"http://127.0.0.1:8080/w/api.php",
query = list(
action = "login",
lgname = user,
format = "json"
)
) %>% httr::POST(
) %>% httr::content(
as = "parsed"
) %$% login %$% token
# login
httr::modify_url(
"http://127.0.0.1:8080/w/api.php",
query = list(
action = "clientlogin",
username = user,
rememberMe = 1,
loginreturnurl = "http://127.0.0.1:8080",
format = "json"
)
) %>%
httr::POST(
body = list(
logintoken = token,
password = pw
)
) %>%
httr::content(
as = "parsed"
) %>% return()
}
get_user_info <- function() {
url <- httr::modify_url(
"http://127.0.0.1:8080/w/api.php",
query = list(
action = "query",
meta = "userinfo",
format = "json"
)
)
api_result_raw <- httr::GET(url)
api_result <- api_result_raw %>% httr::content(as = "parsed")
return(api_result)
}
get_token <- function() {
url <- httr::modify_url(
"http://127.0.0.1:8080/w/api.php",
query = list(
action = "query",
meta = "tokens",
format = "json"
)
)
api_result_raw <- httr::GET(url)
api_result <- api_result_raw %>% httr::content(as = "parsed")
return(api_result$query$tokens$csrftoken)
}
create_article <- function(article_title, article_text, tok = toki) {
httr::modify_url(
"http://127.0.0.1:8080/w/api.php",
query = list(
action = "edit",
title = article_title,
text = article_text,
format = "json"
)
) %>% httr::POST(
body = list(token = tok)
)
}
create_category <- function(category_title, tok = toki) {
httr::modify_url(
"http://127.0.0.1:8080/w/api.php",
query = list(
action = "edit",
title = paste0("Category:", category_title),
text = "",
format = "json"
)
) %>% httr::POST(
body = list(token = tok)
)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment