From e1952e5dda1946aa19357f949d2b11649b615e03 Mon Sep 17 00:00:00 2001 From: mburtonkelly Date: Fri, 2 Nov 2018 13:32:28 -0500 Subject: [PATCH] Only try posting if there are requests to post. --- bin/gfk-publicstuff.R | 46 ++++++++++++++++++++++++------------------ requests.sqlite | Bin 16384 -> 16384 bytes 2 files changed, 26 insertions(+), 20 deletions(-) diff --git a/bin/gfk-publicstuff.R b/bin/gfk-publicstuff.R index 725d135..91963ec 100644 --- a/bin/gfk-publicstuff.R +++ b/bin/gfk-publicstuff.R @@ -7,6 +7,7 @@ # Run with `Rscript bin/gfk-publicstuff.R` # Raspberry Pi: https://www.r-bloggers.com/how-to-install-the-latest-version-of-r-statistics-on-your-raspberry-pi/ +# Can install with sudo apt-get install... but it is 3.1.1 by default. library(jsonlite) library(rjson) @@ -79,33 +80,38 @@ mastodon_token <- login(auth$mastodon$server, auth$mastodon$email, auth$mastodon # Each time this script runs, take the oldest n requests, post them, and mark them in the db. mydb <- dbConnect(RSQLite::SQLite(), "requests.sqlite") -new_requests <- dbGetQuery(mydb, 'SELECT * FROM requests WHERE posted IS NULL ORDER BY date_created') +#all_requests <- dbGetQuery(mydb, 'SELECT * FROM requests') +new_requests <- dbGetQuery(mydb, 'SELECT * FROM requests WHERE posted <> 1 ORDER BY date_created') # 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. posts_at_once <- 3 -for(i in 1:posts_at_once){ - request <- new_requests[i,] - # Post one selected request - post_text <- paste0(request$title, " at ", request$address, " (",request$url,"): ", request$description) - if(nchar(request$image_thumbnail) > 1){ - download.file(gsub("small","large",request$image_thumbnail), 'temp.jpg', mode="wb") - post_media(mastodon_token, post_text, file = "temp.jpg") - } else { - post_status(mastodon_token, post_text) - } +# Only post if there are new requests +if(nrow(new_requests) > 0){ + # One post per request, up to limit + for(i in 1:posts_at_once){ + request <- new_requests[i,] + # Post one selected request + post_text <- paste0(request$title, " at ", request$address, " (",request$url,"): ", request$description) + if(nchar(request$image_thumbnail) > 1){ + download.file(gsub("small","large",request$image_thumbnail), 'temp.jpg', mode="wb") + post_media(mastodon_token, post_text, file = "temp.jpg") + } else { + post_status(mastodon_token, post_text) + } - # After tweeting or tooting, mark what has been posted. - # https://cran.r-project.org/web/packages/RSQLite/vignettes/RSQLite.html - # https://stackoverflow.com/a/43978368/2152245 + # After tweeting or tooting, mark what has been posted. + # https://cran.r-project.org/web/packages/RSQLite/vignettes/RSQLite.html + # https://stackoverflow.com/a/43978368/2152245 - # Update posted column as needed - dbExecute(mydb, "UPDATE requests SET posted = :posted where id = :id", - params=data.frame(posted=TRUE, - id=request$id)) + # Update posted column as needed + 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) } -# Get out of the database -dbDisconnect(mydb) #### Tweeting # You now need a developer account to set up an app, which takes some time. diff --git a/requests.sqlite b/requests.sqlite index c0788a4a8b6f7ffe7486a8217f3d5406dd5c1382..4a5bcdbd823d162bab9a1ba9b06cfad1fddc2812 100644 GIT binary patch delta 150 zcmZo@U~Fh$oFL68I8nx#QE+3zDS4Tu1ZGL*MpM&9O?h=?aTX2^4t91R!LQyZC_4F% zbg(pYBf}SFUhPI*c}rdfggkGfAphh!!Z#SXCieW_2`kT*F}_B@cIsdrrv08bVq+W-In delta 43 zcmZo@U~Fh$oFL7}GEv5vk!54TDfx}H$&8zO1sRz)_o