since_ID 在 mentions_timeline returns 最近的推文中 - 不确定问题是否出在 Python 或 Twitter API

since_ID in mentions_timeline returns most recent tweet - not sure if issue is with Python or Twitter API

我对 Python 很陌生,对 Twitter API 也很陌生,不确定我在这里搞砸了什么(这使得研究解决方案变得困难)。

我制作了一个简单的 Twitter 机器人,它可以查询 mentions_timeline,并使用预先确定的回复列表之一回复 at-replies(对于表情符号提前抱歉)。问题是每次它 运行s 它都会响应所有最近的回复,无论它们是否已经被回复。

我试图通过将机器人回复的最新提及 ID 写入文件并在下一次将该值传递给 mentions_timeline 的 since_ID 来解决这个问题脚本是 运行。据我所知,机器人应该只看到它没有回复的推文,而是看到最近的回复,包括那些它已经回复的。

无论我是否在 运行 查询之前将提及 ID 增加 1,我都会得到相同的结果 - 我认为 since_ID 可能包含在内,但它似乎没有什么区别。本质上 since_ID 似乎对查询 returns 的影响为零。

我不确定该怎么做 - 我不知道问题是否在于我将值传递给 Twitter API 的方式,或者我的代码是否有错误。任何帮助将不胜感激!

这是有问题的脚本:

### find the last mention ID ###
fp=open("lastid.py","r")
last_id_replied = fp.read()

### increment by 1 (or not?!) ###
# last_id_replied = (int(last_id_replied) + 1)
# print(last_id_replied)   

### iterate through mentions ###
mentions = api.mentions_timeline(count=1, since_ID=last_id_replied)
print (mentions)
for mention in mentions:
  fp=open("lastid.py","w")
  fp.write(str(mention.id))

  ### choose a heart ###
  atheart = randrange(0,3)
  if atheart == 0:
    atheartprint = ''
  if atheart == 1:
    atheartprint = ''
  if atheart == 2:
    atheartprint = ''
  if atheart == 3:
    atheartprint = ''

  ### post the tweet ###
  sn = mention.user.screen_name
  m = ("@%s " + atheartprint) % (sn)
  s = api.update_status(m, mention.id)
  print(last_id_replied)
  fp=open("lastid.py","w")
  fp.write(str(mention.id))
  print("done messaging people!")  

我认为,通过完全更改用于获取推文的方法来解决 - 我使用光标来遍历它们,并且它似乎可以正常工作。我真的不明白为什么没有光标它就不能工作。

query = '@name'
max_tweets = 20
recent_mentions = [status for status in tweepy.Cursor(api.mentions_timeline, q=query, since_id=last_id_replied).items(max_tweets)]
print recent_mentions