-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
434 lines (369 loc) · 15.3 KB
/
main.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
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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
"""
ALL BY AI, NOT HUMAN
"""
import os
import re
import json
import pytz
import base64
import logging
import asyncio
import imagesize
from typing import List, Tuple, Dict, Any
from dataclasses import dataclass
from datetime import datetime, timedelta
from telethon import TelegramClient
from telethon.types import Message, Channel, PeerChannel, PeerChat, PeerUser
from telethon.sessions import StringSession
from github import Github, InputGitTreeElement, GithubException
logging.basicConfig(
level=logging.INFO, format="%(asctime)s - %(levelname)s - %(message)s"
)
@dataclass
class Config:
GITHUB_TOKEN: str
GITHUB_REPO: str
API_ID: str
API_HASH: str
SESSION_STRING: str
CHANNEL_USERNAME: str
GITHUB_BRANCH: str = "gh-pages"
GITHUB_FOLDER: str = "assets/channel/"
DAY_LIMIT: int = 7
TIME_ZONE: str = "Asia/Shanghai"
config = Config(
GITHUB_TOKEN=os.getenv("GITHUB_TOKEN"),
GITHUB_REPO=os.getenv("GITHUB_REPO"),
API_ID=os.getenv("TELEGRAM_API_ID"),
API_HASH=os.getenv("TELEGRAM_API_HASH"),
SESSION_STRING=os.getenv("TELEGRAM_SESSION_STRING"),
CHANNEL_USERNAME=os.getenv("TELEGRAM_CHANNEL_USERNAME"),
DAY_LIMIT=int(os.getenv("DAY_LIMIT") or 7),
)
class TelegramProcessor:
def __init__(self, config: Config):
self.config = config
self.client = TelegramClient(
StringSession(config.SESSION_STRING), config.API_ID, config.API_HASH
)
self.py_time_zone = pytz.timezone(config.TIME_ZONE)
@staticmethod
def extract_tags(text: str) -> Tuple[str, List[str]]:
if not text:
return text, []
if "\n" not in text:
tags = re.findall(r"#(\w+)", text) or []
if tags:
return "", tags
return text, []
last_line = text.strip().split("\n")[-1]
tags = re.findall(r"#(\w+)", last_line) or []
if tags:
text = "\n".join(text.strip().split("\n")[:-1])
return text, tags
@staticmethod
def custom_filename(message: Message, file_path: str) -> str:
_, ext = os.path.splitext(file_path)
date_str = message.date.strftime("%Y%m%d%H%M%S")
return f"{message.id}_{date_str}{ext}"
async def get_messages_in_range(
self,
channel: Channel,
start_date: datetime,
end_date: datetime,
limit: int = None,
) -> List[Message]:
messages = []
if start_date > end_date:
start_date, end_date = end_date, start_date
async for message in self.client.iter_messages(
channel, limit=limit, offset_date=start_date, reverse=True
):
if message.date < start_date:
break
if start_date <= message.date <= end_date:
messages.append(message)
return messages
async def process_messages(self) -> Dict[str, Dict[str, Any]]:
async with self.client:
channel = await self.client.get_entity(self.config.CHANNEL_USERNAME)
now = datetime.now(self.py_time_zone)
start_date = (now - timedelta(days=self.config.DAY_LIMIT)).replace(
hour=0, minute=0, second=0, microsecond=0
)
end_date = now
start_date_utc = start_date.astimezone(pytz.utc)
end_date_utc = end_date.astimezone(pytz.utc)
messages = await self.get_messages_in_range(
channel, start_date_utc, end_date_utc
)
return await self.process_message_groups(messages)
async def process_message_groups(
self, messages: List[Message]
) -> Dict[str, Dict[str, Any]]:
updates = {}
message_group = []
for message in reversed(messages):
if not message_group or (
message.grouped_id
and message.grouped_id == message_group[-1].grouped_id
):
message_group.append(message)
else:
await self.process_and_update(message_group, updates)
message_group = [message]
if message_group:
await self.process_and_update(message_group, updates)
return updates
async def process_and_update(
self, message_group: List[Message], updates: Dict[str, Dict[str, Any]]
):
date, group_data, media_files = await self.process_message_group(message_group)
if not (group_data["text"] or group_data["photos"]):
return
month = date[:7] # YYYY-mm
if month not in updates:
updates[month] = {"content": {}, "media": []}
if date not in updates[month]["content"]:
updates[month]["content"][date] = []
updates[month]["content"][date].append(group_data)
updates[month]["media"].extend(media_files)
async def fetch_sender_info(self, from_id):
info = {}
if isinstance(from_id, PeerChannel):
info["from_type"] = "channel"
info["from_id"] = str(from_id.channel_id)
elif isinstance(from_id, PeerUser):
info["from_type"] = "user"
info["from_id"] = str(from_id.user_id)
elif isinstance(from_id, PeerChat):
info["from_type"] = "chat"
info["from_id"] = str(from_id.chat_id)
return info
async def process_message_group(
self, messages: List[Message]
) -> Tuple[str, Dict[str, Any], List[Tuple[str, str]]]:
first_message = messages[0]
utc_date = first_message.date
local_date = utc_date.replace(tzinfo=pytz.utc).astimezone(self.py_time_zone)
date = local_date.strftime("%Y-%m-%d")
msg_time = local_date.strftime("%Y-%m-%d %H:%M:%S")
group_data = {
"id": first_message.id,
"created_at": msg_time,
"date": date,
"text": "",
"photos": [],
"tags": set(),
"quoted_message": None,
"forwarded_info": None,
}
media_files = []
for message in messages:
text = message.text or ""
media = message.media
if not text and not media:
continue
text, tags = self.extract_tags(text)
group_data["tags"].update(tags)
group_data["text"] = "\n".join([group_data["text"], text]).strip()
if message.forward:
forward = message.forward
forward_info = {
"from_id": str(forward.from_id) if forward.from_id else None,
"from_name": forward.from_name,
"post_author": forward.post_author,
"channel_post": forward.channel_post,
"created_at": (
forward.date.strftime("%Y-%m-%d %H:%M:%S")
if forward.date
else None
),
}
if forward.from_id:
info = await self.fetch_sender_info(forward.from_id)
forward_info.update(info)
group_data["forwarded_info"] = forward_info
if media and message.file:
fn = str(message.file.name)
path = await message.download_media(
file=self.custom_filename(message, fn)
)
if path:
width, height = imagesize.get(path)
media_files.append((f"{date}/{path}", path))
group_data["photos"].append(
{
"path": path,
"width": width,
"height": height,
"id": message.id,
}
)
if (
group_data["quoted_message"] is None
and hasattr(message, "reply_to")
and message.reply_to
):
try:
replied_msg = await message.get_reply_message()
if replied_msg:
replied_local_date = replied_msg.date.replace(
tzinfo=pytz.utc
).astimezone(self.py_time_zone)
replied_text = getattr(replied_msg, "message", "")
from_id = (
str(replied_msg.sender_id)
if replied_msg.sender_id
else None
)
group_data["quoted_message"] = {
"id": replied_msg.id,
"text": (
replied_text[:80] + "..."
if replied_text and len(replied_text) > 80
else (replied_text or "")
),
"from_id": from_id,
"created_at": replied_local_date.strftime(
"%Y-%m-%d %H:%M:%S"
),
}
if from_id:
info = await self.fetch_sender_info(from_id)
group_data["quoted_message"].update(info)
except Exception as e:
logging.error(
f"Error fetching replied message for {message.id}: {e}"
)
group_data["tags"] = sorted(list(group_data["tags"]))
group_data["photos"] = list(reversed(group_data["photos"]))
return date, group_data, media_files
class GithubUpdater:
def __init__(self, config: Config):
self.config = config
self.g = Github(config.GITHUB_TOKEN)
self.repo = self.g.get_repo(config.GITHUB_REPO)
def update_repository(self, updates: Dict[str, Dict[str, Any]]):
self.ensure_branch_exists()
branch_ref = self.repo.get_git_ref(f"heads/{self.config.GITHUB_BRANCH}")
branch_sha = branch_ref.object.sha
element_list = self.prepare_updates(updates)
if not element_list:
logging.info("No new updates found. Skipping commit.")
return
logging.info(f"Updating repository with: {element_list}")
self.create_commit(element_list, branch_sha, branch_ref)
def ensure_branch_exists(self):
branch = self.config.GITHUB_BRANCH
try:
self.repo.get_branch(branch)
except GithubException:
logging.info(f"Branch {branch} does not exist. Creating it...")
sb = self.repo.get_branch(self.repo.default_branch)
self.repo.create_git_ref(ref=f"refs/heads/{branch}", sha=sb.commit.sha)
logging.info(f"Branch {branch} created successfully.")
def prepare_updates(
self, updates: Dict[str, Dict[str, Any]]
) -> List[InputGitTreeElement]:
element_list = []
for month, data in updates.items():
self.update_daily_files(month, data, element_list)
self.update_monthly_file(month, data, element_list)
self.update_media_files(month, data, element_list)
return element_list
def update_daily_files(
self, month: str, data: Dict[str, Any], element_list: List[InputGitTreeElement]
):
for date, content in data["content"].items():
file_path = f"{self.config.GITHUB_FOLDER}{month}/{date}/data.json"
content_json = json.dumps(
content, ensure_ascii=False, separators=(",", ":")
)
blob = self.repo.create_git_blob(content_json, "utf-8")
if self.content_changed(file_path, blob.sha):
element_list.append(
InputGitTreeElement(file_path, "100644", "blob", sha=blob.sha)
)
def remove_day_after(self, data: List[Dict[str, Any]], date: str):
for key in data:
if key["date"] >= date:
data.remove(key)
def update_monthly_file(
self, month: str, data: Dict[str, Any], element_list: List[InputGitTreeElement]
):
monthly_file_path = f"{self.config.GITHUB_FOLDER}{month}/data.json"
existing_monthly_data = self.get_file_content(monthly_file_path) or []
new_monthly_data = []
for date in sorted(data["content"].keys()):
new_monthly_data.extend(data["content"][date])
last_day = sorted(new_monthly_data, key=lambda x: x["id"])[0]["date"]
self.remove_day_after(existing_monthly_data, last_day)
combined_data = existing_monthly_data + new_monthly_data
combined_data = sorted(
{item["id"]: item for item in combined_data}.values(),
key=lambda x: x["id"],
reverse=True,
)
monthly_content = json.dumps(
combined_data, ensure_ascii=False, separators=(",", ":")
)
blob = self.repo.create_git_blob(monthly_content, "utf-8")
if self.content_changed(monthly_file_path, blob.sha):
element_list.append(
InputGitTreeElement(monthly_file_path, "100644", "blob", sha=blob.sha)
)
def update_media_files(
self, month: str, data: Dict[str, Any], element_list: List[InputGitTreeElement]
):
for media_path, local_path in data["media"]:
with open(local_path, "rb") as file:
file_content = file.read()
blob = self.repo.create_git_blob(
base64.b64encode(file_content).decode("utf-8"), "base64"
)
github_media_path = f"{self.config.GITHUB_FOLDER}{month}/{media_path}"
if self.content_changed(github_media_path, blob.sha):
element_list.append(
InputGitTreeElement(
github_media_path, "100644", "blob", sha=blob.sha
)
)
os.remove(local_path)
def get_file_content(self, path: str) -> Any:
try:
content = self.repo.get_contents(path, ref=self.config.GITHUB_BRANCH)
return json.loads(content.decoded_content.decode("utf-8"))
except GithubException:
return None
def content_changed(self, file_path: str, new_content_sha: str) -> bool:
try:
old_content = self.repo.get_contents(
file_path, ref=self.config.GITHUB_BRANCH
)
return old_content.sha != new_content_sha
except Exception:
return True
def create_commit(
self, element_list: List[InputGitTreeElement], branch_sha: str, branch_ref: Any
):
base_tree = self.repo.get_git_tree(branch_sha)
tree = self.repo.create_git_tree(element_list, base_tree)
parent = self.repo.get_git_commit(branch_sha)
commit = self.repo.create_git_commit(
f"Update for {datetime.now().strftime('%Y-%m-%d %H:%M:%S')}", tree, [parent]
)
branch_ref.edit(commit.sha)
async def main():
telegram_processor = TelegramProcessor(config)
github_updater = GithubUpdater(config)
try:
updates = await telegram_processor.process_messages()
github_updater.update_repository(updates)
logging.info(
f"Successfully updated repository with changes for months: {', '.join(updates.keys())}"
)
except Exception as e:
logging.error(f"An error occurred: {e}")
if __name__ == "__main__":
asyncio.run(main())