Not quite perfect… A Lil’ Automatic Twitter Thingy
20-12-2012 I am informed by the time-stamp is the day I first wrote what I guess I should call a Twitter, Bot(?) A Python script to update my profile image daily. Why? Well, doesn’t it get a little boring looking at the same thing every day?
Simple as that.
The script worked in concert with PythonAnywhere.com with its free hosted Python environment and scheduled execution of Python tasks; 00:05UTC time every day, my script executes and an image is generated by selecting colours randomly from a simple palette painting them onto a canvas.
The shape it paints is a five-bit binary number between one and 31, see what I did there? The days of the month! So, for example, if you take the number 29 in binary, you can represent it with five bits, 11101.
Flip it on it’s side into a matrix and you can generate a pattern like follows:
11111 11111 11111 00000 11111
Turn the ones into colours and the zeros black and you can create some neat little ever-changing images:
These images are created from the original cut of the code that ran from 20 December 2012 to 29 November 2014.
But it didn’t work perfectly! Say what..?!
A single hope was that I’d get a perfect 365 days of images generated and that would create a nice package of material to look at. Who knows what else… my ambitions extended to creating a GIF. The rest I’d be sure figure out…
But it didn’t happen! From the beginning, my code didn’t change – everything changed around it. That is to say, sometime around April 2013 I noticed images weren’t being created on my host, and therefore, my Twitter profile wasn’t being updated. Similarly, I noticed it again in July that same year.
The cause was infrastructure changes at PythonAnywhere.com, although I can’t remember if it was related to the Python Imaging Library, or the Twitter API Library I was using at the time but once noticed, if it wasn’t soon enough, the continuity in image generation is broken.
In both cases, before I could find the time to write workarounds to the issues (remember, it no longer took priority) the code started working again once my host had finished their infrastructure work.
The reason for this blog was a change very specifically to the Twitter API Library used by PythonAnywhere.com which left the code broken from around May this year and such that it remained so, and looked unlikely to resolve itself. The API library was updated on the server and left my own code completely incompatible.
It came down to a single change: https://github.com/exponential-decay/binary-numbers/blob/master/binary-numbers.py#L49
_base64=True
That parameter is all that was needed now to make the code work. What the API had changed was the assumption that image data would be base64 encoded when it arrived at the function to be encoded into a URL, for the call to the Twitter API itself.
This is a subtle change but not one without a large impact – if the assumption is now that images aren’t encoded those that were originally when using the API are encoded a second time which means that Twitter can no longer identify the stream as an image file.
The way the code is distributed and the way in that it fails silently makes this difficult to debug, and requires digging around in the distribution packages within the Python install, e.g. for me: /usr/local/lib/python2.7/dist-packages/twitter/api.py
This all made it very awkward, but a few hours at the weekend and it was finally resolved after months of having a static profile picture on Twitter.
Out of adversity…
From a small amount of pain trying to debug something I’m quite fond of, I decided it was time to re-engineer the work. With the fix complete I reworked the project to make it a little more sophisticated.
In June 2014 National Library New Zealand (NLNZ) released a blog on Heritage Colour Palettes. Sets of colour palettes inspired by heritage objects in the national library; primarily the colours we see on the pages, or bindings of books.
First New Zealand Christmases: Tasman 1642, Cook 1769, Marsden 1814, 1933. Record page.
Palettes were an issue I had when first creating the code. All the potential colours, all the potential configurations, I didn’t really know how to approach it so I just chose primary colours. This first set of NLNZ palettes gave me a discrete number of quite attractive options intersecting a number of my interests. I knew that I wanted to try them for this purpose from the day they were released – if only everything had been working!
The second change was to the layout of the image. While I still create a binary representation of the number representing the day of the month, I have created a much more sophisticated/complex stripe pattern which will mutate depending on how colours are pulled from any of the numbers of palettes selected on any given day. The way the pattern will begin to work is partially in homage to Birgit Bachler’s Big Data Wallpapers from her Copy Wildly series which I saw first-hand in Wellington. The pattern could potentially find itself enlarged, and on display in a domestic setting.
I have stabilised the Twitter API library code on the server by uploading my own static copy and the routines I am using have been made a bit more flexible for expanding more in future. The first two images have even already been published!
Let’s see what lovely things we can create with colour and lines this time around.
Additional Notes:
Code: https://github.com/exponential-decay/binary-numbers
Active Images (Updated Daily): http://exponentialdecay.pythonanywhere.com/
Archived Images: https://github.com/exponential-decay/binary-numbers/tree/master/binary-numbers-original-images
Heritage colour palettes on Facebook: https://www.facebook.com/media/set/?set=a.263596350499066.1073741881.141682096023826
Addendum:
A friend pointed out similarities between the results of my new approach and Cory Arcangel’s work at the Tate Modern: http://www.tate.org.uk/whats-on/tate-modern/display/cory-arcangel #lines
(This post was edited 2017-10-08 to make reference to Birgit Bachler’s work which had been forgotten in a previous version of the text.)
4 thoughts on “Not quite perfect… A Lil’ Automatic Twitter Thingy”