Hardware Reference
In-Depth Information
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.
he 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. he
Time
module provides the
sleep()
function that creates the delay
between each time you check Twitter. he
Sys
module provides a way to get command-line
arguments. Next constants are deined before the main function begins.
he Twitter module is written in OOP style, so you create a Twitter
api
object with the fol-
lowing statement:
api = twitter.Api(consumer_key='xxxxxxcxK9I3g',;
consumer_secret='xxxxxfLBmh0gqHohRdkEH891B2XCv00', ;
access_token_key='xxxxx25-Dw8foMCfNec2Gff72qxxxxxwMwomXYo',;
access_token_secret='xxxxxxjIuFb88dI')
You check if any command-line arguments were used by checking the length (that is, the
number of items) in
sys.argv
. If there's at least one, then you set the
search_term
to be
the irst argument:
# has user passed command line argument?
if len(sys.argv) > 1: