
Connecting NYPL Digital Collections API to Mastodon API
I have seen many bots on Mastodon and followed some of them – usually some that post regularly, but not too often. Some post photos, others post music.
NYPL Digital Collections Archive
I have been fan of The New York Public Library Digital Collections for long, using some of their historical public domain illustrations for one of my games. They have an extensive archive of digitized historical photos, maps, illustrations, posters, documents etc.
NYPL Picture of the Day on Mastodon
I had an idea. How about I connect to their API to find images related to New York City and post them to Mastodon as a bot? That’s how NYPL bot was born.
It retrieves a random New York related picture from the Digital Collections of The New York Public Library and posts it as a bot to Mastodon for anyone to see.
API Web Development Challenges
Mastodon API is pretty straightforward and now I understand why there are not many feature-complete and/or well-documented PHP libraries for it. You can just easily use Curl as the requests are simple.
NYPL API is more complicated as one needs to understand their data model. It features collections that can have subcollections, containers and items. To complicate things further the items are not final destinations as they are further connected to captures. And finally, captures are what I was looking for – having image links included for download.
Once I understood their model, I created a PHP script that connects to three different endpoints of NYPL API. First it searches for items under a certain topic (NYC), then it chooses random item from total items found and retrieves that, which is immediately followed by fetching a capture with image(s).
Once this is done, one POST request is required to post a status to Mastodon.
If you like New York, go and follow the bot.