-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathbot.py
929 lines (813 loc) · 30.8 KB
/
bot.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
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
import os
import json
import time
from datetime import datetime
import uuid
from typing import Union
from pyrogram import Client
from pyrogram.types import Message
from pyrogram.enums import ChatType, MessageEntityType
from configs import API_ID, API_HASH, MEDIA_EXPORT, CHAT_EXPORT, CHAT_IDS, FILE_NOT_FOUND, JSON_FILE_PAGE_SIZE
from chats import Chat
class Archive:
def __init__(self, chat_ids: list = []):
self.chat_ids = chat_ids
self.voice_num = 0
self.photo_num = 0
self.contact_num = 0
self.video_message_num = 0
self.username = ''
self.messages = []
self.chat_data = {}
def fill_chat_data(self, chat: Chat) -> None:
self.username = chat.username
match chat.type:
case ChatType.PRIVATE:
# TODO: lastname
self.chat_data['name'] = chat.first_name
self.chat_data['type'] = 'personal_chat'
self.chat_data['id'] = chat.id
case ChatType.CHANNEL:
self.chat_data['name'] = chat.title
self.chat_data['type'] = 'public_channel'
# when using telegram api ids have -100 prefix
# https://stackoverflow.com/questions/33858927/how-to-obtain-the-chat-id-of-a-private-telegram-channel
self.chat_data['id'] = str(chat.id)[4::] if str(chat.id).startswith('-100') else chat.id
case ChatType.GROUP:
self.chat_data['name'] = chat.title
self.chat_data['type'] = 'public_group'
self.chat_data['id'] = str(chat.id)[4::] if str(chat.id).startswith('-100') else chat.id
case ChatType.SUPERGROUP:
self.chat_data['name'] = chat.title
self.chat_data['type'] = 'public_supergroup'
self.chat_data['id'] = str(chat.id)[4::] if str(chat.id).startswith('-100') else chat.id
# TODO: private SUPERGROUP and GROUP
case _:
# bot? other chat types
pass
async def process_message(self, chat, message, msg_info: dict) -> None:
# TODO: move msg_info filling to other function
msg_info['id'] = message.id
msg_info['type'] = 'message'
msg_info['date'] = message.date.strftime('%Y-%m-%dT%H:%M:%S')
msg_info['date_unixtime'] = convert_to_unixtime(message.date)
# set chat name
# this part is so shitty fix THIS
if chat.type == ChatType.PRIVATE:
full_name = ''
if message.from_user.first_name is not None:
full_name += message.from_user.first_name
if message.from_user.last_name is not None:
full_name += f' {message.from_user.last_name}'
msg_info['from'] = full_name
msg_info['from_id'] = f'user{message.from_user.id}'
else:
msg_info['from'] = chat.title
# TODO: is this correct for groups?
if chat.type == ChatType.CHANNEL:
if str(message.sender_chat.id).startswith('-100'):
msg_info['from_id'] = f'channel{str(message.sender_chat.id)[4::]}'
else:
msg_info['from_id'] = f'channel{str(message.sender_chat.id)}'
else:
pass
# TODO: use from user...
if message.reply_to_message_id is not None:
msg_info['reply_to_message_id'] = message.reply_to_message_id
if message.forward_from_chat is not None:
msg_info['forwarded_from'] = message.forward_from_chat.title
elif message.forward_from is not None:
msg_info['forwarded_from'] = message.forward_from.first_name
# TODO: type service. actor...
if message.sticker is not None:
if MEDIA_EXPORT['stickers'] is True:
names = get_sticker_name(
message,
self.username
)
await get_sticker_data(message, msg_info, names)
else:
msg_info['file'] = FILE_NOT_FOUND
msg_info['thumbnail'] = FILE_NOT_FOUND
msg_info['media_type'] = 'sticker'
msg_info['sticker_emoji'] = message.sticker.emoji
msg_info['width'] = message.sticker.width
msg_info['height'] = message.sticker.height
elif message.animation is not None:
if MEDIA_EXPORT['animations'] is True:
names = get_animation_name(message, self.username)
await get_animation_data(
message,
msg_info,
names
)
else:
msg_info['file'] = FILE_NOT_FOUND
msg_info['thumbnail'] = FILE_NOT_FOUND
msg_info['media_type'] = 'animation'
msg_info['mime_type'] = message.animation.mime_type
msg_info['width'] = message.animation.width
elif message.photo is not None:
if MEDIA_EXPORT['photos'] is True:
self.photo_num += 1
names = get_photo_name(
message,
self.username,
self.photo_num
)
await get_photo_data(
message,
msg_info,
names
)
else:
msg_info['photo'] = FILE_NOT_FOUND
msg_info['width'] = message.photo.width
msg_info['height'] = message.photo.height
elif message.video is not None:
if MEDIA_EXPORT['videos'] is True:
names = get_video_name(message, self.username)
await get_video_data(message, msg_info, names)
else:
msg_info['file'] = FILE_NOT_FOUND
msg_info['thumbnail'] = FILE_NOT_FOUND
msg_info['media_type'] = 'video_file'
msg_info['mime_type'] = message.video.mime_type
msg_info['duration_seconds'] = message.video.duration
msg_info['width'] = message.video.width
elif message.video_note is not None:
if MEDIA_EXPORT['video_messages'] is True:
self.video_message_num += 1
names = get_video_name(
message,
self.username,
self.video_message_num
)
await get_video_note_data(
message,
msg_info,
names
)
else:
msg_info['file'] = FILE_NOT_FOUND
msg_info['thumbnail'] = FILE_NOT_FOUND
msg_info['media_type'] = 'video_message'
msg_info['mime_type'] = message.video_note.mime_type
msg_info['duration_seconds'] = message.video_note.duration
elif message.audio is not None:
if MEDIA_EXPORT['audios'] is True:
names = get_audio_name(message, self.username)
await get_audio_data(message, msg_info, names)
else:
msg_info['file'] = FILE_NOT_FOUND
msg_info['thumbnail'] = FILE_NOT_FOUND
msg_info['media_type'] = 'audio_file'
msg_info['performer'] = message.audio.performer
msg_info['title'] = message.audio.title
msg_info['mime_type'] = message.audio.mime_type
elif message.voice is not None:
if MEDIA_EXPORT['voice_messages'] is True:
self.voice_num += 1
names = get_voice_name(
message,
self.username,
self.voice_num
)
await get_voice_data(
message,
msg_info,
names
)
else:
msg_info['file'] = FILE_NOT_FOUND
elif message.document is not None:
if MEDIA_EXPORT['documents'] is True:
names = get_document_name(message, self.username)
await get_document_data(
message,
msg_info,
names
)
else:
msg_info['file'] = FILE_NOT_FOUND
msg_info['thumbnail'] = FILE_NOT_FOUND
elif message.contact is not None:
self.contact_num += 1
names = get_contact_name(
self.username,
self.contact_num
)
get_contact_data(
message,
msg_info,
names
)
elif message.location is not None:
msg_info['location_information'] = {
'latitude': message.location.latitude,
'longitude': message.location.longitude
}
if message.text is not None:
text = get_text_data(message, 'text')
if text:
text.append(message.text)
msg_info['text'] = text
else:
msg_info['text'] = message.text
elif message.caption is not None:
caption = get_text_data(message, 'caption')
if caption:
caption.append(message.caption)
msg_info['caption'] = caption
else:
msg_info['caption'] = message.caption
else:
msg_info['text'] = ''
self.messages.append(msg_info)
self.chat_data['messages'] = self.messages
def generate_json_name(username: str, path: str = '') -> str:
# TODO: add path when user want other path
chat_export_date = datetime.now().strftime("%Y-%m-%d")
chat_export_name = f'ChatExport_{username}_{chat_export_date}'
json_name = f'{chat_export_name}/result.json'
os.makedirs(chat_export_name, exist_ok=True)
return json_name
async def get_sticker_data(
message: Message,
msg_info: dict,
names: tuple
) -> None:
sticker_path, thumb_path, sticker_relative_path, thumb_relative_path = names
try:
await app.download_media(
message.sticker.file_id,
sticker_path
)
msg_info['file'] = sticker_relative_path
except ValueError:
print("Oops can't download media!")
msg_info['file'] = FILE_NOT_FOUND
if message.sticker.thumbs is not None:
try:
await app.download_media(
message.sticker.thumbs[0].file_id,
thumb_path
)
msg_info['thumbnail'] = thumb_relative_path
except ValueError:
print("Oops can't download media!")
msg_info['thumbnail'] = FILE_NOT_FOUND
else:
msg_info['thumbnail'] = FILE_NOT_FOUND
msg_info['media_type'] = 'sticker'
msg_info['sticker_emoji'] = message.sticker.emoji
msg_info['width'] = message.sticker.width
msg_info['height'] = message.sticker.height
async def get_animation_data(
message: Message,
msg_info: dict,
names: tuple
) -> None:
animation_path, thumb_path, animation_relative_path, thumb_relative_path = names
try:
await app.download_media(
message.animation.file_id,
animation_path
)
msg_info['file'] = animation_relative_path
except ValueError:
print("Oops can't download media!")
msg_info['file'] = FILE_NOT_FOUND
if message.animation.thumbs is not None:
try:
await app.download_media(
message.animation.thumbs[0].file_id,
thumb_path
)
except ValueError:
print("Oops can't download media!")
msg_info['thumbnail'] = FILE_NOT_FOUND
else:
msg_info['thumbnail'] = FILE_NOT_FOUND
msg_info['media_type'] = 'animation'
msg_info['mime_type'] = message.animation.mime_type
msg_info['width'] = message.animation.width
msg_info['height'] = message.animation.height
async def get_photo_data(
message: Message,
msg_info: dict,
names: tuple
):
photo_path, photo_relative_path = names
try:
await app.download_media(
message.photo.file_id,
photo_path
)
msg_info['photo'] = photo_relative_path
except ValueError:
print("Oops can't download media!")
msg_info['photo'] = FILE_NOT_FOUND
msg_info['width'] = message.photo.width
msg_info['height'] = message.photo.height
async def get_video_data(
message: Message,
msg_info: dict,
names: tuple
) -> None:
video_path, thumb_path, video_relative_path, thumb_relative_path = names
try:
await app.download_media(
message.video.file_id,
video_path
)
msg_info['file'] = video_relative_path
except ValueError:
print("Oops can't download media!")
msg_info['file'] = FILE_NOT_FOUND
if message.video.thumbs is not None:
try:
await app.download_media(
message.video.thumbs[0].file_id,
thumb_path
)
msg_info['thumbnail'] = thumb_relative_path
except ValueError:
print("Oops can't download media!")
msg_info['thumbnail'] = FILE_NOT_FOUND
else:
msg_info['thumbnail'] = FILE_NOT_FOUND
msg_info['media_type'] = 'video_file'
msg_info['mime_type'] = message.video.mime_type
msg_info['duration_seconds'] = message.video.duration
msg_info['width'] = message.video.width
msg_info['height'] = message.video.height
async def get_video_note_data(
message: Message,
msg_info: dict,
names: tuple
):
vnote_path, thumb_path, vnote_relative_path, thumb_relative_path = names
try:
await app.download_media(
message.video_note.file_id,
vnote_path
)
msg_info['file'] = vnote_relative_path
except ValueError:
print("Oops can't download media!")
msg_info['file'] = FILE_NOT_FOUND
if message.video_note.thumbs is not None:
try:
await app.download_media(
message.video_note.thumbs[0].file_id,
thumb_path
)
msg_info['thumbnail'] = thumb_relative_path
except ValueError:
print("Oops can't download media!")
msg_info['thumbnail'] = FILE_NOT_FOUND
else:
msg_info['thumbnail'] = FILE_NOT_FOUND
msg_info['media_type'] = 'video_message'
msg_info['mime_type'] = message.video_note.mime_type
msg_info['duration_seconds'] = message.video_note.duration
async def get_audio_data(
message: Message,
msg_info: dict,
names: tuple
) -> None:
audio_path, thumb_path, audio_relative_path, thumb_relative_path = names
try:
await app.download_media(
message.audio.file_id,
audio_path
)
msg_info['file'] = audio_relative_path
except ValueError:
print("Oops can't download media!")
msg_info['file'] = FILE_NOT_FOUND
if message.audio.thumbs is not None:
try:
await app.download_media(
message.audio.thumbs[0].file_id,
thumb_path
)
msg_info['thumbnail'] = thumb_relative_path
except ValueError:
print("Oops can't download media!")
msg_info['thumbnail'] = FILE_NOT_FOUND
else:
msg_info['thumbnail'] = FILE_NOT_FOUND
msg_info['media_type'] = 'audio_file'
msg_info['performer'] = message.audio.performer
msg_info['title'] = message.audio.title
msg_info['mime_type'] = message.audio.mime_type
msg_info['duration_seconds'] = message.audio.duration
async def get_voice_data(
message: Message,
msg_info: dict,
names: tuple
) -> None:
voice_path, voice_relative_path = names
try:
await app.download_media(
message.voice.file_id,
voice_path
)
msg_info['file'] = voice_relative_path
except ValueError:
print("Oops can't download media!")
msg_info['file'] = FILE_NOT_FOUND
msg_info['media_type'] = 'voice_message'
msg_info['mime_type'] = message.voice.mime_type
msg_info['duration_seconds'] = message.voice.duration
async def get_document_data(
message: Message,
msg_info: dict,
names: tuple
) -> None:
doc_path, thumb_path, doc_relative_path, thumb_relative_path = names
try:
await app.download_media(
message.document.file_id,
doc_path
)
msg_info['file'] = doc_relative_path
except ValueError:
print("Oops can't download media!")
msg_info['file'] = FILE_NOT_FOUND
if message.document.thumbs is not None:
try:
await app.download_media(
message.document.thumbs[0].file_id,
thumb_path
)
msg_info['thumbnail'] = thumb_relative_path
except ValueError:
print("Oops can't download media!")
msg_info['thumbnail'] = FILE_NOT_FOUND
else:
msg_info['thumbnail'] = FILE_NOT_FOUND
msg_info['mime_type'] = message.document.mime_type
def get_text_data(message: Message, text_mode: str) -> list:
text = []
if text_mode == 'caption':
if message.caption_entities is not None:
entities = message.caption_entities
else:
return text
elif text_mode == 'text':
if message.text.entities is not None:
entities = message.text.entities
else:
return text
for e in entities:
txt = {}
match e.type:
case MessageEntityType.URL:
txt['type'] = 'link'
case MessageEntityType.HASHTAG:
txt['type'] = 'hashtag'
case MessageEntityType.CASHTAG:
txt['type'] = 'cashtag'
case MessageEntityType.BOT_COMMAND:
txt['type'] = 'bot_command'
case MessageEntityType.MENTION:
txt['type'] = 'mention'
case MessageEntityType.EMAIL:
txt['type'] = 'email'
case MessageEntityType.PHONE_NUMBER:
txt['type'] = 'phone_number'
case MessageEntityType.BOLD:
txt['type'] = 'bold'
case MessageEntityType.ITALIC:
txt['type'] = 'italic'
case MessageEntityType.UNDERLINE:
txt['type'] = 'underline'
case MessageEntityType.STRIKETHROUGH:
txt['type'] = 'strikethrough'
case MessageEntityType.SPOILER:
txt['type'] = 'spoiler'
case MessageEntityType.CODE:
txt['type'] = 'code'
case MessageEntityType.PRE:
txt['type'] = 'pre'
txt['language'] = ''
case MessageEntityType.BLOCKQUOTE:
txt['type'] = 'blockquote'
case MessageEntityType.TEXT_LINK:
txt['type'] = 'text_link'
txt['href'] = e.url
case MessageEntityType.TEXT_MENTION:
txt['type'] = 'text_mention'
case MessageEntityType.BANK_CARD:
txt['type'] = 'bank_card'
case MessageEntityType.CUSTOM_EMOJI:
txt['type'] = 'custom_emoji'
case _:
txt['type'] = 'unknown'
if text_mode == 'text':
txt['text'] = message.text[e.offset:e.offset + e.length]
else:
txt['text'] = message.caption[e.offset:e.offset + e.length]
text.append(txt)
return text
# TODO: fix better typing
def get_contact_data(
message: Message,
msg_info: dict,
names: tuple
) -> list:
contact_data = {'phone_number': message.contact.phone_number}
contact_data['fist_name'] = message.contact.first_name if message.contact.first_name is not None else ''
contact_data['last_name'] = message.contact.last_name if message.contact.last_name is not None else ''
msg_info['contact_information'] = contact_data
if MEDIA_EXPORT['contacts'] is True:
vcard_path, vcard_relative_path = names
msg_info['contact_vcard'] = vcard_relative_path
# convert to vcard
vcard = (
'BEGIN:VCARD\n'
'VERSION:3.0\n'
f'FN;CHARSET=UTF-8:{message.contact.first_name} {message.contact.last_name}\n'
f'N;CHARSET=UTF-8:{message.contact.last_name};{message.contact.first_name};;;\n'
f'TEL;TYPE=CELL:{message.contact.phone_number}\n'
'END:VCARD\n'
)
with open(vcard_path, 'w') as f:
f.write(vcard)
else:
msg_info['contact_vcard'] = FILE_NOT_FOUND
def get_photo_name(
message: Message,
username: str,
media_num: int = None
) -> tuple:
chat_export_date = datetime.now().strftime("%Y-%m-%d")
chat_export_name = f'ChatExport_{username}_{chat_export_date}'
# TODO: when user want other path
path = ''
media_dir = f'{chat_export_name}/photos'
os.makedirs(media_dir, exist_ok=True)
date = message.date.strftime('%d-%m-%Y_%H-%M-%S')
# TODO: what format for png??
photo_name = f'file_{media_num}@{date}.jpg'
photo_path = f'{chat_export_name}/photos/{photo_name}'
photo_relative_path = f'photos/{photo_name}'
result = photo_path, photo_relative_path
return result
def get_video_name(
message: Message,
username: str,
media_num: int = None
) -> tuple:
chat_export_date = datetime.now().strftime("%Y-%m-%d")
chat_export_name = f'ChatExport_{username}_{chat_export_date}'
# TODO: when user want other path
path = ''
media_dir = f'{chat_export_name}/video_files'
os.makedirs(media_dir, exist_ok=True)
# TODO: gen better way random str
video_name = str(uuid.uuid4()) if message.video.file_name is None else message.video.file_name
video_path = f'{media_dir}/{video_name}'
if message.video.thumbs is not None:
thumb_name = f'{video_name}_thumb.jpg'
thumb_path = f'{media_dir}/{thumb_name}'
else:
thumb_name = None
thumb_path = None
video_relative_path = f'video_files/{video_name}'
thumb_relative_path = None if thumb_name is None else f'video_files/{thumb_name}'
result = (
video_path,
thumb_path,
video_relative_path,
thumb_relative_path
)
return result
def get_voice_name(
message: Message,
username: str,
media_num: int = None
) -> tuple:
chat_export_date = datetime.now().strftime("%Y-%m-%d")
chat_export_name = f'ChatExport_{username}_{chat_export_date}'
# TODO: when user want other path
path = ''
media_dir = f'{chat_export_name}/voice_messages'
os.makedirs(media_dir, exist_ok=True)
date = message.date.strftime('%d-%m-%Y_%H-%M-%S')
voice_name = f'audio_{media_num}@{date}.ogg'
voice_path = f'{media_dir}/{voice_name}'
voice_relative_path = f'voice_messages/{voice_name}'
result = voice_path, voice_relative_path
return result
def get_video_note_name(
message: Message,
username: str,
media_num: int = None
) -> tuple:
chat_export_date = datetime.now().strftime("%Y-%m-%d")
chat_export_name = f'ChatExport_{username}_{chat_export_date}'
# TODO: when user want other path
path = ''
media_dir = f'{chat_export_name}/round_video_messages'
os.makedirs(media_dir, exist_ok=True)
# TODO: by default don't have name
date = message.date.strftime('%d-%m-%Y_%H-%M-%S')
vnote_name = f'file_{media_num}@{date}.mp4'
vnote_path = f'{media_dir}/{vnote_name}'
if message.video_note.thumbs is not None:
thumb_name = f'{vnote_name}_thumb.jpg'
thumb_path = f'{media_dir}/{thumb_name}'
else:
thumb_name = None
thumb_path = None
vnote_relative_path = f'round_video_messages/{vnote_name}'
thumb_relative_path = None if thumb_name is None else f'round_video_messages/{thumb_name}'
result = (
vnote_path,
thumb_path,
vnote_relative_path,
thumb_relative_path
)
return result
def get_sticker_name(
message: Message,
username: str,
media_num: int = None
) -> tuple:
chat_export_date = datetime.now().strftime("%Y-%m-%d")
chat_export_name = f'ChatExport_{username}_{chat_export_date}'
# TODO: when user want other path
path = ''
media_dir = f'{chat_export_name}/stickers'
os.makedirs(media_dir, exist_ok=True)
# TODO: gen better way random str
sticker_name = str(uuid.uuid4()) if message.sticker.file_name is None else message.sticker.file_name
sticker_path = f'{media_dir}/{sticker_name}'
if message.sticker.thumbs is not None:
thumb_name = f'{sticker_name}_thumb.jpg'
thumb_path = f'{media_dir}/{thumb_name}'
else:
thumb_name = None
thumb_path = None
sticker_relative_path = f'stickers/{sticker_name}'
thumb_relative_path = None if thumb_name is None else f'stickers/{thumb_name}'
result = (
sticker_path,
thumb_path,
sticker_relative_path,
thumb_relative_path
)
return result
def get_animation_name(
message: Message,
username: str,
media_num: int = None
) -> tuple:
chat_export_date = datetime.now().strftime("%Y-%m-%d")
chat_export_name = f'ChatExport_{username}_{chat_export_date}'
# TODO: when user want other path
path = ''
media_dir = f'{chat_export_name}/video_files'
os.makedirs(media_dir, exist_ok=True)
# TODO: gen better way random str
animation_name = str(uuid.uuid4()) if message.animation.file_name is None else message.animation.file_name
animation_path = f'{media_dir}/{animation_name}'
if message.animation.thumbs is not None:
thumb_name = f'{animation_name}_thumb.jpg'
thumb_path = f'{media_dir}/{thumb_name}'
else:
thumb_name = None
thumb_path = None
animation_relative_path = f'video_files/{animation_name}'
thumb_relative_path = None if thumb_name is None else f'video_files/{thumb_name}'
result = (
animation_path,
thumb_path,
animation_relative_path,
thumb_relative_path
)
return result
def get_audio_name(
message: Message,
username: str,
media_num: int = None
) -> tuple:
chat_export_date = datetime.now().strftime("%Y-%m-%d")
chat_export_name = f'ChatExport_{username}_{chat_export_date}'
# TODO: when user want other path
path = ''
media_dir = f'{chat_export_name}/files'
os.makedirs(media_dir, exist_ok=True)
# TODO: gen better way random str
audio_name = str(uuid.uuid4()) if message.audio.file_name is None else message.audio.file_name
audio_path = f'{media_dir}/{audio_name}'
if message.audio.thumbs is not None:
thumb_name = f'{audio_name}_thumb.jpg'
thumb_path = f'{media_dir}/{thumb_name}'
else:
thumb_name = None
thumb_path = None
audio_relative_path = f'files/{audio_name}'
thumb_relative_path = None if thumb_name is None else f'files/{thumb_name}'
result = audio_path, thumb_path, audio_relative_path, thumb_relative_path
return result
def get_document_name(
message: Message,
username: str,
media_num: int = None
) -> tuple:
chat_export_date = datetime.now().strftime("%Y-%m-%d")
chat_export_name = f'ChatExport_{username}_{chat_export_date}'
# TODO: when user want other path
path = ''
media_dir = f'{chat_export_name}/files'
os.makedirs(media_dir, exist_ok=True)
# TODO: gen better way random str
doc_name = str(uuid.uuid4()) if message.document.file_name is None else message.document.file_name
doc_path = f'{media_dir}/{doc_name}'
if message.document.thumbs is not None:
thumb_name = f'{doc_name}_thumb.jpg'
thumb_path = f'{media_dir}/{thumb_name}'
else:
thumb_name = None
thumb_path = None
doc_relative_path = f'files/{doc_name}'
thumb_relative_path = None if thumb_name is None else f'files/{thumb_name}'
result = doc_path, thumb_path, doc_relative_path, thumb_relative_path
return result
def get_contact_name(
username: str,
media_num: int,
) -> tuple:
chat_export_date = datetime.now().strftime("%Y-%m-%d")
chat_export_name = f'ChatExport_{username}_{chat_export_date}'
# TODO: when user want other path
path = ''
media_dir = f'{chat_export_name}/contacts'
os.makedirs(media_dir, exist_ok=True)
contact_name = f'contact_{media_num}.vcf'
contact_path = f'{media_dir}/{contact_name}'
contact_relative_path = f'contacts/{contact_name}'
return contact_path, contact_relative_path
def convert_to_unixtime(date: datetime):
# telegram date format: "2022-07-10 08:49:23"
unix_time = int(time.mktime(date.timetuple()))
return unix_time
def to_html():
pass
def split_json_file(data: dict, output_path: str, page_size: int = JSON_FILE_PAGE_SIZE):
messages = data["messages"]
page = 1
current_data = {"messages": []}
for msg in messages:
current_data["messages"].append(msg)
serialized_data = json.dumps(current_data, default=str)
if len(serialized_data.encode('utf-8')) > page_size:
# Remove the last message that made it too big
current_data["messages"].pop()
# Save current part
with open(output_path.replace("result.json", f"result_part{page}.json"), "w") as outf:
json.dump(current_data, outf, indent=4, default=str)
# Start new part with the removed message
page += 1
current_data = {"messages": [msg]}
# Save last part
with open(output_path.replace("result.json", f"result_part{page}.json"), "w") as outf:
json.dump(current_data, outf, indent=4, default=str)
async def main():
async with app:
if not CHAT_IDS:
all_dialogs_id = await Chat(app).get_ids()
CHAT_IDS.extend(all_dialogs_id)
archive = Archive(CHAT_IDS)
for cid in CHAT_IDS:
# when use telegram api, channels id have -100 prefix
if type(cid) == int and not str(cid).startswith('-100'):
new_id = int(f'-100{cid}')
CHAT_IDS[CHAT_IDS.index(cid)] = new_id
chat = await app.get_chat(cid)
archive.fill_chat_data(chat)
# TODO: add exception for private channels
# read messages from first to last
all_messages = [m async for m in app.get_chat_history(cid)]
all_messages.reverse()
for message in all_messages:
# TODO: add initial message of channel
msg_info = {}
await archive.process_message(chat, message, msg_info)
json_name = generate_json_name(username=archive.username)
if not JSON_FILE_PAGE_SIZE:
with open(json_name, mode='w') as f:
json.dump(archive.chat_data, f, indent=4, default=str)
else:
split_json_file(archive.chat_data, json_name, JSON_FILE_PAGE_SIZE)
app = Client(
"my_bot",
api_id=API_ID,
api_hash=API_HASH,
)
app.run(main())