Simple Twitter Bot with Python

on

My friend Noe Rodriguez wanted to make a social experiment. We would take an old mexican movie and cut it second by second and then post it to Twitter. I found it interesting so we divided the workload, I made the bot while he got the frames. This is the resulting code and experiences. You can look at the experiment here @RisaNvacaciones. First I must say this is some pretty simple Twitter Bot. It is some basic code, but I think is a good starting point for more complex bots (or some interesting experiments). If you want to know how to cut a film second by second using ffmpeg go here.

The Twitter Bot

First let’s get the basics installed. If you have a Mac you should already have python installed, if you have windows you can download python 2.7 for the code below. Personally I use ActivePython. It was the first one I found and works really well.

Next we will install pip so we can download the twitter lib we will be using called Twython. Installing pip is quite easy, download the pip file and then run it:


python get-pip.py

After that just run


pip install twython

Now everything we need in our computer for the code below to work is done. But now we need to get the keys from Twitter so we can post in our accounts.

Make a new Twitter APP

First we head to Twitter’s developers page and we sign in. Then we go to our personal APP page and make a new APP.
twitter1

After you have finished your APP registration go to the app page. It should look something like this
twitter2

To get the keys you need for the bot go to the API keys tab and generate the needed keys (API keys and Access tokens). Then add your personal keys and tokens to the code above.

After that you can go and change the APP permissions depending on your needs. To do this go to the Permissions tab in your Twitter APP page. For us, we needed to make it able to post so we changed them to Read and Write.

twitter3

So finally to start the bot you just have to run


python bot.py pasitotuntun.txt

Where pasitotuntun.txt is the name of our text file where we have the song for the flavor text and is completely optional.

And that should be all. If you have any question leave it in the comments.