Hardware Reference
In-Depth Information
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 ile 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
#alternative form of print statement to display contents ;
of a variable
print “Listening to tweets containing the word '%s'.”;
% search_term
while True:
# grab the first tweet containing the ;
search_term
current_status = api.GetSearch(term=search_term,;
count=1)[0]
Search WWH ::




Custom Search