OEIS Twitterbot based on Raspberry Pi

I was actively finding fascinating afternoon-projects (as defibrillators) since the beginning of this semester, until I came across the @MoMARobot on twitter. This is a script robot spidering artworks from https://www.moma.org/  and then post them to twitter on a 6-hour basis. With the tasty Raspberry Pi 3 in my hand, I felt the urge to create my own twitter bot (on my own desk!) posting random entries from my all-time favorite website: https://oeis.org/.

The Online Encyclopedia of Integer Sequences

OEIS is an online database of integer sequences, e.g. A000796: {3, 1, 4, 1, 5, 9, 2,...}. The main purpose is to allow mathematicians or other scientists to find out if some sequence that turns up in their research has ever been seen before. Despite the fact that my love of this site is kind of an inner geek thing, it does generate a huge amount of interesting academical uses https://oeis.org/wiki/Works_Citing_OEIS.

Ever wondered the number of distinct n -carbon alkanes? No problem. Just use your high school chemistry to brutal force it for small n 's and dump it to OEIS, it will return you

A000602: Number of n-node unrooted quartic trees; number of n-carbon alkanes C(n)H(2n+2) ignoring stereoisomers.

1, 1, 1, 1, 2, 3, 5, 9, 18, 35, 75, 159, 355, 802, 1858, 4347, 10359, 24894, 60523, 148284, 366319, 910726, 2278658, 5731580, 14490245, 36797588, 93839412, 240215803, 617105614, 1590507121, 4111846763, 10660307791, 27711253769

Look at it. It's just beautiful! Cannot wait to have an hourly feed of it on twitter.

Another good news I forgot to mention is OEIS offers json data interchanging now! Do

url = "https://oeis.org/search?q="+ name +"&fmt=json"
j = requests.get(url).json()

you will get everything about a sequence!


As every major social network, twitter offers well designed APIs for status updating. Since I am a science student who cannot code properly :^, I use python package tweepy. This package contains various utilities you need to deploy a twitterbot. Simply authenticate by

auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token, access_token_secret)
api = tweepy.API(auth)

and post using the api handle by

api.update_status(status = text)

Here the consumer_key, consumer_secret, access_token and access_token_secret can be obtained from the application page of you twitter account.

Schedule a Periodic Task on Raspberry Pi

Although we have got all the ingredients needed for tweeting one post, a while True loop relentlessly tweeting posts will blacklist your IP on the twitter side. However, on unix-like systems, one can schedule periodic tasks using cron. To add a cron task, execute

pi@raspberrypi:~ $ crontab -e

then use your favorite text editor to add one new line in the file

0 * * * * \abs\path\to\your\python \abs\path\to\your\twitterbot.py args

The 0 * * * * here is the cron schedule expression standing for "every hour at 0 minute". You can get assistance setting this up for your need at https://crontab.guru/.

Enjoy the Twitterbot

Yummy! The Raspberry Pi is tweeting. Follow my implementation here and get your hourly dose of integer sequences!

Leave a Reply

Your email address will not be published. Required fields are marked *