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

no edit summary
imported>Aisha
(Created page with "== Project == Use the Wordplay API to write the basic parts of a Twitter client. See what your friends are tweeting, get trending topics, search tweets, and more! ==Goals== ...")
 
imported>Aisha
No edit summary
Line 1:
== Project ==
 
# Review relevant material from lecture (e.g. <tt>for</tt> loops)
Use the Wordplay API to write the basic parts of a Twitter client. See what your friends are tweeting, get trending topics, search tweets, and more!
# Go over <tt>words1.py</tt> through <tt>words6.py</tt> as a class
 
# Demo <tt>scrabble_cheater.py</tt>
==Goals==
# Explore other interesting word properties or play with <tt>scrabble_cheater.py</tt> (we've had people start Words With Friends games and cheat using the script)
 
* 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 <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>.
# Find <code>GetSearch</code> 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 <code>GetSearch</code>?
 
<b>Check your understanding</b>: What does <code>api.GetSearch</code> return?
 
 
=== 2. Read through and 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>.
# Find <code>GetTrendsWoeid</code> 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?
 
<b>Check your understanding</b>: What are the differences between the <code>optparse</code> logic for <code>--search</code> and <code>--trending-topics</code>?
You could refer to the <code>optparse</code> module for parsing command-line options: http://docs.python.org/library/optparse.html
 
=== 3. Implement <code>userTweets</code> ===
 
<ol>
<li>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 [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.
 
<pre>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</pre>
 
</li>
<li>We've already written some <code>optparse</code> logic for <code>userTweets</code> in <code>twitter_api.py</code>. Read through that logic. In what ways is it the same/different from the logic for <code>search</code>?
<li>Test your function. You can do this with:
 
<pre>python twitter_api.py -u <username></pre>
 
For example,
 
<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!
</ol>
 
<b>Check your understanding</b>: What are the two ways to pass command line arguments for <code>userTweets</code>?
 
=== 4. Implement <code>trendingTweets</code> ===
 
<ol>
<li>Using the <code>search</code> and <code>trendingTopics</code> functions as a reference, implement <code>trendingTweets</code> in <code>twitter_functions.py</code>.
 
This function should print a couple of recent tweets from each of the currently trending topics.
 
<li>We've already written some <code>optparse</code> logic for <code>trendingTweets</code> in <code>twitter_api.py</code>. Read through that logic. In what ways is it the same/different from the logic for <code>search</code>?
<li>Test your function. You can do this with:
 
<pre>python twitter_api.py -w</pre>
</li>
 
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.
</ol>
 
<b>Check your understanding</b>: What is the purpose of <code>CHICAGO_WOEID</code> in <code>twitter_functions.py</code>?
 
== Bonus exercises ==
 
If you have time, try out some of these extra exercises.
 
=== 1. Customize how tweets are printed by <code>search</code> ===
 
The tweets printed by <code>search</code> could look much nicer and have more useful metadata!
 
Customize how tweets are displayed. Look at the <code>Status</code> and <code>User</code> 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 <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/
 
===Congratulations!===
 
You've read, modified, and added code to a software project that makes it easy to get useful information from Twitterwords. Keep practicing!
 
[[File:Fireworks.png|150px]]
Anonymous user