A simple Twitter bot that posts random images by Swedish photographer Hilding Mickelsson, using the K-samsök/SOCH API.
This is the code that runs the @hilding_bot Twitter bot. It posts random images by Swedish photographer Hilding Mickelsson, using the K-samsök/SOCH API. Aside from a few parts that are peculiar to that particular collection of photos, the code is otherwise fairly generic and could very easily be adapted to find and post any set of photos from K-samsök/SOCH, simply by changing the CQL search query at the top of hilding_cache.pl
.
The archive of Hilding Mickelsson's photographs is curated by Hälsinglands Museum, who publish them under a CC BY-NC license on DigitaltMuseum, from where they are aggregated to K-samsök/SOCH.
You'll need a modern (≥5.16) version of Perl (I suggest using Perlbrew). The scripts use a few non-core modules which you'll also need to install: run perl -c
on each of the scripts (or just check the use
directives at the top) and install whatever it complains about with cpanm
until it stops complaining. 😊 (Perlbrew lets you easily bootstrap cpanm
with perlbrew install-cpanm
.)
There are two scripts:
-
hilding_bot.pl
is the Twitter bot itself. It fetches RDF data from K-samsök/SOCH and image data from DigitaltMuseum, mungs it a bit to make a suitable tweet text, and posts the image and data to Twitter.
To use it, create a new application in the Twitter Developer site. Copyoauth.yml.example
tooauth.yml
and edit it to add your consumer key and secret.hilding_bot.pl
will prompt you to authenticate the first time it is run on the command line, and save an access token and secret for future authentication.
After the first interactive run for authentication,hilding_bot.pl
is intended to be run as a scheduledcron
job. -
hilding_cache.pl
is a support script that generates and maintains a list of K-samsök/SOCH URIs matching its search criteria inuri-cache.yml
. This lets the bot quickly pick a URI at random while excluding those it has already tweeted (which it saves toused-uris.yml
).hilding_cache.pl
parallelises its search, but even so it can take a few minutes to run if the search results in a large number of hits; I suggest setting a weeklycron
job.
The caches need to be primed beforehilding_bot.pl
is run for the first time: runhilding_cache.pl
and when it's finished, copyuri-cache.yml
toused-uris.yml
; the bot will take care of the book-keeping after that.
If you don't care about potentially posting the same thing twice, you could dispense with hilding_cache.pl
and the YAML caches entirely, and instead adapt hilding_bot.pl
to just pick a URI at random from the K-samsök/SOCH search result at run-time.