电视电话蟒蛇一直重新下载照片

2024-10-04 09:22:19 发布

您现在位置:Python中文网/ 问答频道 /正文

过去半个小时我一直在挠头,但我不能纠正这个错误,让我解释一下。 基本上我想做的是做一个简单的脚本,可以下载过去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())

也许问题与异步有关?你知道吗


Tags: fromimport脚本clientapiidmessagedatetime