Repository navigation
Expand file tree
/
Copy pathparser.py
More file actions
124 lines (93 loc) · 3.89 KB
/
Copy pathparser.py
File metadata and controls
124 lines (93 loc) · 3.89 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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
import asyncio
from datetime import datetime, timezone
from telethon import TelegramClient
from telethon.tl.functions.messages import GetHistoryRequest
from database import add_message, save_event, get_stats, check_sent
from rabbitmq import connect_rabbit, setup_exchange, publish_event, EXCHANGE_EVENTS, ROUTING_EVENT
from models import load_model, classify
from config import (
TG_API_ID, TG_API_HASH,
RABBITMQ_HOST, RABBITMQ_PORT, RABBITMQ_USER, RABBITMQ_PASS
)
async def connect_tg():
client = TelegramClient("sessions/extractor_session", TG_API_ID, TG_API_HASH)
await client.connect()
try:
authorized = await client.is_user_authorized()
except TypeError:
authorized = client.is_user_authorized()
if not authorized:
await client.disconnect()
raise RuntimeError("Telegram session missing; run auth.py to authorize")
return client
def init_rabbit():
connect_rabbit(RABBITMQ_HOST, RABBITMQ_PORT, RABBITMQ_USER, RABBITMQ_PASS)
setup_exchange()
async def parse_messages(client, model_name, channel_name, start_date, end_date, check_duplicates=True):
entity = await client.get_entity(channel_name)
parsed_count = 0
offset_id = 0
offset_date = end_date
while True:
history = await client(GetHistoryRequest(
peer=entity,
offset_id=offset_id,
offset_date=offset_date,
add_offset=0,
limit=100,
max_id=0,
min_id=0,
hash=0
))
if not history.messages:
break
stop_parsing = False
for message in history.messages:
if not message.message:
continue
msg_date = message.date.replace(tzinfo=timezone.utc)
if msg_date < start_date:
stop_parsing = True
continue
if msg_date > end_date:
continue
msg_link = "https://t.me/" + channel_name + "/" + str(message.id)
add_message(channel_name, message.id, message.message, msg_date, msg_link)
is_event, confidence = classify(message.message, model_name)
save_event(channel_name, message.id, is_event, confidence, "мероприятие" if is_event else None)
if is_event:
send = True
if check_duplicates:
send = not check_sent(channel_name, message.id)
if send:
event = {
'id': message.id,
'channel': channel_name,
'text': message.message,
'date': msg_date.isoformat(),
'link': msg_link,
'confidence': confidence,
'event_type': "мероприятие"
}
publish_event(event)
parsed_count += 1
if stop_parsing or len(history.messages) < 100:
break
offset_id = history.messages[-1].id
offset_date = history.messages[-1].date
await asyncio.sleep(0.2)
return parsed_count
async def parse_channel(channel, start_date, end_date, model_name=None, check_duplicates=True):
start_date = start_date.replace(tzinfo=timezone.utc)
end_date = end_date.replace(tzinfo=timezone.utc)
model_name = model_name or 'rubert'
load_model(model_name)
client = await connect_tg()
init_rabbit()
extracted = await parse_messages(
client, model_name,
channel, start_date, end_date, check_duplicates
)
await client.disconnect()
stats = get_stats(channel, start_date, end_date)
return extracted, stats['events'], stats['sent']