<< return


- Lui Crowie -

Pi Twitter Bot


I wanted to set myself a challenge and after a random shower thought that is when a Pi calculation bot was born! I wanted to do it in a new language so chose Python as I have little experience with that. Also, i'm a little wacky, I like doing things just to learn.

What is it?

A Python self-contained application that uses a reduced polynomial version of the Bailey–Borwein–Plouffe (BPP) formula that calculates the Nth digit of Pi and then tweets it to the @moredigitsofpi account!

I have this deployed as a docker container within my K8s cluster. It uses OAuth and the Twitter API v1.1 as well as it dyncamically works out what Nth digit to calculate next by the count of tweets on the account meaning no database, no store or variable to keep track; if it dies (pod scale = 1) or there is an error with the Twitter API it just continues.

What I did?

Firstly I had to work out how to work out the Nth digit of Pi, this involved a bit of research and a handful of experiments, initially I started with caculating the whole Pi value and then take the Nth digit but that would prove a lot of hassle when we get into the 1000's of decimal places. Next I stumbled upon the BPP formula, and then the polynomial version... BOOM, process to calcuate sorted!

Next, I then experimented with the Twitter API using Postman to understand what and how I can do the POST requests, the fields and headers etc. Then I just coded, back and forth from the Python documents and after multiple revisions, I then had the final product which is still going strong!

What did you learn?

  • A handful of Python knowledge
  • Use of OAuth and the Request module in Python
  • Twitter API
  • Twitter sometimes has a "catchup" process so the API is a little out of sync, I handled this with catching the HTTP response code and if you tweet the exact same thing twice, Twitter will reject it.