0

I am currently experimenting with python-twitter along with the Twitter API, and am making a script that sounds the print "\a" alarm when a user uploads a new tweet.

So far I have this function:

def tweets():
    statuses = api.GetUserTimeline('username')
    tweets = [s.text for s in statuses]
    latest = tweets[0]
    prev = tweets[1]
    time.sleep(10)
    print latest

It works so far as retrieving and printing the latest tweet every 10 seconds. However, how can I store the latest tweet from the last iteration of the loop (I have the function looping indefinitely) and compare it to the latest tweet in the current iteration? My idea is, that if these two are different, then print "\a" will sound.

Ryan Werner
  • 177
  • 3
  • 5
  • 13

1 Answers1

0

This should accomplish the general idea:

latest = None
errors = False

while not errors:
    try:
        statuses = api.GetUserTimeline('username')
        tweets = [s.text for s in statuses]
    except Exception:  # handle specific exception(s) here
        errors = True
        continue

    if latest and latest != tweets[0]:
        print "\a"  # sound
        latest = tweets[0]  # set new latest tweet for next iteration

    time.sleep(10)
Bob Dylan
  • 1,773
  • 2
  • 16
  • 27