Twitter: Difference between revisions

From OpenHatch wiki
Content added Content deleted
imported>Jesstess
No edit summary
imported>Jesstess
No edit summary
Line 10: Line 10:
* Get experience reading other people's code.
* Get experience reading other people's code.


==Setup==
==Project setup==


=== Download and install the <code>python-twitter</code> dependencies and library ===
=== Download and install the <code>python-twitter</code> dependencies and library ===
Line 35: Line 35:
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.
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.


==Steps==
==Project steps==


=== 1. Understand <code>search</code> ===
=== 1. Read through and understand <code>search</code> ===


# 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.
Line 45: Line 45:
# 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>.


=== 2. Understand <code>trendingTopics</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>
# Run <code>python twitter_api.py --trending-topics</code>
# Read through the <code>trendingTopics</code> function in <code>twitter_functions.py</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>.
# 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>?
<b>Check your understanding</b>: What are the differences between the <code>optparse</code> logic for <code>--search</code> and <code>--trending-topics</code>?



=== 3. Implement <code>userTweets</code> ===
=== 3. Implement <code>userTweets</code> ===

Revision as of 20:45, 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

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

  1. 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"
  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.

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.

Check your understanding: What are the differences between the optparse logic for --search and --trending-topics?


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.

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

  1. Using the search and trendingTopics functions as a reference, implement trendingTweets in twitter_functions.py.


Bonus material

  • Customize how tweets are displayed. Look at the Status and User 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.