-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgetbib-copy.R
39 lines (38 loc) · 1.81 KB
/
getbib-copy.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
options(stringsAsFactors = F)
require(RPostgreSQL)
drv<-dbDriver("PostgreSQL")
con<-dbConnect(drv, dbname = "vsyverson", host = "localhost", port = "5432", user = "vsyverson")
uniquedocIDs<-unname(unlist(dbGetQuery(con,"SELECT DISTINCT docid FROM specimens;")))
print(paste(length(uniquedocIDs),"docIDs found in specimens table. Fetching bib data..."))
bibpb<-txtProgressBar(min=0,max=length(uniquedocIDs))
dbSendQuery(con,"DROP TABLE IF EXISTS bib;")
bib.line<-jsonlite::fromJSON(paste0("https://geodeepdive.org/api/articles?docid=",uniquedocIDs[1]))$success$data
bib.line["author"]<-paste(unlist(bib.line["author"]),collapse=", ")
bib<-bib.line[c("_gddid","author","title","journal","year")]
for(i in 2:length(uniquedocIDs)){
bib.line<-try(jsonlite::fromJSON(paste0("https://geodeepdive.org/api/articles?docid=",uniquedocIDs[i]))$success$data)
if (!is.null(bib.line)) {
bib.line$author<-paste(unlist(bib.line["author"]),collapse=", ")
bib<-rbind(bib,bib.line[c("_gddid","author","title","journal","year")])
}
if(nrow(bib)%%10==0) setTxtProgressBar(bibpb,nrow(bib))
}
remainingdocIDs<-subset(uniquedocIDs,!uniquedocIDs%in%bib$`_gddid`)
nremaining<-length(remainingdocIDs)
paste(nremaining,"documents left to fetch. Starting second loop...")
while(nremaining>0){
for (i in 1:length(remainingdocIDs)){
bib.line<-try(jsonlite::fromJSON(paste0("https://geodeepdive.org/api/articles?docid=",remainingdocIDs[i]))$success$data)
if (!is.null(bib.line)) {
bib<-rbind(bib,bib.line[c("_gddid","author","title","journal","year")])
}
}
remainingdocIDs<-subset(uniquedocIDs,!uniquedocIDs%in%bib$`_gddid`)
setTxtProgressBar(bibpb,nrow(bib))
if(length(remainingdocIDs)==nremaining) {
print(paste("Exiting with",nremaining,"documents still missing."))
break
}
}
names(bib)[1]<-"gddid"
dbWriteTable(con,"bib",bib)