Hardware Reference
In-Depth Information
while True:
# grab the first tweet containing the
search_term
current_status = api.GetSearch(term=search_term,
count=1)[0]
# if the status is different then
# pass it to the chicken
if current_status.id != previous_status.id:
#if the result we get from twitter is
#different from what we got last time,
# we know there's a new tweet
print current_status
previous_status = current_status
# wait for a short while before checking again
time.sleep(TIME_DELAY)
if __name__ == “__main__”:
main()
Run the code to test it. Obviously, you need to be connected to the Internet for it to work.
The program should print the latest tweets that contain the word chicken. Press Ctrl + C to
stop the program.
You are nearly ready to add in the code that controls the toy, but before you do, it is worth
looking at how the code works.
By now you should be recognising statements in the program. First you import three mod-
ules that you need. The Time module provides the sleep() function that creates the delay
between each time you check Twitter. The Sys module provides a way to get command-line
arguments. Next constants are defined before the main function begins.
The Twitter module is written in OOP style, so you create an api object with the following
statement:
api = twitter.Api(consumer_key='xxxxxxcxK9I3g',
consumer_secret='xxxxxfLBmh0gqHohRdkEH891B2XCv00',
access_token_key='xxxxx25-
Dw8foMCfNec2Gff72qxxxxxwMwomXYo',
access_token_secret='xxxxxxjIuFb88dI')
Search WWH ::




Custom Search