Hardware Reference
In-Depth Information
Upon success, scroll down and click Create My Access Token. Wait up to a minute and reload
the page.
Make a note of the Consumer Key and Consumer Secret entries from the OAuth section, and
Access Token and Access Token Secret from the Your Access Token section because you will
need to include these in your program.
Writing Code to Talk to Twitter
With the Python modules installed, it is time to write the code that will talk to Twitter.
Create a new file named twitter_tag_listen.py , containing the following code:
#!/usr/bin/env python
#twitter_tag_listen.py
#listens for new tweets containing a search term
import time
import sys
import twitter
DEFAULT_SEARCH_TERM = “chicken” #what we search twitter for
TIME_DELAY = 30 # seconds between each status check
def main():
#replace values for consumer_key, consumer_secret,
access_token_key and access_token_secret with the values
you made a note of from the Twitter website.
api = twitter.Api(consumer_key='xxxxxxcxK9I3g', ↵
consumer_secret='xxxxxfLBmh0gqHohRdkEH891B2XCv00', ↵
access_token_key='xxxxx25-
Dw8foMCfNec2Gff72qxxxxxwMwomXYo',
access_token_secret='xxxxxxjIuFb88dI')
previous_status = twitter . Status()
# has user passed command line argument?
if len(sys.argv) > 1:
search_term = sys.argv[1]
else:
search_term = DEFAULT_SEARCH_TERM
print “Listening to tweets containing the word '%s'.”
% search_term
Search WWH ::




Custom Search