Twitter: Difference between revisions
Content added Content deleted
imported>Jesstess |
imported>Jesstess |
||
Line 33: | Line 33: | ||
* Get experience reading other people's code. |
* Get experience reading other people's code. |
||
==Steps== |
|||
==Suggested exercises== |
|||
=== 1. Understand <code>search</code> === |
|||
# Run <code>python twitter_api.py --search</code> with various search terms, e.g. |
|||
#* <code>python twitter_api.py --search=Python</code> |
|||
#* <code>python twitter_api.py --search="Red Sox"</code> |
|||
# Read through the <code>search</code> function in <code>twitter_functions.py</code>. |
|||
# Trace through the logic in <code>twitter_api.py</code> that turns the <code>--search</code> command line option into a call to <code>search</code>. |
|||
=== 2. Understand <code>trendingTopics</code> === |
|||
# Run <code>python twitter_api.py --trending-topics</code> |
|||
# Read through the <code>trendingTopics</code> function in <code>twitter_functions.py</code>. |
|||
# Trace through the logic in <code>twitter_api.py</code> that turns the <code>--trending-topics</code> command line option into a call to <code>search</code>. |
|||
#* What are the <code>optparse</code> differences between the logic for <code>--search</code> and the logic for <code>--trending-topics</code>? |
|||
=== 3. Implement <code>userTweets</code> === |
|||
# Using the <code>search</code> and <code>trendingTopics</code> functions as a reference, implement <code>userTweets</code> in <code>twitter_functions.py</code>. |
|||
This function should print recent tweets by the username provided on the command line. |
|||
You may find the <code>twitter.Api()</code> function <code>GetUserTimeline()</code> helpful. |
|||
To test this function, at the command line run |
|||
<pre>python twitter_api.py -u <username></pre> |
|||
For example, |
|||
<pre>python twitter_api.py -u bostonpython</pre> |
|||
=== 4. Implement <code>trendingTweets</code> === |
|||
# Using the <code>search</code> and <code>trendingTopics</code> functions as a reference, implement <code>trendingTweets</code> in <code>twitter_functions.py</code>. |
|||
=== Bonus material === |
|||
<ul> |
<ul> |
||
Line 39: | Line 78: | ||
Customize how tweets are displayed. Look at the <code>Status</code> and <code>User</code> classes in the [http://code.google.com/p/python-twitter/source/browse/twitter.py| Twitter code] for inspiration; options include the URL for the tweet, how many followers the sender has, the location of the sender, and if it was a retweet. |
Customize how tweets are displayed. Look at the <code>Status</code> and <code>User</code> classes in the [http://code.google.com/p/python-twitter/source/browse/twitter.py| Twitter code] for inspiration; options include the URL for the tweet, how many followers the sender has, the location of the sender, and if it was a retweet. |
||
</li> |
</li> |
||
<li> |
|||
Write a new function to display tweets from all the trending topics. Add a new command line option for this function. |
|||
</li> |
|||
<li> |
|||
The code to display tweets gets re-used several times. De-duplicate the code by moving it into a function and calling that function instead. Example prototype: |
|||
<pre> |
|||
def print_tweet(tweet): |
|||
""" |
|||
tweet is an instance of twitter.Status. |
|||
""" |
|||
pass |
|||
</pre> |
|||
</li> |
|||
<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. |
<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. |
||
Line 62: | Line 88: | ||
</li> |
</li> |
||
</ul> |
</ul> |
||
[[Boston Python workshop 2/Saturday projects|« Back to the Saturday project page]] |
Revision as of 20:39, 4 July 2012
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.
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.
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.
Steps
1. 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
.
2. 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
.- What are the
optparse
differences between the logic for--search
and the logic for--trending-topics
?
- What are the
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 the twitter.Api()
function GetUserTimeline()
helpful.
To test this function, at the command line run
python twitter_api.py -u <username>
For example,
python twitter_api.py -u bostonpython
4. Implement trendingTweets
- Using the
search
andtrendingTopics
functions as a reference, implementtrendingTweets
intwitter_functions.py
.
Bonus material
-
Customize how tweets are displayed. Look at the
Status
andUser
classes in the Twitter code for inspiration; options include the URL for the tweet, how many followers the sender has, the location of the sender, and if it was a retweet. - [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. 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.