简体   繁体   English

Twitter 机器人不在文本文件中保存推文 ID

[英]Twitter bot not saving tweet IDs in text file

I am trying to make a Twitter bot that will reply to tweet automatically but I do not want it to reply to tweets it has already replied to so I am trying to have the tweet IDs saved to a text document and have the bot reference it for the last seen ID but it is not updating the text document.我正在尝试制作一个 Twitter 机器人,它会自动回复推文,但我不希望它回复它已经回复的推文,所以我试图将推文 ID 保存到文本文档中,并让机器人引用它最后看到的 ID,但它没有更新文本文档。

FILE_NAME = 'last_seen.txt'

def read_last_seen(FILE_NAME):
    file_read = open(FILE_NAME, 'r')
    last_seen_id = int(file_read.read().strip())
    file_read.close()
    return last_seen_id

def store_last_seen(FILE_NAME, last_seen_id):
    file_write = open(FILE_NAME, 'w')
    file_write.write(str(last_seen_id))
    file_write.close()
    return
tweets = api.mentions_timeline(read_last_seen(FILE_NAME), tweet_mode='extended')


for tweet in reversed(tweets):

    if '#botting' in tweet.full_text.lower():
        print(str(tweet.id) + ' - ' + tweet.full_text)
        api.update_status('@' + tweet.user.screen_name + ' This is a bot.', tweet.id)
        store_last_seen(FILE_NAME, tweet.id)

When I simplified the tweets for testing purposes, the program just overwrrote the previous entries.当我出于测试目的简化推文时,该程序只是覆盖了以前的条目。 Repl here在这里回复

So your writing program is not advancing past previous entries.所以你的写作计划并没有超越以前的条目。 I think you want "a" mode and not "w."我认为您想要“a”模式而不是“w”。 You also want to probably put new lines or a break character in between entries so when scanning for a match you don't get false matches.您还可能希望在条目之间放置新行或换行符,以便在扫描匹配项时不会得到错误匹配项。

Python write to file without overwriting current txt Python写入文件而不覆盖当前的txt

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM