From 3ab537b3a93dd62f3cd19c729313e4974a16e945 Mon Sep 17 00:00:00 2001 From: Matt Date: Thu, 1 Nov 2018 23:44:53 -0500 Subject: [PATCH] Rearrange and stop deleting the database after every run. --- bin/gfk-publicstuff.R | 62 ++++++++++++++++++++---------------------- requests.sqlite | Bin 0 -> 16384 bytes 2 files changed, 29 insertions(+), 33 deletions(-) create mode 100644 requests.sqlite diff --git a/bin/gfk-publicstuff.R b/bin/gfk-publicstuff.R index 6a7529b..0d37b49 100644 --- a/bin/gfk-publicstuff.R +++ b/bin/gfk-publicstuff.R @@ -3,6 +3,9 @@ # Note that the API version at https://www.publicstuff.com/developers#!/API is v2.0, # but this only includes requests up to a certain date. Use v2.1 for recent requests. +# Run from one machine, otherwise you'll get duplicate databases that have differing posted items. +# Run with `Rscript bin/gfk-publicstuff.R` + library(jsonlite) library(rjson) library(dplyr) @@ -11,11 +14,15 @@ library(ini) library(mastodon) #devtools::install_github('ThomasChln/mastodon') library(RSQLite) -# TODO Generalize to remove gfk_ from these variable names - +### Config +# Authentication variables +auth <- read.ini("auth.ini") # Grab city view for Grand Forks space_id <- 15174 client_id <- 1353 #needed later + + +### Get going 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 city_request_types <- as.data.frame(t(sapply(city$response$request_types$request_types, @@ -34,8 +41,7 @@ recent_requests <- lapply(city_request_types$request_type_id, recent_requests <- lapply(recent_requests, function(x) x$response$requests$request) # Drop null list items recent_requests <- Filter(Negate(is.null), recent_requests) -# 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!) +# Image data is in a sub-dataframe, which we don't need drop_image <- function(x){ if(class(x$primary_attachment) == "data.frame") { x$primary_attachment <- NULL @@ -47,10 +53,8 @@ recent_requests <- lapply(recent_requests, drop_image) 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 (to include in database table) recent_requests$posted <- NA -# Sort by date -#gfk_requests <- gfk_requests[order(gfk_requests$date_created),] ## Store requests in a database # Create DB if it doesn't exist, otherwise connect @@ -66,31 +70,9 @@ dbWriteTable(mydb, "requests", rows.add,append=T) # Get out of the database dbDisconnect(mydb) -#### Tweeting -# You now need a developer account to set up an app, which takes some time. -# Do that here: https://developer.twitter.com/en/apply-for-access.html -# A workaround could be to set up a Mastodon account and then auto-tweet -# using the fantastic https://crossposter.masto.donte.com.br/. - -# Read authentication values from ini file -# Don't commit real values to git! -auth <- read.ini("auth.ini") - -# setup_twitter_oauth(consumer_key = auth$twitter$consumer_key, -# access_token = auth$twitter$access_token, -# consumer_secret = auth$twitter$consumer_secret, -# access_secret = auth$twitter$access_secret) - -# https://rcrastinate.blogspot.com/2018/05/send-tweets-from-r-very-short.html - - - - #### Tooting # https://shkspr.mobi/blog/2018/08/easy-guide-to-building-mastodon-bots/ -# Might be able to use this natively: https://github.com/ThomasChln/mastodon - -auth <- read.ini("auth.ini") +# https://github.com/ThomasChln/mastodon 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. @@ -119,10 +101,24 @@ for(i in 1:posts_at_once){ dbExecute(mydb, "UPDATE requests SET posted = :posted where id = :id", params=data.frame(posted=TRUE, id=request$id)) - } - # Get out of the database dbDisconnect(mydb) -unlink("requests.sqlite") + +#### Tweeting +# You now need a developer account to set up an app, which takes some time. +# Do that here: https://developer.twitter.com/en/apply-for-access.html +# A workaround could be to set up a Mastodon account and then auto-tweet +# using the fantastic https://crossposter.masto.donte.com.br/. + +# Read authentication values from ini file +# Don't commit real values to git! +auth <- read.ini("auth.ini") + +# setup_twitter_oauth(consumer_key = auth$twitter$consumer_key, +# access_token = auth$twitter$access_token, +# consumer_secret = auth$twitter$consumer_secret, +# access_secret = auth$twitter$access_secret) + +# https://rcrastinate.blogspot.com/2018/05/send-tweets-from-r-very-short.html diff --git a/requests.sqlite b/requests.sqlite new file mode 100644 index 0000000000000000000000000000000000000000..c0788a4a8b6f7ffe7486a8217f3d5406dd5c1382 GIT binary patch literal 16384 zcmeHOU2Ggz6<)_qV<$MFNHi_9+8a`lrg7GuU9Z~6y=p-|#fBDB%%_YM1xPfpBEve}7!)01qWy?cS(vYD}kz*}Hbhi50t zlg9__cEoF9-uOvWiFpt%u-VBIv-XGyOei|GJ)vC{)J-7cj=kD&ljx2e-1C&soqIy* z@-=7O7t~$pU2%18#JsBn-qy^}l}T*oT^U6pHn?u*?3>4`G7M!ysMkHHlX_h$bJfw1 zn*|1>rme9d8MV5VXrbntS6y>Wd*t=!Vh^>@zH*j|$BP{Z(ZJ44)ropt>P`eU@7@T% z>)LjSysu~5rM{o_{&3S%*KF@G-76bbx<3DoamxRXiL9L3vc0>1uC{Z}uI)X&y}g?@ z;m@}Im1{GH8{eC7-6TrjSBMZMtL$VFFAgxHxb_E_R7@q6N}&C_?d}>^6-3?uPprqqDGw|#*XksHe-|!<|8q_7V@xxh|$H-{E##E z!lf6+%YS|On@_y-`wJ)b)zr!Rzv1}C)b(wHgF#jCNaX5CB@A3`l4>;vHy+%1ryB-{ ztNFmY!zmOCA8a`=Z5N&4{BSY9@~O?&clYmEIg;(a%4pHuoJ<(he8Y|;X4V%>+xfBP zzCbfxk%?h>Rsj(#YUasC%&JO8Y$k~6p)@(h?oPB}8pC`7&fp?tsp}%7OM(+zpY8V( z<@!0MlS&jASbmlLg}LXy|Im2(??hFB4^~6YTeeQp>H?Wxx$^GV5EUU=h%c2gzhq{NjlF+d|(ysE;esQz%Ow(Ng_Ig z0ONKsbo-8&Ej&rx$)*eorG#%{O9jZOc0sTNVX#Pdz$XesJ59#JusI6@N4~s3PH`yD zSbh{PJh3F$%-plz`sH}}mFhJ=xh@ND-0}D)AHCH%E^+gHiMOt(%scowckQMdx>t&O z&hE(W*}1)^ugyhSTBE&t+Bfi=5OgzM1Y;v(Mg9*ZqdwNhu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