Twitter: Difference between revisions
imported>Jesstess |
imported>Jesstess No edit summary |
||
Line 105: | Line 105: | ||
If you have time, try out some of these extra exercises. |
If you have time, try out some of these extra exercises. |
||
=== 1. Customize how tweets are printed by <code>search</code> === |
|||
<ol> |
|||
<li> |
|||
⚫ | Customize how tweets are displayed. Look at the <code>Status</code> and <code>User</code> classes in |
||
</li> |
|||
The tweets printed by <code>search</code> could look much nicer and have more useful metadata! |
|||
<li>[Long] A lot of the Twitter API requires that you be authenticated. Examples of actions that require authentication include: posting new tweets, getting a user's followers, getting private tweets from your friends, and following new people. |
|||
⚫ | |||
Set up oAuth so you can make authenticated requests. |
|||
* the sender of the tweet |
|||
* the URL for the tweet |
|||
* how many followers the sender has |
|||
* the location of the sender |
|||
* if it was a retweet |
|||
and more! |
|||
http://dev.twitter.com/pages/auth describe how Twitter uses oAuth. |
|||
http://code.google.com/p/python-twitter/ has examples of using oAuth authentication to make authenticated Twitter API requests. |
|||
</li> |
|||
</ul> |
Revision as of 21:03, 4 July 2012
Project
Use the Twitter API to write the basic parts of a Twitter client. See what your friends are tweeting, get trending topics, search tweets, and more!
Goals
- Have fun playing with data from Twitter.
- See how easy it is to programmatically gather data from social websites that have APIs.
- Get experience with command line option parsing and passing data to a Python script.
- Get experience reading other people's code.
Project setup
Download and install the python-twitter
dependencies and library
- http://pypi.python.org/pypi/httplib2/
- http://pypi.python.org/pypi/simplejson/
- http://pypi.python.org/pypi/oauth2/
- http://code.google.com/p/python-twitter/
Download and un-archive the Twitter project skeleton code
Un-archiving will produce a Twitter
folder containing 3 python files: twitter_api.py
, twitter_functions.py
, and util.py
.
Test your installation
From a command prompt, navigate to the Twitter
directory and run
python twitter_api.py --search=python
You should see the text from 20 tweets containing the word "Python" printed to the screen. If you don't, let a staff member know so you can debug this together.
Project steps
1. Read through and understand search
- Run
python twitter_api.py --search
with various search terms, e.g.python twitter_api.py --search=Python
python twitter_api.py --search="Red Sox"
- Read through the
search
function intwitter_functions.py
. - Trace through the logic in
twitter_api.py
that turns the--search
command line option into a call tosearch
. - Find
GetSearch
in the Twitter library code at http://code.google.com/p/python-twitter/source/browse/twitter.py. What other options could we have passed toGetSearch
?
Check your understanding: What does api.GetSearch
return?
2. Read through and understand trendingTopics
- Run
python twitter_api.py --trending-topics
- Read through the
trendingTopics
function intwitter_functions.py
. - Trace through the logic in
twitter_api.py
that turns the--trending-topics
command line option into a call tosearch
. - Find
GetTrendsWoeid
in the Twitter library code at http://code.google.com/p/python-twitter/source/browse/twitter.py. How many trending topics does that method return?
Check your understanding: What are the differences between the optparse
logic for --search
and --trending-topics
?
3. Implement userTweets
- Using the
search
andtrendingTopics
functions as a reference, implementuserTweets
intwitter_functions.py
. This function should print recent tweets by the username provided on the command line. You may find thetwitter.Api()
functionGetUserTimeline()
helpful. - We've already written some
optparse
logic foruserTweets
intwitter_api.py
. Read through that logic. In what ways is it the same/different from the logic forsearch
? - Test your function. You can do this with:
python twitter_api.py -u <username>
For example,
python twitter_api.py -u bostonpython
You should see 20 tweets by the provided username. If you have a Twitter account, try running it on yourself, tweeting something new, and running it again!
Check your understanding: What are the two ways to pass command line arguments for userTweets
?
4. Implement trendingTweets
- Using the
search
andtrendingTopics
functions as a reference, implementtrendingTweets
intwitter_functions.py
. This function should print a couple of recent tweets from each of the currently trending topics. - We've already written some
optparse
logic foruserTweets
intwitter_api.py
. Read through that logic. In what ways is it the same/different from the logic forsearch
? - Test your function. You can do this with:
python twitter_api.py -w
You should see recent tweets from all of the currently trending topics. Depending on how you implemented your function, you might see a chunk of tweets from each trending topic in turn, or tweets from each topic interleaved.
Check your understanding: What is the purpose of BOSTON_WOEID
in twitter_functions.py
?
Bonus exercises
If you have time, try out some of these extra exercises.
1. Customize how tweets are printed by search
The tweets printed by search
could look much nicer and have more useful metadata!
Customize how tweets are displayed. Look at the Status
and User
classes in http://code.google.com/p/python-twitter/source/browse/twitter.py for inspiration; options include displaying:
- the sender of the tweet
- the URL for the tweet
- how many followers the sender has
- the location of the sender
- if it was a retweet
and more!