-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
521 lines (478 loc) · 25.2 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
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
import asyncio
import os
import time
import re
import shutil
from pyrogram import Client, compose, filters
from pyrogram.errors import FloodWait
from pyrogram.types import (InputMediaAudio, InputMediaDocument, InputMediaPhoto, InputMediaVideo)
from progress import progress_for_pyrogram
from config import *
async def main():
try:
shutil.rmtree('./downloads')
except FileNotFoundError:
os.mkdir('./downloads')
apps = [
Client("bot_session", api_id=api_id, api_hash=api_hash, bot_token=bot_token, sleep_threshold=100000),
Client("account_session", api_id=api_id, api_hash=api_hash, sleep_threshold=100000, takeout=True)
]
my_bot = apps[0]
app = apps[1]
async def get_target_message(msg_link):
if "?single" in msg_link:
msg_link = msg_link.split("?single")[0]
from_chat = int('-100' + msg_link.split('/')[-2]) if msg_link.split('/')[-2].isdigit() else \
msg_link.split('/')[-2]
msg_id = int(msg_link.split('/')[-1])
print(msg_id)
return await app.get_messages(chat_id=from_chat, message_ids=msg_id)
def handle_file_list(file_list):
try:
if len(file_list) > 0:
for file_path in file_list:
os.remove(file_path)
except (UnboundLocalError, TypeError):
pass
async def handle_caption(target_message, message_group_list=None, want_replace=False):
# 如果想处理caption的话改成True..
rex = r'[a-zA-z]+://[^\s]*|t.me[^\s]*|@[a-zA-Z0-9_-]+'
p = re.compile(rex)
replace_username = "@username"
if target_message.media_group_id:
caption = None
for new_message in message_group_list:
if new_message.caption:
caption = new_message.caption
elif not target_message.media_group_id and target_message.media:
caption = target_message.caption
else:
caption = target_message.text
if want_replace:
if caption:
caption = p.sub(replace_username, caption)
return caption
filter_keyword = []
filter_user = []
def filter_message(caption, target_message):
Can_be_forwarded = True
for i in filter_keyword:
if i in caption:
Can_be_forwarded = False
return False
else:
Can_be_forwarded = True
if Can_be_forwarded:
for i in filter_user:
if i in target_message.from_user.id:
return False
else:
return True
async def check_is_video_photo(target_message, from_chat):
if target_message.media_group_id:
message_group_list = await app.get_media_group(from_chat, target_message.id)
media_type_list = []
for new_message in message_group_list:
media_type_list.append(str(new_message.media).split('.')[-1].lower())
media_type_set = set(media_type_list)
is_video_photo = True if len(media_type_set) != 1 else False
return is_video_photo
async def handle_download(target_message, client, to_edit_message):
file_save_path = await app.download_media(
target_message,
progress=progress_for_pyrogram,
progress_args=(
client,
"**DOWNLOADING:**\n",
to_edit_message,
time.time()
)
)
return file_save_path
async def handle_text(target_message, client, to_edit_message, message, from_chat):
await client.send_message(message.chat.id, target_message.text)
await to_edit_message.delete()
async def handle_video(target_message, client, to_edit_message, message, from_chat):
file_save_path_list = []
try:
if target_message.media_group_id:
upload_list = []
message_group_list = await app.get_media_group(from_chat, target_message.id)
caption = await handle_caption(target_message, message_group_list=message_group_list)
is_video_photo = await check_is_video_photo(target_message, from_chat)
if is_video_photo:
file_list = await handle_video(target_message, client, to_edit_message, message, from_chat,
)
handle_file_list(file_list)
else:
for new_message in message_group_list:
meta_media = getattr(new_message, new_message.media.value)
if meta_media.file_size > 2000000000:
print('文件超过 2G 无法上传跳过..')
continue
if meta_media.thumbs:
thumbs_path = await app.download_media(
meta_media.thumbs[0].file_id)
file_save_path_list.append(thumbs_path)
else:
thumbs_path = None
file_save_path = await app.download_media(
new_message,
progress=progress_for_pyrogram,
progress_args=(
client,
f"**DOWNLOADING:** {meta_media.file_name}\n",
to_edit_message,
time.time()
)
)
if message_group_list.index(new_message) == len(message_group_list) - 1:
upload_list.append(
InputMediaVideo(file_save_path,
thumb=thumbs_path,
caption=caption,
duration=meta_media.duration
)
)
else:
upload_list.append(
InputMediaVideo(file_save_path,
thumb=thumbs_path,
duration=meta_media.duration
)
)
file_save_path_list.append(file_save_path)
to_edit_message = await client.edit_message_text(message.chat.id, to_edit_message.id,
"**Trying to Upload.**")
await client.send_media_group(message.chat.id, upload_list)
else:
meta_media = getattr(target_message, target_message.media.value)
if meta_media.file_size > 2000000000:
print('文件超过 2G 无法上传跳过..')
return
file_path = await handle_download(target_message, client, to_edit_message)
thumbs_path = await app.download_media(
target_message.video.thumbs[0].file_id) if target_message.video.thumbs else None
caption = await handle_caption(target_message)
await client.send_video(message.chat.id, file_path,
caption=caption,
duration=target_message.video.duration,
width=target_message.video.width,
height=target_message.video.height,
thumb=thumbs_path,
progress=progress_for_pyrogram,
progress_args=(
client,
'**UPLOADING:**\n',
to_edit_message,
time.time()
)
)
os.remove(file_path)
os.remove(thumbs_path)
await to_edit_message.delete()
return file_save_path_list
except FloodWait as e:
await asyncio.sleep(e.value)
async def handle_photo(target_message, client, to_edit_message, message, from_chat):
file_save_path_list = []
try:
if target_message.media_group_id:
upload_list = []
message_group_list = await app.get_media_group(from_chat, target_message.id)
caption = await handle_caption(target_message, message_group_list=message_group_list)
is_video_photo = await check_is_video_photo(target_message, from_chat)
if is_video_photo:
file_list = await handle_video_photo(target_message, client, to_edit_message, message, from_chat,
)
handle_file_list(file_list)
else:
for new_message in message_group_list:
file_save_path = await app.download_media(
new_message,
progress=progress_for_pyrogram,
progress_args=(
client,
f"**DOWNLOADING:** 正在下载第{message_group_list.index(new_message) + 1}个图片\n",
to_edit_message,
time.time()
)
)
if message_group_list.index(new_message) == len(message_group_list) - 1:
upload_list.append(
InputMediaPhoto(file_save_path, caption=caption)
)
else:
upload_list.append(
InputMediaPhoto(file_save_path)
)
file_save_path_list.append(file_save_path)
to_edit_message = await client.edit_message_text(message.chat.id, to_edit_message.id,
"**Trying to Upload.**")
await client.send_media_group(message.chat.id, upload_list)
else:
file_path = await handle_download(target_message, client, to_edit_message)
caption = await handle_caption(target_message)
await client.send_photo(message.chat.id, file_path,
caption=caption,
progress=progress_for_pyrogram,
progress_args=(
client,
'**UPLOADING:**\n',
to_edit_message,
time.time()
)
)
os.remove(file_path)
await to_edit_message.delete()
return file_save_path_list
except FloodWait as e:
await asyncio.sleep(e.value)
async def handle_video_photo(target_message, client, to_edit_message, message, from_chat):
message_group_list = await app.get_media_group(from_chat, target_message.id)
caption = await handle_caption(target_message, message_group_list=message_group_list)
upload_list = []
file_save_path_list = []
for new_message in message_group_list:
meta_media = getattr(new_message, new_message.media.value)
if meta_media.file_size > 2000000000:
print('文件超过 2G 无法上传跳过..')
continue
if new_message.video:
if meta_media.thumbs:
thumbs_path = await app.download_media(meta_media.thumbs[0].file_id)
file_save_path_list.append(thumbs_path)
else:
thumbs_path = None
file_save_path = await app.download_media(
new_message,
progress=progress_for_pyrogram,
progress_args=(
client,
f"**DOWNLOADING:** {meta_media.file_name}\n",
to_edit_message,
time.time()
)
)
if message_group_list.index(new_message) == len(message_group_list) - 1:
upload_list.append(
InputMediaVideo(file_save_path, thumb=thumbs_path, caption=caption,
duration=meta_media.duration)
)
else:
upload_list.append(
InputMediaVideo(file_save_path, thumb=thumbs_path,
duration=meta_media.duration)
)
file_save_path_list.append(file_save_path)
elif new_message.photo:
file_save_path = await app.download_media(
new_message,
progress=progress_for_pyrogram,
progress_args=(
client,
f"**DOWNLOADING:** 正在下载第{message_group_list.index(new_message) + 1}个图片\n",
to_edit_message,
time.time()
)
)
if message_group_list.index(new_message) == len(message_group_list) - 1:
upload_list.append(
InputMediaPhoto(file_save_path, caption=caption)
)
else:
upload_list.append(
InputMediaPhoto(file_save_path)
)
file_save_path_list.append(file_save_path)
to_edit_message = await client.edit_message_text(message.chat.id, to_edit_message.id,
"**Trying to Upload.**")
await client.send_media_group(message.chat.id, upload_list)
await to_edit_message.delete()
return file_save_path_list
async def handle_document(target_message, client, to_edit_message, message, from_chat):
file_save_path_list = []
try:
if target_message.media_group_id:
upload_list = []
message_group_list = await app.get_media_group(from_chat, target_message.id)
caption = await handle_caption(target_message, message_group_list=message_group_list)
for new_message in message_group_list:
meta_media = getattr(new_message, new_message.media.value)
if meta_media.file_size > 2000000000:
print('文件超过 2G 无法上传跳过..')
continue
file_save_path = await app.download_media(
new_message,
progress=progress_for_pyrogram,
progress_args=(
client,
f"**DOWNLOADING:** {meta_media.file_name}\n",
to_edit_message,
time.time()
)
)
if message_group_list.index(new_message) == len(message_group_list) - 1:
upload_list.append(
InputMediaDocument(file_save_path, caption=caption)
)
else:
upload_list.append(
InputMediaDocument(file_save_path)
)
file_save_path_list.append(file_save_path)
to_edit_message = await client.edit_message_text(message.chat.id, to_edit_message.id,
"**Trying to Upload.**")
await client.send_media_group(message.chat.id, upload_list)
else:
meta_media = getattr(target_message, target_message.media.value)
if meta_media.file_size > 2000000000:
print('文件超过 2G 无法上传跳过..')
return
file_path = await handle_download(target_message, client, to_edit_message)
caption = await handle_caption(target_message)
await client.send_document(message.chat.id, file_path,
caption=caption,
progress=progress_for_pyrogram,
progress_args=(
client,
'**UPLOADING:**\n',
to_edit_message,
time.time()
)
)
os.remove(file_path)
await to_edit_message.delete()
return file_save_path_list
except FloodWait as e:
await asyncio.sleep(e.value)
async def handle_audio(target_message, client, to_edit_message, message, from_chat):
performer = None
file_save_path_list = []
try:
if target_message.media_group_id:
upload_list = []
message_group_list = await app.get_media_group(from_chat, target_message.id)
caption = await handle_caption(target_message, message_group_list=message_group_list)
for new_message in message_group_list:
meta_media = getattr(new_message, new_message.media.value)
title = new_message.audio.title if new_message.audio.title else None
file_save_path = await app.download_media(
new_message,
progress=progress_for_pyrogram,
progress_args=(
client,
f"**DOWNLOADING:** {meta_media.file_name}\n",
to_edit_message,
time.time()
)
)
if message_group_list.index(new_message) == len(message_group_list) - 1:
upload_list.append(
InputMediaAudio(file_save_path, caption=caption,
duration=new_message.audio.duration, title=title,
performer=performer)
)
else:
upload_list.append(
InputMediaAudio(file_save_path,
duration=new_message.audio.duration, title=title,
performer=performer)
)
file_save_path_list.append(file_save_path)
to_edit_message = await client.edit_message_text(message.chat.id, to_edit_message.id,
"**Trying to Upload.**")
await client.send_media_group(message.chat.id, upload_list)
else:
file_path = await handle_download(target_message, client, to_edit_message)
title = target_message.audio.title if target_message.audio.title else None
caption = await handle_caption(target_message, from_chat)
await client.send_audio(message.chat.id, file_path,
caption=caption,
duration=target_message.audio.duration,
title=title,
file_name=title, performer=performer,
progress=progress_for_pyrogram,
progress_args=(
client,
'**UPLOADING:**\n',
to_edit_message,
time.time()
)
)
os.remove(file_path)
await to_edit_message.delete()
return file_save_path_list
except FloodWait as e:
await asyncio.sleep(e.value)
async def handle_web_page(message, target_message, to_edit_message, client):
to_edit_message = await client.edit_message_text(message.chat.id,
to_edit_message.id,
"Cloning.")
await client.send_message(message.chat.id, target_message.text.markdown)
await to_edit_message.delete()
@my_bot.on_message(filters.text)
async def handle_command(client, message):
delay = 4
func_dic = {
'audio': handle_audio,
'document': handle_document,
'video': handle_video,
'photo': handle_photo,
}
split_space = message.text.split(' ')
if len(split_space) == 2:
msg_link, end_id = split_space[0], int(split_space[1])
if "?single" in msg_link:
msg_link = msg_link.split("?single")[0]
start_id = int(msg_link.split('/')[-1])
from_chat = int('-100' + msg_link.split('/')[-2]) if msg_link.split('/')[-2].isdigit() else \
msg_link.split('/')[
-2]
print(msg_link, start_id, end_id, from_chat)
target_message = await get_target_message(msg_link)
offset = 0
for message_id in range(start_id, end_id + 1):
if message_id <= offset:
print(f'{message_id} 已经处理过')
continue
target_message = await app.get_messages(chat_id=from_chat, message_ids=message_id)
if target_message.empty:
print(f'跳过空消息 {message_id}')
continue
is_super_user = target_message.sender_chat or target_message.from_user.id == filter_user_id
if not is_super_user:
print('跳过非管理员信息')
continue
to_edit_message = await client.send_message(message.chat.id, f'Processing message id {message_id}!')
if target_message.media_group_id:
message_group_list = await app.get_media_group(from_chat, target_message.id)
offset = message_group_list[-1].id
if target_message.media:
meta_media = getattr(target_message, target_message.media.value)
try:
file_name = meta_media.file_name if meta_media.file_name else '未知文件'
to_edit_message = await client.edit_message_text(message.chat.id, to_edit_message.id,
f"Trying to Download {file_name}!")
except AttributeError:
to_edit_message = await client.edit_message_text(message.chat.id, to_edit_message.id,
f"Trying to Download Photo!")
try:
file_list = await func_dic[target_message.media.value](target_message, client, to_edit_message,
message,
from_chat)
handle_file_list(file_list)
except KeyError:
await to_edit_message.delete()
continue
if target_message.media_group_id:
await asyncio.sleep(delay * len(message_group_list))
else:
await asyncio.sleep(delay)
if not target_message.media:
if target_message.sender_chat or target_message.from_user.id == filter_user_id:
await handle_text(target_message, client, to_edit_message, message, from_chat)
await asyncio.sleep(delay)
await to_edit_message.delete()
await compose(apps)
asyncio.run(main())