forked from tomoresee/Telegram-Chat-Parser
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
95 lines (73 loc) · 3.44 KB
/
Copy pathmain.py
File metadata and controls
95 lines (73 loc) · 3.44 KB
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
import asyncio
from datetime import datetime
from typing import Optional
from core.auth import get_client, authenticate
from core.fetcher import get_private_dialogs, fetch_messages
from core.formatter import format_dialog
from core.exporter import save_to_file
def parse_date(prompt: str) -> Optional[datetime]:
raw = input(prompt).strip()
if not raw:
return None
try:
return datetime.strptime(raw, "%d.%m.%Y")
except ValueError:
print(" Неверный формат даты, игнорируется.")
return None
async def main():
print("=== Telegram Parser ===\n")
client = get_client()
async with client:
await authenticate(client)
print("Загружаю список чатов...")
dialogs = await get_private_dialogs(client)
if not dialogs:
print("Личные чаты не найдены.")
return
print(f"\nНайдено личных чатов: {len(dialogs)}\n")
for i, d in enumerate(dialogs, 1):
print(f" {i}. {d['name']}")
print("\nВведите номера чатов через запятую (например: 1,3,5)")
print("Или введите 0, чтобы выбрать все чаты.")
choice = input("Ваш выбор: ").strip()
if choice == "0":
selected = dialogs
else:
try:
indices = [int(x.strip()) - 1 for x in choice.split(",")]
selected = [dialogs[i] for i in indices if 0 <= i < len(dialogs)]
except (ValueError, IndexError):
print("Неверный ввод. Завершение.")
return
if not selected:
print("Ни один чат не выбран. Завершение.")
return
print("\nУкажите временной период (или нажмите Enter, чтобы пропустить):")
date_from = parse_date(" Начальная дата (ДД.ММ.ГГГГ): ")
date_to_raw = parse_date(" Конечная дата (ДД.ММ.ГГГГ): ")
# Включаем весь конечный день: сдвигаем до 23:59:59
date_to = date_to_raw.replace(hour=23, minute=59, second=59) if date_to_raw else None
print(f"\nНачинаю экспорт {len(selected)} чат(ов)...\n")
all_blocks = []
for d in selected:
name = d["name"]
print(f" Обрабатываю: {name}", end="", flush=True)
def progress(n, _name=name):
print(f"\r Обрабатываю: {_name} — {n} сообщений", end="", flush=True)
messages = await fetch_messages(
client, d["entity"], date_from, date_to, on_progress=progress
)
print() # перевод строки после счётчика
if messages:
block = format_dialog(name, messages)
all_blocks.append(block)
else:
print(f" (нет сообщений за указанный период)")
if not all_blocks:
print("\nНет данных для экспорта.")
return
content = "\n".join(all_blocks)
filepath = save_to_file(content)
print(f"\nГотово! Файл сохранён:\n {filepath}")
if __name__ == "__main__":
asyncio.run(main())