I wanted to be able to interact with my twitter account(s) using Python scripts. I’d heard there was a Python library called tweepy that does a lot of the Applications Programming Interface (API) work for you. I’d also seen quite a few posts in the Python section of the Raspberry Pi Forums, where people had problems with it.
I thought I’d take a look, spurred on by the idea that I’d like to be able to select a random twitter follower every once-in-a-while for a prize draw.
So, last week, I spent a morning on this. The biggest problem I had was that, following the instructions for basic authentication “it just didn’t work”. I knew that twitter had changed its API over the summer because the twitter widget on the RasPi.TV blog stopped working. I had to go and created my own widget on twitter to make it work again. Could it be related to that?
You can’t use tweepy without OAuth authentication
It certainly could and it certainly was. In fact, the problem was that the tweepy documentation was hopelessly out of date. This basic authentication is irrevocably broken and will never work again.
But you know how it is. People who like programming, often don’t like documenting. And that leaves people like you and I up the proverbial creek without a paddle. We can either give up, or try and see if other people are having the same issues and how they solved it. A second round of googling was required.
I eventually found a blog that got me through the process (although sadly I forgot to bookmark it and can no longer find it). In fact, it’s rather easy – when you know how! And I’m going to share it here with you, so you don’t have to suffer the same wasted couple of hours I had to.
Create an ‘App’
The first thing you have to do is create an app for the twitter API. Sounds scary, but it’s not. Just go to https://apps.twitter.com/ and log in with your twitter account id and password.
Once logged in, hover over your avatar and click My Applications, then click Create a new Application
Then you’ll have to complete four fields in the form. Name, Description, website and callback URL. The last two don’t much matter for what we’re doing here.
Then you have to tick the box agreeing to their terms and then complete the CAPTCHA. Then click Create Your Twitter Application.
Find Your App
Hover over your avatar and click on “My Applications”.
You should now see a page with all your created twitter apps. Click on your new app.
You should then see something like this…
It creates a “Read only” app by default. You can change that later if you want (by clicking the OAuth tab) but we’ll leave it as is for now because we only need read-only access for this program.
Further down the page it looks like this…
Create Access Token
In order for your program to talk to twitter, it needs a way of authenticating. The system used is called OAuth, and it uses access tokens (codes). So you need to click the “Create My Access token” button…
…to make this happen. Soon after that, you should have an additional section at the bottom of the page (you might need to refresh the page (F5) to see it)…
Keep this page open. You will need to copy these codes into your program.
Before you can use tweepy, you need to get it onto your Pi. Fortunately, it’s a Python extension, so easy to install.
sudo apt-get update updates Raspbian package lists
sudo apt-get install python-dev python-pip install pip and dev (You may not need python-dev for pip and tweepy, but I installed it anyway.)
Update Jan 2015: I’ve just tried to install tweepy and it failed. The reason appears to be an older version of pip in the Raspbian repo. You can get round this with…
sudo pip install -U pip (this will uninstall pip and replace it with a version that works).
…before you install tweepy.
sudo pip install tweepy install tweepy itself
You now have all you need to make your Python script talk to twitter.
All you need now is a Python script
Here’s the script I wrote to choose a random subscriber from my list of twitter followers.
To make this script work on your system, you’ll need to edit lines 7-10 to include your keys, secrets and tokens.
#!/usr/bin/env python2.7 # twitterwin.py by Alex Eames http://raspi.tv/?p=5281 import tweepy import random # Consumer keys and access tokens, used for OAuth consumer_key = 'copy your consumer key here' consumer_secret = 'copy your consumer secret here' access_token = 'copy your access token here' access_token_secret = 'copy your access token secret here' # OAuth process, using the keys and tokens auth = tweepy.OAuthHandler(consumer_key, consumer_secret) auth.set_access_token(access_token, access_token_secret) # Creation of the actual interface, using authentication api = tweepy.API(auth) follow2 = api.followers_ids() # gives a list of followers ids print "you have %d followers" % len(follow2) show_list = str(raw_input("Do you want to list the followers array?")) if show_list == ('y' or 'yes' or 'Y' or 'Yes' or 'YES'): print follow2 def pick_winner(): random_number = random.randint(0, len(follow2)-1) winner = api.get_user(follow2[random_number]) print winner.screen_name, random_number while True: pick = raw_input("Press Enter to pick a winner, Q to quit.") if pick == ('q' or 'Q' or 'quit' or 'QUIT' or 'Quit'): break pick_winner()
I called this program twitterwin.py because I wrote it to pick a winner from my twitter subscribers list.
To run the program, just type
This is what the output looks like. I chose “No” when it asked if I want to see the list of subscribers, as it’s not very pretty or useful, except for debugging. :)
Apologies to the abovementioned twitter members. This was just a test, not a real draw.
In part 2, I’ll show you how to use your Python program “app” to send a tweet.
What about the Twitter Draw?
I’ll be announcing this in due course, but for now, make sure you follow @RasPiTV to be in with a chance of winning something. :) That’s all I’m saying about that for now.