Anda di halaman 1dari 3

9/11/2014 Using Twitter API with Python | Dark Matter Sheep

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

September 15, 2013



Uncategorized code, python, twitter
My brain never rests. One of the ways I relax is finding solutions to problems. Sometimes thats with
crosswords or old episodes of Poirot on ITV3, sometimes thats with writing code. I particularly like using
Python.
I use Twitter all the time, for finding out all the latest news and political gossip in astronomy. So, I
thought Id have a bash at using Twitter through python. You used to be able to access Twitter just by
using your account username and password, with their basic auth in Twitter API v.1.0. Not any longer.
Version 1.0 is now defunct and to do interesting things with Twitter API you have to use version 1.1
which requires OAuth.
This can be a whole world of pain, so Im writing my findings here to help anyone experiencing similar
discomfort.
1. Find a Twitter library
Various people have written libraries to access twitter (I tried writing my own and it was a thankless
task). I would recommend using the simply named twitter package. Youll also need a python way of
logging in via OAuth, so I would recommend using OAuth2.
They are both on PyPi so you should be able use:
2. Get developer credentials
Youll need a Twitter account to go much further with this tutorial. Once you have that:
1. Log into the Twitter developer site,
2. Create a new app by clicking on your icon in the top left
3. Click My applications in the menu,
4. Click the button Create a new application

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

Anda mungkin juga menyukai