An updated post for Python 3 is now available at: http://ow.ly/PH0rX.
During the end of last week I was set the challenge of implementing a Twitter feed in Python to run on the Raspberry Pi, and would be used to display Tweets for the University of Chester's Open Day. I didn't know the Twitter API, Python or the Raspberry Pi - this post covers how I did it, and the source code is available at the bottom of this post. Here is the final result;
The basics steps to implement this feed are;
- Add your application to your Twitter Developer account
- Use OAuth to make a request
- Get the Tweets from the Twitter's Search API
- Use the Tkinter GUI to draw the Tweets to the Canvas
- Deploy to the Raspberry Pi
1. Add your application to your Twitter Developer account
2. Use OAuth to make an authenticated request
In Python, OAuth2 requests can be made using the OAuth2 module. If this needs to be installed, I recommend installing PIP - a package manager for Python - and then using PIP to install OAuth;
pip install oauth2
Once OAuth is installed, you can use the following function to make a request;
import oauth2 as oauth def Request(url, key, secret, http_method = 'GET', post_body = '', http_headers = ''): consumer = oauth.Consumer(key = '<your consumer key>', secret = '<your consumer secret>') token = oauth.Token(key = key, secret = secret) client = oauth.Client(consumer, token) request = client.request( url, method = http_method, body = post_body, headers = http_headers) return request
3. Get the Tweets from the Twitter's Search API
Twitter has a search API that returns a JSON result of relevant Tweets matching the specified query. However, bear in mind that you can make up to 180 requests every 15 minutes and the query will only return Tweets from the last 7 days or so. Queries are made by calling https://api.twitter.com/1.1/search/tweets.json?q= and passing a URL-encoded search query. You can also perform complex queries with the keywords AND and OR, and you can specify keywords to exclude by using "-keyword".
Here is an example of a query for '#informaticstips' using the function outlined above;
import urllib encodedQuery = urllib.quote_plus('#informaticstips') request, response = Request( 'https://api.twitter.com/1.1/search/tweets.json?q=' + encodedQuery, '<your api key>', '<your api secret>')
Once you have obtained the result you can also use Python's JSON module to parse the result - which will give you access to the tweets, which are called 'statuses' in Twitter;
import json data = json.loads(response) tweets = data['statuses']
4. Use the Tkinter GUI to draw the Tweets to the Canvas
Tkinter is Python's de-facto standard GUI (Graphical User Interface) package. It also contains a default set of widgets for common controls such as labels and radio buttons. In this case, I made use of the Canvas widget to draw the Tweets to the screen, which gave more control over the style I needed.
The Twitter feed is implemented in fullscreen mode - to do this, use the following code;
import tkinter root = Tk() screenWidth = root.winfo_screenwidth() screenHeight = root.winfo_screenheight() root.overrideredirect(1) root.geometry('%dx%d+0+0' % (screenWidth, screenHeight)) root.configure(background = '#000000') #black canvas = Canvas( background = '#000000', #black borderwidth = -5, height = 500, relief= 'flat', width = 500) canvas.pack(expand = 1, fill = BOTH) root.mainloop()
5. Deploy to the Raspberry Pi
Once the Twitter feed was completed it took about 12 hours (and many OS re-installs) to get the feed working with the Raspberry Pi. The root problem is that the Raspberry Pi comes pre-installed with Python 3 and the feed (and it's modules) make use of Python 2.7. So, please uninstall Python 3 and install Python 2.7 if you need to.
If you need the Python Imaging Library (PIL) - I used it to display a background image - make sure you install the prerequisites first! This includes libjpeg to read any Jpeg files and Tkinter for your GUI. Also, use PIP wherever possible to install Python packages and use a good package manager to install OS packages, as these generally install any dependencies for you.
Download the source code GitHub
*You must register your own app to your twitter developer account, and then edit the fields in "start.py".
*This Twitter feed will also check your blocked users, and any Tweets from them are ignored.