This project contains a Python script for updating your Twitter banner picture. By deploying this script (and a set of images) to Heroku, and setting a scheduler to run the script once a day, your Twitter banner picture will get automatically updated once a day -- selecting one at random from a set of images you provided.
Clone this repository: in your terminal, type
git clone https://github.com/Datamine/Twitter-Banner-Rotatorand
Go on Heroku, make an account if necessary, and make a new app. Your app has a URL. For example, on your app dashboard, if you hit
open app, it'll take you to a website:
https://example.herokuapp.com. The part between the
.herokuapp.comis your app's name (in this case, that's "example"). It's a unique identifier.
Download the Heroku Toolbelt and log in on the terminal.
In your local repository, type
heroku git:remote -a <your app name here>. See instructions for adding a heroku remote to your git repository.
Back on the Heroku app dashboard, on app addons, add the
scheduler. Set it to schedule
python run.pyonce a day. (Or hourly/every 10 minutes depending on the level of skittish pizzazz that you want.)
Go on Twitter. Make a new app or use one that you've created previously. Obtain:
On the Heroku Dashboard for your app, set four config variables for the credentials obtained above. (It may be worth opening up the Python REPL and using the
requirements.txt, that this app uses the
python-twitterlibrary, not the
pip install python-twitter. If you run into a naming conflict, I suggest you use a virtual environment.)
Download the pictures you like, and put them in the
To deploy your application to heroku, type:
git add . git commit -m "<your commit message here>" git push heroku master
You can then view the logs either on app dashboard, or using
heroku logs --app <your app name here> --tailfrom the command line. Check the logs to make sure everyhing works as desired.
I suggest changing your scheduled task's frequency to every 10 minutes in the start, so you can easily verify that it works, then changing it to daily thereafter.
The following are all quite straight-forwardly possible:
If you want to rotate among lots of images, you don't need to store them all in a
Banners/folder. That can get cumbersome. Instead, you can upload them to an external image host, e.g. imgur, and let some file, e.g.
banners_list.txtbe a list of URLs to your banner images. Then you can use
urllib.urlretrieveto download the image to a temporary file, upload the file to Twitter, and delete the file afterwards.
In line with the suggestion above, you could even randomly retrieve nice images to use as backgrounds. I am reminded of Adam Cadre's stochastic planet project.
Issues & Notes
.giffiles currently fail when you try to upload them. This appears to be a problem in the
python-twitterlibrary. See the issue here. You can correct this in the meantime by using ImageMagic to convert any gifs to jpg.
At first this script was written to cycle through a set of images. However, this relied on the script being able to write to a file to denote the current position in the cycle, which is not possible because Heroku's filesystem is read-only. I have been led to believe that the best alternative is to attach a small database to store that index, but this seems like a cumbersome solution, especially if I'm releasing this as a tool for other people to use. It'd be better to migrate the entire setup to AWS.
Since the script selects a banner from a set of pictures at random, it's currently possible for the newly chosen banner to be the same as the one already in place. This happens on an update with probability 1/n, where n is the number of pictures in the
Bannersdirectory. There are some tricks that could be employed to prevent this, but I've currently deemed this nonessential.
You can also read about this project on my blog.