2016-02-04 42 views
0

我目前正在尝试使用python-twitter和Twitter API,并且制作一个脚本,当用户上传新推文时,会发出声音print "\a"报警。当指定用户创建新推文时,如何让我的Python脚本听起来像一个警报?

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

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

它的工作原理,只要检索和打印最新的tweet每10秒。然而,我怎样才能存储循环最后一次迭代的最新推文(我有无限循环的函数),并将它与当前迭代中的最新推文进行比较?我的想法是,如果这两个不同,那么print "\a"会发声。

回答

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)