Skip to content

Commit 289d067

Browse files
committed
setup of page creation functions
1 parent fe976bd commit 289d067

File tree

8 files changed

+230
-6
lines changed

8 files changed

+230
-6
lines changed

NAMESPACE

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
# Generated by roxygen2: do not edit by hand
22

33
export(categories_in_page)
4+
export(create_pages)
45
export(login)
56
export(page_backlinks)
67
export(page_content)

R/create_content.R

Lines changed: 137 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,137 @@
1+
#' request token for api action as signed in user
2+
#'
3+
#' helper function to request a user action token
4+
#'
5+
#' @param url a URL body
6+
#'
7+
#' @return a token string
8+
#'
9+
#' @importFrom magrittr "%>%"
10+
#'
11+
get_action_token <- function(url) {
12+
13+
# get login token
14+
response <- httr::modify_url(
15+
url,
16+
query = list(
17+
action = "query",
18+
meta = "tokens",
19+
format = "json"
20+
)
21+
) %>% httr::GET(
22+
) %>% httr::stop_for_status(
23+
)
24+
25+
# parse the response, check for API errors
26+
parsed_response <- response_parse(
27+
response = response, out_class = "actiontoken"
28+
)
29+
if(!is.null(parsed_response$error)){
30+
stop(
31+
"The API returned an error: ",
32+
parsed_response$error$code,
33+
" - ", parsed_response$error$info
34+
)
35+
}
36+
37+
parse_response(parsed_response) %>%
38+
return()
39+
}
40+
41+
#' wikimedia api page creation
42+
#'
43+
#' Create pages or category-pages on a wikimedia instance.
44+
#'
45+
#' @param url a URL body
46+
#' @param p_title vector with page title strings of new pages
47+
#' @param p_text vector with page content strings of new pages
48+
#' @param category switch to decide, if the pages should be
49+
#' created as category-pages
50+
#'
51+
#' @return TRUE
52+
#'
53+
#' @export
54+
create_pages <- function(url, p_title, p_text, category = FALSE){
55+
56+
if(length(p_title) != length(p_text)) {
57+
stop(
58+
"The length of the vectors p_title and p_text
59+
is not equal."
60+
)
61+
}
62+
63+
# get action token
64+
token <- NA
65+
token <- get_action_token(url)
66+
if(token %>% is.na){
67+
stop("Problems with the action token request.")
68+
}
69+
70+
# create page(s) (with progress bar)
71+
pb <- utils::txtProgressBar(
72+
min = 0, max = length(p_title), style = 3
73+
)
74+
75+
res <- c()
76+
for (i in 1:length(p_title)) {
77+
res[i] <- create_page(
78+
url, p_title[i], p_text[i], category, token
79+
)
80+
utils::setTxtProgressBar(pb, i)
81+
}
82+
83+
close(pb)
84+
85+
all(res) %>%
86+
return()
87+
}
88+
89+
#' wikimedia api page creation (single pages)
90+
#'
91+
#' helper function to do the actual api requests for page
92+
#' and category-page creation
93+
#'
94+
#' @param url a URL body
95+
#' @param p_title page title string of new page
96+
#' @param p_text page content string of new page
97+
#' @param category switch to decide, if the page should be
98+
#' created as category-page
99+
#' @param token action token to perform the request
100+
#'
101+
#' @return TRUE
102+
#'
103+
create_page <- function(url, p_title, p_text, category, token){
104+
105+
# create page
106+
response <- httr::modify_url(
107+
url,
108+
query = list(
109+
action = "edit",
110+
title = ifelse(
111+
category,
112+
paste0("Category:", p_title),
113+
p_title
114+
),
115+
text = p_text,
116+
format = "json"
117+
)
118+
) %>% httr::POST(
119+
body = list(token = token)
120+
) %>% httr::stop_for_status(
121+
)
122+
123+
# parse the response, check for API errors
124+
parsed_response <- response_parse(
125+
response = response, out_class = "createpage"
126+
)
127+
if(!is.null(parsed_response$error)){
128+
stop(
129+
"The API returned an error: ",
130+
parsed_response$error$code,
131+
" - ", parsed_response$error$info
132+
)
133+
}
134+
135+
parse_response(parsed_response) %>%
136+
return()
137+
}

R/login.R

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
#' request token to start client login
22
#'
3-
#' helper function to request a user login token within
4-
#' \code{client\_login}
3+
#' helper function to request a user login token
54
#'
65
#' @param url a URL body
76
#' @param user a username of a registered user
@@ -26,7 +25,7 @@ get_prelogin_token <- function(url, user) {
2625

2726
# parse the response, check for API errors
2827
parsed_response <- response_parse(
29-
response = response, out_class = "token"
28+
response = response, out_class = "prelogintoken"
3029
)
3130
if(!is.null(parsed_response$error)){
3231
stop(
@@ -76,6 +75,7 @@ login <- function(url, user, pw){
7675
logintoken = token,
7776
password = pw
7877
)
78+
) %>% httr::stop_for_status(
7979
)
8080

8181
# parse the response, check for API errors

R/parse.R

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,12 +86,22 @@ parse_response.pageinfo <- function(x){
8686
return(results)
8787
}
8888

89-
parse_response.token <- function(x){
89+
parse_response.prelogintoken <- function(x){
9090
x <- x$login$token
9191
return(x)
9292
}
9393

9494
parse_response.login <- function(x){
9595
x <- x$clientlogin$status == "PASS"
9696
return(x)
97+
}
98+
99+
parse_response.actiontoken <- function(x){
100+
x <- x$query$tokens$csrftoken
101+
return(x)
102+
}
103+
104+
parse_response.createpage <- function(x){
105+
x <- x$edit$result == "Success"
106+
return(x)
97107
}

man/create_page.Rd

Lines changed: 32 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/create_pages.Rd

Lines changed: 28 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/get_action_token.Rd

Lines changed: 17 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/get_prelogin_token.Rd

Lines changed: 1 addition & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)