Only use one JSON package.
This commit is contained in:
@ -10,7 +10,6 @@
|
|||||||
# Can install in Jessie with sudo apt-get install... but it is 3.1.1 by default.
|
# Can install in Jessie with sudo apt-get install... but it is 3.1.1 by default.
|
||||||
|
|
||||||
library(jsonlite)
|
library(jsonlite)
|
||||||
library(rjson)
|
|
||||||
library(ini)
|
library(ini)
|
||||||
library(mastodon) #devtools::install_github('ThomasChln/mastodon')
|
library(mastodon) #devtools::install_github('ThomasChln/mastodon')
|
||||||
library(RSQLite)
|
library(RSQLite)
|
||||||
@ -26,10 +25,10 @@ client_id <- 1353 #needed later
|
|||||||
|
|
||||||
|
|
||||||
### Get going
|
### Get going
|
||||||
city <- rjson::fromJSON(file=paste0("https://www.publicstuff.com/api/2.1/city_view?space_id=",space_id))
|
city <- jsonlite::fromJSON(txt=paste0("https://www.publicstuff.com/api/2.1/city_view?space_id=",space_id))
|
||||||
## Make a data frame of request_type IDs and names
|
## Make a data frame of request_type IDs and names
|
||||||
city_request_types <- as.data.frame(t(sapply(city$response$request_types$request_types,
|
city_request_types <- city$response$request_types$request_types$request_type[,c("id","name")]
|
||||||
function(x) c(x$request_type$id, x$request_type$name))))
|
|
||||||
# Add column names
|
# Add column names
|
||||||
names(city_request_types) <- c("request_type_id","request_type_name")
|
names(city_request_types) <- c("request_type_id","request_type_name")
|
||||||
# Loop through request types and get n most recent in each category
|
# Loop through request types and get n most recent in each category
|
||||||
@ -53,7 +52,6 @@ drop_image <- function(x){
|
|||||||
}
|
}
|
||||||
recent_requests <- lapply(recent_requests, drop_image)
|
recent_requests <- lapply(recent_requests, drop_image)
|
||||||
# Put the requests together in a data frame
|
# Put the requests together in a data frame
|
||||||
#recent_requests <- bind_rows(recent_requests)
|
|
||||||
recent_requests <- do.call("rbind",recent_requests)
|
recent_requests <- do.call("rbind",recent_requests)
|
||||||
# Add URL
|
# Add URL
|
||||||
recent_requests$url <- paste0("https://iframe.publicstuff.com/#?client_id=",client_id,"&request_id=",recent_requests$id)
|
recent_requests$url <- paste0("https://iframe.publicstuff.com/#?client_id=",client_id,"&request_id=",recent_requests$id)
|
||||||
@ -68,11 +66,12 @@ mydb <- dbConnect(RSQLite::SQLite(), "requests.sqlite")
|
|||||||
# See if table exists, then get existing rows back
|
# See if table exists, then get existing rows back
|
||||||
if(nrow(dbGetQuery(mydb, "SELECT name FROM sqlite_master WHERE type='table' AND name='requests'")) > 0){
|
if(nrow(dbGetQuery(mydb, "SELECT name FROM sqlite_master WHERE type='table' AND name='requests'")) > 0){
|
||||||
rows.exist <- dbGetQuery(mydb, 'SELECT id FROM requests')$id
|
rows.exist <- dbGetQuery(mydb, 'SELECT id FROM requests')$id
|
||||||
|
col_names <- names(dbGetQuery(mydb, 'SELECT * FROM requests'))
|
||||||
} else rows.exist <- NA
|
} else rows.exist <- NA
|
||||||
# Only add rows that don't exist, by request ID
|
# Only add rows that don't exist, by request ID
|
||||||
rows.add <- recent_requests[!recent_requests$id %in% rows.exist,]
|
rows.add <- recent_requests[!recent_requests$id %in% rows.exist,]
|
||||||
# Add the rows
|
# Add the rows (rarrange to be in right order)
|
||||||
dbWriteTable(mydb, "requests", rows.add,append=T)
|
dbWriteTable(mydb, "requests", rows.add[,col_names],append=T)
|
||||||
# Get out of the database
|
# Get out of the database
|
||||||
dbDisconnect(mydb)
|
dbDisconnect(mydb)
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user