Clean things up and duplicate some earlier work.

This commit is contained in:
Matt
2018-11-01 22:47:05 -05:00
parent c0aaf8a0ad
commit a6fb224fb1

View File

@ -8,30 +8,32 @@ library(rjson)
library(dplyr) library(dplyr)
library(twitteR) library(twitteR)
library(ini) library(ini)
library(mastodon) library(mastodon) #devtools::install_github('ThomasChln/mastodon')
library(RSQLite) library(RSQLite)
# TODO Generalize to remove gfk_ from these variable names # TODO Generalize to remove gfk_ from these variable names
# Grab city view for Grand Forks # Grab city view for Grand Forks
gfk <- rjson::fromJSON(file="https://www.publicstuff.com/api/2.1/city_view?space_id=15174") space_id <- 15174
client_id <- 1353 #needed later
city <- rjson::fromJSON(file=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
gfk_request_types <- as.data.frame(t(sapply(gfk$response$request_types$request_types, city_request_types <- as.data.frame(t(sapply(city$response$request_types$request_types,
function(x) c(x$request_type$id, x$request_type$name)))) function(x) c(x$request_type$id, x$request_type$name))))
# Add column names # Add column names
names(gfk_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
# Unix timestamp from a week ago # Unix timestamp from a week ago
today <- as.numeric(as.POSIXct(Sys.time())) today <- as.numeric(as.POSIXct(Sys.time()))
week_ago <- today-604800 week_ago <- today-604800
# For all request types, get (at most) 10 requests from the last week from the PublicStuff API. # For all request types, get requests from the last week from the PublicStuff API.
gfk_requests <- lapply(gfk_request_types$request_type_id, recent_requests <- lapply(city_request_types$request_type_id,
function(x) jsonlite::fromJSON(paste0("https://www.publicstuff.com/api/2.1/requests_list?request_type_id=", function(x) jsonlite::fromJSON(paste0("https://www.publicstuff.com/api/2.1/requests_list?request_type_id=",
x,"&after_timestamp=",week_ago,"&limit=10"))) x,"&after_timestamp=",week_ago,"&limit=100")))
# Pull out exactly the data we need # Pull out exactly the data we need
gfk_requests <- lapply(gfk_requests, function(x) x$response$requests$request) recent_requests <- lapply(recent_requests, function(x) x$response$requests$request)
# Drop null list items # Drop null list items
gfk_requests <- Filter(Negate(is.null), gfk_requests) recent_requests <- Filter(Negate(is.null), recent_requests)
# Drop images (in fact, there is image_thumbnail in the data we want, # Drop images (in fact, there is image_thumbnail in the data we want,
# and we can just replace small_ with large_ to get a bigger image later!) # and we can just replace small_ with large_ to get a bigger image later!)
drop_image <- function(x){ drop_image <- function(x){
@ -40,11 +42,13 @@ drop_image <- function(x){
} }
return(x) return(x)
} }
gfk_requests <- lapply(gfk_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
requests <- bind_rows(gfk_requests) recent_requests <- bind_rows(recent_requests)
# Add URL
recent_requests$url <- paste0("https://iframe.publicstuff.com/#?client_id=",client_id,"&request_id=",recent_requests$id)
# Add posted column # Add posted column
requests$posted <- NA recent_requests$posted <- NA
# Sort by date # Sort by date
#gfk_requests <- gfk_requests[order(gfk_requests$date_created),] #gfk_requests <- gfk_requests[order(gfk_requests$date_created),]
@ -56,9 +60,11 @@ if(nrow(dbGetQuery(mydb, "SELECT name FROM sqlite_master WHERE type='table' AND
rows.exist <- dbGetQuery(mydb, 'SELECT id FROM requests')$id rows.exist <- dbGetQuery(mydb, 'SELECT id FROM requests')$id
} 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 <- requests[!requests$id %in% rows.exist,] rows.add <- recent_requests[!recent_requests$id %in% rows.exist,]
# Add the rows # Add the rows
dbWriteTable(mydb, "requests", rows.add,append=T) dbWriteTable(mydb, "requests", rows.add,append=T)
# Get out of the database
dbDisconnect(mydb)
#### Tweeting #### Tweeting
# You now need a developer account to set up an app, which takes some time. # You now need a developer account to set up an app, which takes some time.
@ -88,19 +94,21 @@ auth <- read.ini("auth.ini")
mastodon_token <- login(auth$mastodon$server, auth$mastodon$email, auth$mastodon$password) mastodon_token <- login(auth$mastodon$server, auth$mastodon$email, auth$mastodon$password)
# Each time this script runs, take the oldest n requests, post them, and mark them in the db. # Each time this script runs, take the oldest n requests, post them, and mark them in the db.
requests.new <- dbGetQuery(mydb, 'SELECT * FROM requests WHERE posted IS NULL ORDER BY date_created') mydb <- dbConnect(RSQLite::SQLite(), "requests.sqlite")
new_requests <- dbGetQuery(mydb, 'SELECT * FROM requests WHERE posted IS NULL ORDER BY date_created')
# Set number of posts allowed at once. Will need to adjust according to cron # Set number of posts allowed at once. Will need to adjust according to cron
# schedule and number of posts coming in daily so you don't get behind. # schedule and number of posts coming in daily so you don't get behind.
posts_at_once <- 3 posts_at_once <- 3
for(i in 1:posts_at_once){ for(i in 1:posts_at_once){
request <- requests.new[i,] request <- new_requests[i,]
# Post one selected request # Post one selected request
post_text <- paste0(request$title, " at ", request$address, " (",request$url,"): ", request$description)
if(nchar(request$image_thumbnail) > 1){ if(nchar(request$image_thumbnail) > 1){
download.file(gsub("small","large",request$image_thumbnail), 'temp.jpg', mode="wb") download.file(gsub("small","large",request$image_thumbnail), 'temp.jpg', mode="wb")
post_media(mastodon_token, paste0(request$title, " at ", request$address, ": ", request$description), file = "temp.jpg") post_media(mastodon_token, post_text, file = "temp.jpg")
} else { } else {
post_status(mastodon_token, paste0(request$title, " at ", request$address, ": ", request$description)) post_status(mastodon_token, post_text)
} }
# After tweeting or tooting, mark what has been posted. # After tweeting or tooting, mark what has been posted.