fix: keep tdata export working offline and under open_basedir

This commit is contained in:
hashbro
2026-10-12 20:07:48 +00:00
parent fa9366703f
commit df91654724
2 changed files with 30 additions and 7 deletions
+11 -3
View File
@@ -64,14 +64,19 @@ def make_session(tmpdir: str, auth_key: bytes, dc_id: int) -> str:
return path + ".session"
def convert_to_tdata(session_file: str, out_dir: str):
def convert_to_tdata(session_file: str, out_dir: str, user_id: int):
"""Use opentele-ng to convert Telethon session → tdata folder (offline)."""
from opentele.td import TDesktop
from opentele.tl import TelegramClient
from opentele.api import UseCurrentSession
if not user_id:
raise ValueError("missing user_id (peerId); cannot convert offline")
async def _run():
client = TelegramClient(session_file)
# Seed UserId so opentele UseCurrentSession skips get_me() / network.
client.UserId = int(user_id)
try:
tdesk = await client.ToTDesktop(flag=UseCurrentSession)
if not tdesk.isLoaded():
@@ -80,7 +85,10 @@ def convert_to_tdata(session_file: str, out_dir: str):
shutil.rmtree(out_dir)
tdesk.SaveTData(out_dir)
finally:
await client.disconnect()
try:
await client.disconnect()
except Exception:
pass
asyncio.run(_run())
@@ -108,7 +116,7 @@ def main():
try:
session_file = make_session(tmpdir, auth_key, dc_id)
tdata_dir = os.path.join(tmpdir, "tdata")
convert_to_tdata(session_file, tdata_dir)
convert_to_tdata(session_file, tdata_dir, user_id)
zip_tdata(tdata_dir, output_zip)
print(f"wrote {output_zip} ({os.path.getsize(output_zip)} bytes)", file=sys.stderr)
finally: