Twitter: Difference between revisions

From OpenHatch wiki
Content added Content deleted
imported>Jesstess
m (Protected "Twitter" ([edit=autoconfirmed] (indefinite) [move=autoconfirmed] (indefinite)))
imported>Jesstess
Line 3: Line 3:
==Setup==
==Setup==


=== Download and install the <code>python-twitter</code> dependencies and library ===
See the [http://openhatch.org/wiki/Boston_Python_Workshop_3/Friday Friday setup instructions].

* 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 ===

* http://web.mit.edu/jesstess/www/BostonPythonWorkshop6/Twitter.zip

Un-archiving will produce a <code>Twitter</code> folder containing 3 python files: <code>twitter_api.py</code>, <code>twitter_functions.py</code>, and <code>util.py</code>.

=== Test your installation ===

From a command prompt, navigate to the <code>Twitter</code> directory and run

<pre>
python twitter_api.py --search=python
</pre>

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.


==Goals==
==Goals==

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

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.

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.

Suggested exercises

  • 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.
  • Write a new function to display tweets from all the trending topics. Add a new command line option for this function.
  • 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:
      def print_tweet(tweet):
          """
          tweet is an instance of twitter.Status.
          """
          pass
    
  • [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.

« Back to the Saturday project page