knadh / tg-archive

A tool for exporting Telegram group chats into static websites like mailing list archives.
MIT License
829 stars 121 forks source link

[Feature Enhancement/Takeout] Auto delete invalid session.session files when starting new instance. #107

Closed shinji257 closed 11 months ago

shinji257 commented 1 year ago

When running tg-archive using Takeout mode if you run the program again while a session.session file already exists it will try to use it. If the last session start was recent enough it can reuse it but most of the time it seems to become invalid. When that happens the application dumps and produces a message instructing to delete the file and try again. Can we have the program catch this error, delete the file, then start the process of starting a new session? Of course it should still alert the user that an existing session was found but no longer valid before doing so probably by still throwing the error itself without the dump and handling it gracefully.

2023-07-29 08:05:19,122: takeout invalidated. delete the session.session file and try again.
Traceback (most recent call last):
  File "/debian/.local/bin/tg-archive", line 8, in <module>
    sys.exit(main())
  File "/debian/.local/lib/python3.9/site-packages/tgarchive/__init__.py", line 141, in main
    s = Sync(cfg, args.session, DB(args.data))
  File "/debian/.local/lib/python3.9/site-packages/tgarchive/sync.py", line 29, in __init__
    self.client = self.new_client(session_file, config)
  File "/debian/.local/lib/python3.9/site-packages/tgarchive/sync.py", line 123, in new_client
    takeout_client.get_messages("me")
  File "/debian/.local/lib/python3.9/site-packages/telethon/sync.py", line 39, in syncified
    return loop.run_until_complete(coro)
  File "/usr/lib/python3.9/asyncio/base_events.py", line 642, in run_until_complete
    return future.result()
  File "/debian/.local/lib/python3.9/site-packages/telethon/client/messages.py", line 586, in get_messages
    return await it.collect()
  File "/debian/.local/lib/python3.9/site-packages/telethon/requestiter.py", line 113, in collect
    async for message in self:
  File "/debian/.local/lib/python3.9/site-packages/telethon/requestiter.py", line 74, in __anext__
    if await self._load_next_chunk():
  File "/debian/.local/lib/python3.9/site-packages/telethon/client/messages.py", line 184, in _load_next_chunk
    r = await self.client(self.request)
  File "/debian/.local/lib/python3.9/site-packages/telethon/client/account.py", line 77, in __call__
    return await self.__client(
  File "/debian/.local/lib/python3.9/site-packages/telethon/client/users.py", line 30, in __call__
    return await self._call(self._sender, request, ordered=ordered)
  File "/debian/.local/lib/python3.9/site-packages/telethon/client/users.py", line 84, in _call
    result = await future
telethon.errors.rpcerrorlist.TakeoutInvalidError: The takeout session has been invalidated by another data export session (caused by InvokeWithTakeoutRequest(GetHistoryRequest))
shinji257 commented 1 year ago

Ahh... Just looked at the code. The above is you handling it but you just leave it to the user to delete it and start again. I'm not sure if the fact that it still does a Traceback dump is intended though.

dima-kov commented 6 months ago

so what is the solution here? @shinji257

shinji257 commented 5 months ago

Probably to do as instructed. ¯\_(ツ)_/¯