Twitter: Difference between revisions

m
Changed name of tab from "API Keys" to "Keys and Access Tokens" as it is now named
imported>Jesstess
imported>Clarkkarenl
m (Changed name of tab from "API Keys" to "Keys and Access Tokens" as it is now named)
 
(25 intermediate revisions by 6 users not shown)
Line 1:
[[File:Twitter.png|right|300px]]
 
== Project ==
 
Line 14 ⟶ 16:
=== Download and install the <code>python-twitter</code> dependencies and library ===
 
This project has 4 dependencies: <code>httplib2</code>, <code>simplejson</code>, <code>python-oauth2</code>, and <code>python-twitter</code>.
* http://pypi.python.org/pypi/httplib2/
 
* http://pypi.python.org/pypi/simplejson/
Install these as you typically install Python packages. For example, if you use <code>pip</code>, you can use the following <code>pip</code> command:
* http://pypi.python.org/pypi/oauth2/
 
* http://code.google.com/p/python-twitter/
<pre>pip install httplib2 simplejson oauth2 python-twitter</pre>
 
=== Register a "Twitter application" on Twitter.com ===
 
The python-twitter library now only supports oAuth authentication as the Twitter devs have indicated that OAuth is the only method that will be supported moving forward. So you need to register as a Twitter developer. This is fairly easy and free of cost.
 
Registration Process:
* Log in to the Twitter developer site [https://dev.twitter.com/apps/new] with your regular Twitter username and password.
* Create a new Twitter application. For the fields, try these suggestions:
** '''Name''': My sample Twitter app
** '''Description''': My description
** '''Website''': https://openhatch.org/wiki/Twitter
** ''Callback URL''': (leave this blank)
** Now you can click to '''Create your Twitter application'''. You should proceed to the app information page.
 
 
(If you run into problems, take note: The website URL has to start with http or https. You don't need to specify Callback URL.)
 
Now, make the Twitter application able to post to your Twitter profile. To do that:
 
* Start at the app information page. (The '''Details''' tab should be selected.)
* Switch to the '''Permissions''' tab.
* Scroll down to '''Access''' and choose '''Read, and Write'''.
* Click '''Update settings'''.
 
 
Now, you need to permit yourself to use that app. To do that:
 
* Start at the app information page. (Make sure you are on the '''Details''' tab.)
* Switch to the '''Keys and Access Tokens''' tab.
* At the bottom of the page, under the "Your access token" section, there is a button - "Create my access token" , click on this to generate the access_token_key and access_token_secret. Once you get the confirmation that your app's access tokens have been created, refresh the page and you'll see that all your app's credentials will be listed and are to be kept confidential.
* Write down the below credentials Twitter just created for your application, or just keep the browser tab open:
** API key
** API secret
** Access token
** Access token secret
 
Once you have the above mentioned details, you can use the skeleton code shared below.
 
=== Download and un-archive the Twitter project skeleton code ===
 
* httphttps://webopenhatch.mit.eduorg/w/jesstessimages/www4/IntermediatePythonWorkshop45/Twitter.zip
 
Un-archiving will produce a <code>Twitter</code> folder containing 3 python files and 1 config file: <code>twitter_api.py</code>, <code>twitter_functions.py</code>, <code>util.py</code>, and <code>utilconfig.py</code>.
 
Put the consumer_key, consumer_secret, access_token_key, access_token_secret in the <code>config.py</code> .
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 ===
Line 44 ⟶ 86:
# 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 TwitterPython librarywrapper codefor Twitter at httphttps://code.googlegithub.com/pbear/python-twitter/sourceblob/browsemaster/twitter/api.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> ===
Line 54 ⟶ 95:
# 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 TwitterPython librarywrapper codefor Twitter at httphttps://code.googlegithub.com/pbear/python-twitter/sourceblob/browsemaster/twitter/api.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>?
 
<b>Step 2 resources</b>:
<ul>
<li>
The <code>optparse</code> module for parsing command-line options: http://docs.python.org/library/optparse.html</li>
</ul>
 
=== 3. Implement <code>userTweets</code> ===
Line 89 ⟶ 135:
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>userTweetstrendingTweets</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:
 
Line 99 ⟶ 145:
 
<b>Check your understanding</b>: What is the purpose of <code>BOSTON_WOEID</code> in <code>twitter_functions.py</code>?
 
 
== Bonus exercises ==
Line 109 ⟶ 154:
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 httphttps://code.googlegithub.com/pbear/python-twitter/sourceblob/browsemaster/twitter.py for inspiration; options include displaying:
* the sender of the tweet
* the URL for the tweet
Line 117 ⟶ 162:
 
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>BOSTON_WOEID</code> to only display results for Boston.
 
You can look up WOEIDs by location at http://woeid.rosselliot.co.nz/
 
===Congratulations!===
 
You've read, modified, and added code to a software project that makes it easy to get useful information from Twitter. Keep practicing!
 
[[File:Fireworks.png|150px]]
[[File:Balloons.png|150px]]
Anonymous user