Chicago Python Workshop/Chicago Python Workshop 1/Saturday projects/Twitter: Difference between revisions

From OpenHatch wiki
Content added Content deleted
 
(8 intermediate revisions by 4 users not shown)
Line 19: Line 19:
# Run <code>python twitter_api.py --search</code> with various search terms, e.g.
# 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=Python</code>
#* <code>python twitter_api.py --search="Red Sox"</code>
#* <code>python twitter_api.py --search="White Sox"</code>
#* <code>python twitter_api.py --search="Cubs"</code>
# Read through the <code>search</code> function in <code>twitter_functions.py</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>.
# 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>.
Line 25: Line 26:


<b>Check your understanding</b>: What does <code>api.GetSearch</code> return?
<b>Check your understanding</b>: What does <code>api.GetSearch</code> return?



=== 2. Read through and understand <code>trendingTopics</code> ===
=== 2. Read through and understand <code>trendingTopics</code> ===
Line 44: Line 44:
This function should print recent tweets by the username provided on the command line.
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.
You may find the <code>twitter.Api()</code> function [http://code.google.com/p/python-twitter/source/browse/twitter.py#2628|<code>GetUserTimeline()</code>] helpful.


Currently, the code in <code>twitter_functions.py</code> is empty, with only a comment, and a <code>pass</code> statement that allows us to define this function without actually doing anything.
Currently, the code in <code>twitter_functions.py</code> is empty, with only a comment, and a <code>pass</code> statement that allows us to define this function without actually doing anything.


<nowiki>def userTweets(username):
<pre>def userTweets(username):
"""
"""
Print recent tweets by `username`.
Print recent tweets by `username`.
Line 59: Line 59:
python twitter_api.py -u bostonpython
python twitter_api.py -u bostonpython
"""
"""
pass</nowiki>
pass</pre>


</li>
</li>
Line 69: Line 69:
For example,
For example,


<pre>python twitter_api.py -u bostonpython</pre></li>
<pre>python twitter_api.py -u Orbitz</pre>
or,
<pre>python twitter_api.py -u zoro_tools</pre>
</li>


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!
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!
Line 92: Line 95:
</ol>
</ol>


<b>Check your understanding</b>: What is the purpose of <code>BOSTON_WOEID</code> in <code>twitter_functions.py</code>?
<b>Check your understanding</b>: What is the purpose of <code>CHICAGO_WOEID</code> in <code>twitter_functions.py</code>?


== Bonus exercises ==
== Bonus exercises ==
Line 113: Line 116:
=== 2. Print trending topics by location ===
=== 2. Print trending topics by location ===


Extend <code>trendingTopics</code> so that a Yahoo! Where On Earth ID (WOEID) can be specified on the command line and trending topics for that location will be displayed instead of using the hardcoded <code>BOSTON_WOEID</code> to only display results for Boston.
Extend <code>trendingTopics</code> so that a Yahoo! Where On Earth ID (WOEID) can be specified on the command line and trending topics for that location will be displayed instead of using the hardcoded <code>CHICAGO_WOEID</code> to only display results for Chicago.


You can look up WOEIDs by location at http://sigizmund.info/woeidinfo/
You can look up WOEIDs by location at http://sigizmund.info/woeidinfo/



===Congratulations!===
===Congratulations!===

Latest revision as of 19:15, 8 December 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 steps

1. Read through and understand search

  1. Run python twitter_api.py --search with various search terms, e.g.
    • python twitter_api.py --search=Python
    • python twitter_api.py --search="White Sox"
    • python twitter_api.py --search="Cubs"
  2. Read through the search function in twitter_functions.py.
  3. Trace through the logic in twitter_api.py that turns the --search command line option into a call to search.
  4. 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 to GetSearch?

Check your understanding: What does api.GetSearch return?

2. Read through and understand trendingTopics

  1. Run python twitter_api.py --trending-topics
  2. Read through the trendingTopics function in twitter_functions.py.
  3. Trace through the logic in twitter_api.py that turns the --trending-topics command line option into a call to search.
  4. 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? You could refer to the optparse module for parsing command-line options: http://docs.python.org/library/optparse.html

3. Implement userTweets

  1. Using the search and trendingTopics functions as a reference, implement userTweets in twitter_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. Currently, the code in twitter_functions.py is empty, with only a comment, and a pass statement that allows us to define this function without actually doing anything.
    def userTweets(username):
        """
        Print recent tweets by `username`.
    
        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
        """
        pass
  2. We've already written some optparse logic for userTweets in twitter_api.py. Read through that logic. In what ways is it the same/different from the logic for search?
  3. Test your function. You can do this with:
    python twitter_api.py -u <username>

    For example,

    python twitter_api.py -u Orbitz

    or,

    python twitter_api.py -u zoro_tools
  4. 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

  1. Using the search and trendingTopics functions as a reference, implement trendingTweets in twitter_functions.py. This function should print a couple of recent tweets from each of the currently trending topics.
  2. We've already written some optparse logic for trendingTweets in twitter_api.py. Read through that logic. In what ways is it the same/different from the logic for search?
  3. Test your function. You can do this with:
    python twitter_api.py -w
  4. 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 CHICAGO_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!

2. Print trending topics by location

Extend trendingTopics so that a Yahoo! Where On Earth ID (WOEID) can be specified on the command line and trending topics for that location will be displayed instead of using the hardcoded CHICAGO_WOEID to only display results for Chicago.

You can look up WOEIDs by location at http://sigizmund.info/woeidinfo/

Congratulations!

You've read, modified, and added code to a software project that makes it easy to get useful information from Twitter. Keep practicing!