-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtelegram-download.py
65 lines (52 loc) · 1.88 KB
/
telegram-download.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
import asyncio
from datetime import datetime
import os
import json
import csv
import telethon
import telethon.sessions
from tqdm.asyncio import tqdm
class App:
tg: telethon.TelegramClient
settings: dict
def __init__(self) -> None:
self.tg = telethon.TelegramClient(
telethon.sessions.StringSession(os.environ["TELEGRAM_SESSION_TELETHON"]),
int(os.environ["TELEGRAM_API_ID"]),
os.environ["TELEGRAM_API_HASH"],
)
self.settings = json.load(open("settings.json"))
self.settings["download"]["from_date"] = datetime.strptime(
self.settings["download"]["from_date"], "%Y-%m-%d"
)
async def __aenter__(self):
await self.tg.start() # type: ignore
return self
async def __aexit__(self, exc_type, exc, tb):
await self.tg.disconnect() # type: ignore
async def amain():
async with App() as app:
for chat in app.settings["download"]["from_chats"]:
with open(f"{chat}.csv", "w") as f_:
f = csv.DictWriter(f_, fieldnames=["from_id", "date", "message"])
f.writeheader()
async for msg in tqdm(
app.tg.iter_messages(
chat,
offset_date=app.settings["download"]["from_date"],
reverse=True,
)
):
if msg.message:
# print(f"{msg.from_id.user_id} at {msg.date}: {msg.message}")
f.writerow(
{
"from_id": msg.from_id.user_id,
"date": msg.date.isoformat(),
"message": msg.message,
}
)
def main():
asyncio.run(amain())
if __name__ == "__main__":
main()