This repository has been archived by the owner on Mar 2, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 119
/
deez_bot.py
executable file
·891 lines (704 loc) · 18.3 KB
/
deez_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
#!/usr/bin/python3
from utils.utils import check_config_bot, show_menu
check_config_bot()
mode_bot = show_menu()
from configs.set_configs import SetConfigs
SetConfigs(mode_bot)
from sys import argv
from hashlib import md5
#from signal import SIGTERM
#from os import getpid, kill
from time import sleep, time
from telegram import ParseMode
from helpers.download_help import DW
from pyrogram import idle as tg_user_start
from utils.special_thread import magicThread
from utils.converter_bytes import convert_bytes_to
from telegram.error import BadRequest, Unauthorized
from utils.utils_data import create_response_article, shazam_song
from utils.utils_graphs import (
create_graph_users, create_graph_top_downloaders, get_data_downloaders
)
from telegram import (
MessageEntity, Update, InputMediaPhoto
)
from helpers.db_help import (
delete_banned, select_all_users, write_banned
)
from configs.customs import (
not_found_query_gif, shazam_audio_query,
shazam_function_msg, max_download_user_msg,
help_msg, help_photo, feedback_text, what_can_I_do,
donate_text, reasons_text, startup_text
)
from configs.bot_settings import (
download_dir_max_size, time_sleep,
output_shazam, recorded_file_max_size,
root_ids, bunker_channel,
owl_channel, max_download_user
)
from utils.utils_users_bot import (
user_setting_save_db, users_set_cache,
check_flood, get_banned_ids,
get_info, kill_threads
)
from utils.utils import (
is_supported_link, get_download_dir_size,
clear_download_dir, clear_recorded_dir,
my_round, create_tmux
)
from telegram.ext import (
CommandHandler, MessageHandler, Filters,
InlineQueryHandler, CallbackQueryHandler
)
from inlines.inline_keyboards import (
create_keyboad_search, create_keyboard_settings,
create_keyboard_qualities, create_shazamed_keyboard,
create_keyboard_search_method, create_banned_keyboard,
create_c_dws_user_keyboard, create_info_keyboard
)
bot_chat_id = SetConfigs.tg_bot_api.bot.id
bot = SetConfigs.tg_bot_api.bot
banned_ids = get_banned_ids()
users_data = {}
roots_data = {}
to_ban = Filters.user(banned_ids)
strict_modes = [1, 3]
def ban_chat_id(chat_id):
write_banned(chat_id)
to_ban.add_chat_ids(chat_id)
def help_check_user(chat_id, date = None):
if (
(mode_bot in strict_modes) and
(not chat_id in root_ids)
):
bot.send_message(
chat_id = chat_id,
text = "BOT IS UNDER MAINTENANCE"
)
to_ban.add_chat_ids(chat_id)
return
user_exist = users_set_cache(chat_id, users_data)
if date:
user_data = users_data[chat_id]
result = check_flood(date, user_data, chat_id)
if result:
msg, mode = result
bot.send_message(
chat_id = chat_id,
text = msg
)
if mode == 1:
to_ban.add_chat_ids(chat_id)
del users_data[chat_id]
if not user_exist:
bot.send_message(
chat_id = chat_id,
text = startup_text,
parse_mode = ParseMode.MARKDOWN
)
def help_download(link, chat_id):
if chat_id in to_ban.user_ids:
return
if not is_supported_link(link):
text = "THIS LINK IS NOT SUPPORTED :("
bot.send_message(
chat_id = chat_id,
text = text
)
return
c_user_data = users_data[chat_id]
c_downloads = c_user_data['c_downloads']
if len(c_downloads) == max_download_user:
bot.send_message(
chat_id = chat_id,
text = max_download_user_msg
)
return
to_hash = f"{link}{c_user_data['quality']}{chat_id}{time()}".encode()
hash_link = md5(to_hash).hexdigest()
new_ins = DW(chat_id, c_user_data, hash_link)
t = magicThread(
target = new_ins.download,
args = (link, )
)
c_downloads[hash_link] = {
"link": link,
"thread": t,
"dw": new_ins
}
t.start()
def handle_inline_queries(update: Update, context):
inline_query = update.inline_query
chat_id = inline_query.from_user.id
if chat_id in to_ban.user_ids:
return
help_check_user(chat_id)
query_id = inline_query.id
query = inline_query.query
c_user_data = users_data[chat_id]
results = create_response_article(query, c_user_data)
try:
bot.answer_inline_query(
inline_query_id = query_id,
results = results
)
except BadRequest:
pass
def handle_callback_queries(update: Update, context):
callback_query = update.callback_query
chat_id = callback_query.from_user.id
if chat_id in to_ban.user_ids:
return
msg = callback_query.message
date = msg.date
help_check_user(chat_id, date)
msg_id = msg.message_id
query_id = callback_query.id
data = callback_query.data
c_user_data = users_data[chat_id]
text = "Your settings"
answer = "DONE"
mode = 0
if data == "/edit_setting_quality":
text = "Qualities"
c_keyboard = create_keyboard_qualities()
elif data == "/edit_setting_search_method":
text = "Search methods"
c_keyboard = create_keyboard_search_method()
elif data == "/edit_setting_zips":
zips = c_user_data['zips']
if zips:
c_user_data['zips'] = False
else:
c_user_data['zips'] = True
c_keyboard = create_keyboard_settings(c_user_data)
user_setting_save_db(chat_id, c_user_data)
elif data == "/edit_setting_tracks":
tracks = c_user_data['tracks']
if tracks:
c_user_data['tracks'] = False
else:
c_user_data['tracks'] = True
c_keyboard = create_keyboard_settings(c_user_data)
user_setting_save_db(chat_id, c_user_data)
elif data == "/edit_setting_source":
source = c_user_data['source']
if source == "SpoDee":
c_user_data['source'] = "Dee"
else:
c_user_data['source'] = "SpoDee"
c_keyboard = create_keyboard_settings(c_user_data)
user_setting_save_db(chat_id, c_user_data)
elif data.startswith("/unban_"):
c_data = data.replace("/unban_", "")
c_chat_id = int(c_data)
text = "BANNED USERS"
delete_banned(c_chat_id)
to_ban.remove_chat_ids(c_chat_id)
c_keyboard = create_banned_keyboard()
answer = f"UNBANNED {c_chat_id}"
elif data.startswith("/edit_setting_quality_"):
c_data = data.replace("/edit_setting_quality_", "")
c_user_data['quality'] = c_data
c_keyboard = create_keyboard_settings(c_user_data)
user_setting_save_db(chat_id, c_user_data)
elif data.startswith("/edit_setting_search_method_"):
c_data = data.replace("/edit_setting_search_method_", "")
c_user_data['search_method'] = c_data
c_keyboard = create_keyboard_settings(c_user_data)
user_setting_save_db(chat_id, c_user_data)
elif data.startswith("/down:"):
mode = "down:"
answer = "GOOD CHOICE :)"
c_data = data.replace("/down:", "")
elif data == "/back_home":
c_keyboard = create_keyboard_settings(c_user_data)
elif data.startswith("/kill_dw_"):
mode = "kill"
answer = "DOWNLOAD DELETED :)"
c_hash = data.replace("/kill_dw_", "")
c_dws = c_user_data['c_downloads']
if not c_hash in c_dws:
answer = "THIS IS AN OLD DOWNLOAD :)"
else:
t = c_dws[c_hash]['thread']
t.kill()
del c_dws[c_hash]
else:
mode = 1
answer = "Sorry isn't avalaible :("
if mode == 0:
bot.edit_message_text(
chat_id = chat_id,
message_id = msg_id,
text = text,
reply_markup = c_keyboard
)
bot.answer_callback_query(
callback_query_id = query_id,
text = answer
)
if mode == "kill":
bot.delete_message(
chat_id = chat_id,
message_id = msg_id
)
elif mode == "down:":
help_download(c_data, chat_id)
def audio_handler(update: Update, context):
msg = update.message
chat_id = msg.from_user.id
msg_id = msg.message_id
date = msg.date
help_check_user(chat_id, date)
audio = msg.audio
if not audio:
audio = msg.voice
audio_size = convert_bytes_to(audio.file_size, "mb")
if audio_size > recorded_file_max_size:
bot.send_message(
chat_id = chat_id,
text = f"PLEASE SEND A FILE AUDIO/RECORD LOWER THAN {recorded_file_max_size} MB =)"
)
return
file_id = audio.file_id
c_file = bot.get_file(file_id)
try:
SetConfigs.queues_started += 1
out = c_file.download(
custom_path = f"{output_shazam}{file_id}"
)
resp = shazam_song(out)
finally:
SetConfigs.queues_finished += 1
if not resp:
bot.send_message(
chat_id = chat_id,
text = "I CANNOT SHAZAM THE SONG RETRY :("
)
return
artist, genre,\
album, label,\
track_link, album_link,\
artist_link, image_url,\
release_date, title = resp
if track_link:
reply_markup = create_shazamed_keyboard(track_link, album_link, artist_link)
else:
reply_markup = None
image_url = not_found_query_gif
bot.send_photo(
chat_id = chat_id,
photo = image_url,
reply_to_message_id = msg_id,
reply_markup = reply_markup,
caption = (
shazam_audio_query
% (
artist,
genre,
album,
label,
release_date,
title
)
)
)
def start_command(update: Update, context):
msg = update.message
chat_id = msg.from_user.id
date = msg.date
help_check_user(chat_id, date)
bot.send_message(
chat_id = chat_id,
text = "YOU JUST PRESSED START YOU WON A NICE DAY :)))",
reply_markup = create_keyboad_search("")
)
def settings_command(update: Update, context):
msg = update.message
chat_id = msg.from_user.id
date = msg.date
help_check_user(chat_id, date)
c_user_data = users_data[chat_id]
bot.send_message(
chat_id = chat_id,
text = "Your settings",
reply_markup = create_keyboard_settings(c_user_data)
)
def quality_command(update: Update, context):
msg = update.message
chat_id = msg.from_user.id
date = msg.date
help_check_user(chat_id, date)
c_user_data = users_data[chat_id]
bot.send_message(
chat_id = chat_id,
text = f"Current quality {c_user_data['quality']}",
reply_markup = create_keyboard_qualities()
)
def managing_banned_command(update: Update, context):
msg = update.message
chat_id = msg.from_user.id
bot.send_message(
chat_id = chat_id,
text = "Banned users",
reply_markup = create_banned_keyboard()
)
def add_banned_command(update: Update, context):
msg = update.message
chat_id = msg.from_user.id
if not chat_id in roots_data:
roots_data[chat_id] = {
"stage": None
}
bot.send_message(
chat_id = chat_id,
text = "Send me the user to ban"
)
roots_data[chat_id]['stage'] = "add_banned"
def send_global_msg_command(update: Update, context):
msg = update.channel_post
text = msg.text
audio = msg.audio
document = msg.document
animation = msg.animation
sticker = msg.sticker
video = msg.video
photo = msg.photo
to_send = None
method = None
if text:
to_send = text
method = bot.send_message
elif audio:
to_send = audio.file_id
method = bot.send_audio
elif document:
to_send = document.file_id
method = bot.send_document
elif animation:
to_send = animation.file_id
method = bot.send_animation
elif sticker:
to_send = sticker.file_id
method = bot.send_sticker
elif video:
to_send = video.file_id
method = bot.send_video
elif photo:
to_send = photo[0].file_id
method = bot.send_photo
def broadcast():
all_user = select_all_users()
for user_id in all_user:
c_user_id = user_id[0]
try:
method(c_user_id, to_send)
except (BadRequest, Unauthorized):
pass
magicThread(target = broadcast).start()
def shazam_command(update: Update, context):
msg = update.message
chat_id = msg.from_user.id
date = msg.date
help_check_user(chat_id, date)
bot.send_message(
chat_id = chat_id,
text = shazam_function_msg
)
def kill_dw_command(update: Update, context):
msg = update.message
chat_id = msg.from_user.id
date = msg.date
help_check_user(chat_id, date)
c_user_data = users_data[chat_id]
c_downloads = c_user_data['c_downloads']
if not c_downloads:
bot.send_message(
chat_id = chat_id,
text = "There aren't any download in progress"
)
return
keybords_dws = create_c_dws_user_keyboard(c_downloads)
for c_download, keyboard in zip(
c_downloads.values(), keybords_dws
):
c_text = f"{c_download['link']} in {c_user_data['quality']}"
bot.send_message(
chat_id = chat_id,
text = c_text,
reply_markup = keyboard
)
def info_command(update: Update, context):
msg = update.message
chat_id = msg.from_user.id
date = msg.date
help_check_user(chat_id, date)
users_graph = create_graph_users()
bot.send_photo(
chat_id = chat_id,
photo = users_graph,
caption = get_info(),
reply_markup = create_info_keyboard()
)
def reasons_command(update: Update, context):
msg = update.message
chat_id = msg.from_user.id
date = msg.date
help_check_user(chat_id, date)
bot.send_message(
chat_id = chat_id,
text = reasons_text
)
def help_command(update: Update, context):
msg = update.message
chat_id = msg.from_user.id
date = msg.date
help_check_user(chat_id, date)
bot.send_photo(
chat_id = chat_id,
photo = help_photo,
caption = help_msg
)
bot.send_message(
chat_id = chat_id,
text = what_can_I_do
)
def feedback_command(update: Update, context):
msg = update.message
chat_id = msg.from_user.id
date = msg.date
help_check_user(chat_id, date)
bot.send_message(
chat_id = chat_id,
text = feedback_text
)
def donate_command(update: Update, context):
msg = update.message
chat_id = msg.from_user.id
date = msg.date
help_check_user(chat_id, date)
bot.send_message(
chat_id = chat_id,
text = donate_text,
reply_markup = create_info_keyboard()
)
def graphs_command(update: Update, context):
msg = update.message
chat_id = msg.from_user.id
date = msg.date
help_check_user(chat_id, date)
downloads_graph = create_graph_top_downloaders()
users_graph = create_graph_users()
users, nums_downloads = get_data_downloaders()
caption_text_download_graph = ""
times = 1
for user, num_downloads in zip(users, nums_downloads):
caption_text_download_graph += f"{times} Place: User `{user}` with {num_downloads} downloads\n"
times += 1
medias = [
InputMediaPhoto(
media = downloads_graph,
caption = caption_text_download_graph,
parse_mode = ParseMode.MARKDOWN_V2
),
InputMediaPhoto(
media = users_graph
)
]
bot.send_media_group(
chat_id = chat_id,
media = medias
)
def msgs_handler(update: Update, context):
msg = update.message
chat_id = msg.from_user.id
text = msg.text
if chat_id in roots_data:
if roots_data[chat_id]['stage'] == "add_banned":
ban_chat_id(text)
roots_data[chat_id]['stage'] = None
bot.send_message(
chat_id = chat_id,
text = f"User {text} has been blocked"
)
return
date = msg.date
help_check_user(chat_id, date)
bot.send_message(
chat_id = chat_id,
text = text,
reply_markup = create_keyboad_search(text)
)
def controls_links(update: Update, context):
msg = update.message
chat_id = msg.from_user.id
date = msg.date
help_check_user(chat_id, date)
entity_link = msg.entities[0]
link = msg.parse_entity(entity_link)
help_download(link, chat_id)
dispatcher = SetConfigs.tg_bot_api.dispatcher
start_handler = CommandHandler(
"start",
start_command,
filters = ~ to_ban, run_async = True
)
dispatcher.add_handler(start_handler)
settings_handler = CommandHandler(
"settings",
settings_command,
filters = ~ to_ban, run_async = True
)
dispatcher.add_handler(settings_handler)
quality_handler = CommandHandler(
"quality",
quality_command,
filters = ~ to_ban, run_async = True
)
dispatcher.add_handler(quality_handler)
to_root = Filters.user(root_ids)
managing_banned_handler = CommandHandler(
"managing_banned",
managing_banned_command,
filters = to_root, run_async = True
)
dispatcher.add_handler(managing_banned_handler)
add_banned_handler = CommandHandler(
"add_banned",
add_banned_command,
filters = to_root, run_async = True
)
dispatcher.add_handler(add_banned_handler)
shazam_handler = CommandHandler(
"shazam",
shazam_command,
filters = ~ to_ban, run_async = True
)
dispatcher.add_handler(shazam_handler)
kill_dw_handler = CommandHandler(
"kill_dw",
kill_dw_command,
filters = ~ to_ban, run_async = True
)
dispatcher.add_handler(kill_dw_handler)
info_handler = CommandHandler(
"info",
info_command,
filters = ~ to_ban, run_async = True
)
dispatcher.add_handler(info_handler)
reasons_handler = CommandHandler(
"reasons",
reasons_command,
filters = ~ to_ban, run_async = True
)
dispatcher.add_handler(reasons_handler)
help_handler = CommandHandler(
"help",
help_command,
filters = ~ to_ban, run_async = True
)
dispatcher.add_handler(help_handler)
feedback_handler = CommandHandler(
"feedback",
feedback_command,
filters = ~ to_ban, run_async = True
)
dispatcher.add_handler(feedback_handler)
donate_handler = CommandHandler(
"donate",
donate_command,
filters = ~ to_ban, run_async = True
)
dispatcher.add_handler(donate_handler)
graphs_handler = CommandHandler(
"graphs",
graphs_command,
filters = ~ to_ban, run_async = True
)
dispatcher.add_handler(graphs_handler)
filter_owl_channel = Filters.chat(owl_channel)
send_global_msg_handler = MessageHandler(
filter_owl_channel,
send_global_msg_command,
run_async = True
)
dispatcher.add_handler(send_global_msg_handler)
filter_url = Filters.entity(
MessageEntity.URL
)
control_links = MessageHandler(
(
filter_url &
~ to_ban
), controls_links, run_async = True
)
dispatcher.add_handler(control_links)
filter_text = Filters.text
msgs = MessageHandler(
(
filter_text &
~ to_ban
), msgs_handler, run_async = True
)
dispatcher.add_handler(msgs)
filter_shazam = (Filters.voice | Filters.audio)
filter_bunker_channel = Filters.chat(bunker_channel)
filter_no_bot = Filters.via_bot(bot_chat_id)
audio_msgs = MessageHandler(
(
filter_shazam &
~ filter_no_bot &
~ to_ban &
~ filter_bunker_channel
), audio_handler, run_async = True
)
dispatcher.add_handler(audio_msgs)
inline_queries = InlineQueryHandler(
handle_inline_queries, run_async = True
)
dispatcher.add_handler(inline_queries)
callback_queries = CallbackQueryHandler(
handle_callback_queries, run_async = True
)
dispatcher.add_handler(callback_queries)
SetConfigs.tg_bot_api.start_polling()
def checking():
while True:
sleep(time_sleep)
dir_size = my_round(
get_download_dir_size()
)
print(
f"STATUS DOWNLOADS {SetConfigs.queues_started}/{SetConfigs.queues_finished} {dir_size}/{download_dir_max_size}"
)
if (
dir_size >= download_dir_max_size
) or (
SetConfigs.queues_started == SetConfigs.queues_finished
):
SetConfigs.tg_bot_api.stop()
kill_threads(users_data)
sleep(3)
SetConfigs.queues_started = 0
SetConfigs.queues_finished = 0
clear_download_dir()
clear_recorded_dir()
SetConfigs.tg_bot_api.start_polling()
if len(argv) == 1:
tmux_session = None
else:
tmux_session = create_tmux()
check_thread = magicThread(target = checking)
check_thread.start()
tg_user_start()
print("\nEXITTING WAIT A FEW SECONDS :)")
clear_download_dir()
clear_recorded_dir()
SetConfigs.tg_bot_api.stop()
check_thread.kill()
kill_threads(users_data)
if tmux_session:
tmux_session.kill_session()