0

我目前正在试验 python-twitter 和 Twitter API,并正在制作一个脚本,print "\a"当用户上传新推文时会发出警报。

到目前为止,我有这个功能:

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

它的工作原理是每 10 秒检索和打印最新的推文。但是,如何存储循环的最后一次迭代中的最新推文(我的函数无限循环)并将其与当前迭代中的最新推文进行比较?我的想法是,如果这两个不同,那么print "\a"听起来。

4

1 回答 1

0

这应该完成一般的想法:

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)
于 2016-02-04T18:09:58.077 回答