-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdata_dump.R
87 lines (62 loc) · 2.28 KB
/
data_dump.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
suppressPackageStartupMessages({
library(httr)
library(jsonlite)
library(purrr)
library(dplyr)
library(googledrive)
library(readr)
})
# get command line secret inputs
args <- commandArgs(trailingOnly = TRUE)
# 1st: credentials for Google Drive
credentials <- args[[1]]
# State file path on google drive (stores the hooks):
state_file_path <- args[[2]]
# Dump file path on google drive:
file_path <- args[[3]]
# Sciwheel authentication token:
f1000auth <- args[[4]]
# Load the state from Drive
# authenticate to Google drive
drive_auth(path = rawToChar(base64enc::base64decode(credentials)), email = "[email protected]" )
# read the file from google drive
webhooks <- drive_read_string(file = state_file_path) %>% read_csv()
get_page <- function(projectId, page, f1000auth) {
resp <- GET(
paste0("https://sciwheel.com/extapi/work/references?projectId=", projectId, "&sort=addedDate:desc&page=", page),
add_headers(Authorization = paste("Bearer", f1000auth))
)
if (resp$status_code == 200) {
project.content <- content(resp)
project.notes <- project.content$results %>% map(function(r) {
GET(
paste0("https://sciwheel.com/extapi/work/references/", r$id, "/notes?"),
add_headers(Authorization = paste("Bearer", f1000auth))
) %>% content()
})
names(project.notes) <- project.content$results %>% map_chr(~ .x$id)
list(content = project.content, notes = project.notes)
} else {
NULL
}
}
get_pages <- function(projectId, f1000auth, page = 1) {
page.results <- get_page(projectId, page, f1000auth)
if (page >= page.results$content$totalPages) {
list(content = page.results$content$results, notes = page.results$notes)
} else {
next.page.results <- get_pages(projectId, f1000auth, page + 1)
list(
content = c(page.results$content$results, next.page.results$content),
notes = c(page.results$notes, next.page.results$notes)
)
}
}
distinct.webhooks <- webhooks %>% distinct(projectId, .keep_all = TRUE)
dump <- distinct.webhooks %>% pmap(function(...) {
current <- data.frame(...)
get_pages(current$projectId, f1000auth)
})
names(dump) <- distinct.webhooks$channel
saveRDS(dump, "./data_dump.rds")
drive_update(file = file_path, media = "./data_dump.rds")