Telethon python不断重新下载照片

时间:2019-11-16 19:05:41

标签: python telegram telethon

过去半小时我一直在挠头,但我无法解决此错误,请让我解释一下。 基本上,我想做的是制作一个简单的脚本,该脚本可以下载最近6天的文字和图片,该脚本在所有内容上都可以正常运行,但当我重新运行该脚本时,它将再次下载以重复的图像。

这是代码:

from datetime import timedelta
from datetime import datetime
from telethon import TelegramClient


date = datetime.now() + timedelta(-6)
api_id =  123
api_hash = 'XXX'
client = TelegramClient('SESSION', api_id, api_hash)

#Blank files have been already created
async def main():
    with open("downloadedids.txt", "r+") as archivio, open("messagesaved.txt", "r+") as scaricare:
        async for message in client.iter_messages('test', offset_date=date, reverse=True): ##I want to download only messages 6 days ago to now
            if str(message.id) not in str(archivio.read()):
                if (message.photo):
                    await message.download_media("Include")
                scaricare.write(str(message.text))
                archivio.write(str(message.id) + "\n")


with client:
    client.loop.run_until_complete(main())

也许问题与异步有关?

1 个答案:

答案 0 :(得分:0)

正在重新下载它,因为您正在告诉它!也许您想对现有文件进行校验和,如果匹配,就不要保留第二个校验和?