Only try posting if there are requests to post.

This commit is contained in:
mburtonkelly
2018-11-02 13:32:28 -05:00
parent ab68c2d488
commit e1952e5dda
2 changed files with 26 additions and 20 deletions

View File

@ -7,6 +7,7 @@
# Run with `Rscript bin/gfk-publicstuff.R` # 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/ # 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(jsonlite)
library(rjson) 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. # Each time this script runs, take the oldest n requests, post them, and mark them in the db.
mydb <- dbConnect(RSQLite::SQLite(), "requests.sqlite") 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 # 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){ # Only post if there are new requests
request <- new_requests[i,] if(nrow(new_requests) > 0){
# Post one selected request # One post per request, up to limit
post_text <- paste0(request$title, " at ", request$address, " (",request$url,"): ", request$description) for(i in 1:posts_at_once){
if(nchar(request$image_thumbnail) > 1){ request <- new_requests[i,]
download.file(gsub("small","large",request$image_thumbnail), 'temp.jpg', mode="wb") # Post one selected request
post_media(mastodon_token, post_text, file = "temp.jpg") post_text <- paste0(request$title, " at ", request$address, " (",request$url,"): ", request$description)
} else { if(nchar(request$image_thumbnail) > 1){
post_status(mastodon_token, post_text) 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. # After tweeting or tooting, mark what has been posted.
# https://cran.r-project.org/web/packages/RSQLite/vignettes/RSQLite.html # https://cran.r-project.org/web/packages/RSQLite/vignettes/RSQLite.html
# https://stackoverflow.com/a/43978368/2152245 # https://stackoverflow.com/a/43978368/2152245
# Update posted column as needed # Update posted column as needed
dbExecute(mydb, "UPDATE requests SET posted = :posted where id = :id", dbExecute(mydb, "UPDATE requests SET posted = :posted where id = :id",
params=data.frame(posted=TRUE, params=data.frame(posted=TRUE,
id=request$id)) id=request$id))
}
# Get out of the database
dbDisconnect(mydb)
} }
# 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.

Binary file not shown.