http://darkmattersheep.net/2013/09/using-twitter-api-with-python/ 1/3
Dark Matter Sheep
Some ramblings of a musical astronomer
Using Twitter API with Python
1
2
pip install twitter
pip install oauth2
9/11/2014 Using Twitter API with Python | Dark Matter Sheep
http://darkmattersheep.net/2013/09/using-twitter-api-with-python/ 2/3
5. Fill out all the details, sign your life away and click Create you Twitter application
You should now be redirected to a page with all the information about your new app, including OAuth
settings. This page has the 4 horsemen of the OAuth apocalypse:
Consumer Key,
Consumer secret,
Access token (often called OAuth token)
Access secret (or OAuth token secret) these last 2 in the lower section
3. Put it together
You are now ready to have a play with Twitter from python. Open up a python shell:
Substitute the 4 horsemen into those capitalised variables and hopefully that wont give you any
trouble.
Now you can take Twitter for a spin. There are loads of interesting things you can do listed on the
Twitter REST API v1.1 help pages.
I really wanted to have a look at various #hashtags without being rate limited. The way you do this is by
using the Twitter Stream and not the Twitter Search. This lets you enter hashtags, words or phrases and
then sit on the stream of tweets happening which contain the words you are tracking.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
~ ipython
Python 2.7.1 |CUSTOM| (r271:86832, Dec 3 2010, 15:56:20)
Type "copyright", "credits" or "license" for more information.
IPython 0.13.2 -- An enhanced Interactive Python.
? -> Introduction and overview of IPython's features.
%quickref -> Quick reference.
help -> Python's own help system.
object? -> Details about 'object', use 'object??' for extra details.
In [1]: from twitter import *
In [2]: t = Twitter(
auth=OAuth(OAUTH_TOKEN, OAUTH_SECRET,
CONSUMER_KEY, CONSUMER_SECRET)
)
In [3]:
1
2
3
4
5
6
7
8
9
from twitter import TwitterStream
config = { --- add your 4 horsemen in this dictionary --- }
ts = TwitterStream(
auth=OAuth(
config['oauth_token'],
config['oauth_token_secret'],
config['key'],
config['secret']
9/11/2014 Using Twitter API with Python | Dark Matter Sheep
http://darkmattersheep.net/2013/09/using-twitter-api-with-python/ 3/3
Ive popped my 4 horsemen into a dictionary called config.
Once you open a stream, tracking certain words, it will carry on forever. For this reason I stop the for
loop which checks the stream with a timeout, in the variable stop (a datetime object).
Tip: You cant look backwards in time in a Twitter stream, only forwards in real-time. If you want to look
back in the Twitter feeds youll have to use search.
Thats my recent findings. There are so many more things you can do with the Twitter API, but that is just
a taste.
10
11
12
13
14
15
16
17
)
)
openstream = ts.statuses.filter(track=words)
for item in openstream:
print item['user']['screen_name'], datetime.strptime(item['created_at'
if datetime.now() > stop:
print datetime.now().isoformat()
break