forked from Bfaschat/APKFetcher
/
server.py
828 lines (696 loc) · 29 KB
/
server.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
import time
import logging
logging.basicConfig(level=logging.DEBUG,
format='%(asctime)s - %(name)s - %(levelname)s - %(message)s')
logger = logging.getLogger(__name__)
from bs4 import BeautifulSoup
#from pyaxmlparser import APK
from shutil import copyfile
import subprocess
import math
import requests
from requests import exceptions
import sys, os, re, sys, io
import warnings, random
from random import randint
from hachoir.metadata import extractMetadata
from hachoir.parser import createParser
from pyrogram import Client, Filters, ReplyKeyboardMarkup, InlineKeyboardMarkup, InlineKeyboardButton, ReplyKeyboardRemove, ForceReply
from contextlib import redirect_stdout
from translation import Translation
from clint.textui import progress
import traceback
active_chats = {}
import os
class Config(object):
DOWNLOAD_LOCATION = "./DOWNLOADS"
# Telegram maximum file upload size
MAX_FILE_SIZE = 50000000
TG_MAX_FILE_SIZE = 14000000000
# chunk size that should be used with requests
CHUNK_SIZE = 128
# default thumbnail to be used in the videos
DEF_THUMB_NAIL_VID_S = "https://placehold.it/90x90"
app = Client(
os.environ.get("TOKEN"),
api_id=os.environ["APP_ID"],
api_hash=os.environ["API_HASH"])
from pyrogram.api.errors import (
BadRequest, Flood, InternalServerError,
SeeOther, Unauthorized, UnknownError
)
import sys
try:
from urllib.parse import quote_plus
import urllib.request
python3 = True
except ImportError:
from urllib import quote_plus
import urllib2
python3 = False
import traceback
from pyrogram import Client, Filters
import requests
import threading
import io
import urllib
import subprocess
import traceback
def exec_thread(target, *args, **kwargs):
t = threading.Thread(target=target, args=args, kwargs=kwargs)
t.daemon = True
t.start()
from hurry.filesize import size, alternative
import warnings
warnings.filterwarnings("ignore", category=UserWarning, module='bs4')
def prog(client, current, total, message_id, chat_id, required_file_name):
if round(current/total*100, 0) % 5 == 0:
try:
file_size = os.stat(required_file_name).st_size
client.send_chat_action(chat_id,'UPLOAD_DOCUMENT')
client.edit_message_text(
chat_id,
message_id,
text = "{}% of {}".format(round(current/total*100, 0), str(pretty_size(file_size)))
)
except:
pass
def progs(client, current, total, message_id, chat_id):
if round(current/total*100, 0) % 5 == 0:
try:
client.edit_message_text(
chat_id,
message_id,
text = "**⬇️ Download Progress:** `{}%`".format(round(current/total*100, 0))
)
except:
pass
def shuffle(word):
wordlen = len(word)
word = list(word)
for i in range(0,wordlen-1):
pos = randint(i+1,wordlen-1)
word[i], word[pos] = word[pos], word[i]
word = "".join(word)
return word
def pretty_size(sizes):
units = ['B', 'KB', 'MB', 'GB']
unit = 0
while sizes >= 1024:
sizes /= 1024
unit += 1
return '%0.2f %s' % (sizes, units[unit])
def dosomething(buf):
"""Do something with the content of a file"""
sleep(0.01)
pass
def get_filename_from_cd(cd):
"""
Get filename from content-disposition
"""
if not cd:
return None
fname = re.findall('filename=(.+)', cd)
if len(fname) == 0:
return None
return fname[0]
def dynamic_data(data):
return Filters.create(
name="DynamicData",
func=lambda filter, callback_query: filter.data == callback_query.data,
data=data # "data" kwarg is accessed with "filter.data"
)
def DownLoadFile(url, file_name):
if not os.path.exists(file_name):
r = requests.get(url, allow_redirects=True, stream=True)
with open(file_name, 'wb') as fd:
for chunk in r.iter_content(chunk_size=Config.CHUNK_SIZE):
fd.write(chunk)
return file_name
fetching_download_link = "🔍 Searching for **{}** in progress."
download_job_started = "\n ⬇️ **Download Server** [{}]({})"
download_successfull = "Download was completed in `{}` seconds\n\nNow Uploading to telegram in progres and that should not take long."
no_result_found = "Oops! There was an error!!!"
def humanbytes(size):
# https://stackoverflow.com/a/49361727/4723940
#2**10 = 1024
if not size:
return ""
power = 2**10
n = 0
Dic_powerN = {0 : ' ', 1: 'Ki', 2: 'Mi', 3: 'Gi', 4: 'Ti'}
while size > power:
size /= power
n += 1
return str(math.floor(size)) + " " + Dic_powerN[n] + 'B'
import pyrogram
logging.getLogger("pyrogram").setLevel(logging.WARNING)
## The telegram Specific Functions
def error(bot, update, error):
# TRChatBase(update.from_user.id, update.text, "error")
logger.warning('Update "%s" caused error "%s"', update, error)
def messages(bot, update):
global active_chats
user_chat = active_chats.get(update.from_user.id, None)
if user_chat is None:
bot.send_message(chat_id=update.from_user.id,text="**INVALID ACTION:** 😩 Kindly send /search to initiate your session", parse_mode="Markdown")
return
actions = user_chat.get('actions', None)
if actions is None:
bot.send_message(chat_id=update.from_user.id, text="**UNKNOWN ACTIVITY:** 👀 I have no clue on what you mean.", parse_mode="Markdown")
return
if len(actions) == 0:
bot.send_message(chat_id=update.from_user.id,
text="**ERROR:** 😏 You seem to be a stranger. Use /help to learn more about me", parse_mode="Markdown")
return
recent_action = actions[-1]
#bot.send_message(chat_id=update.from_user.id, text="DEBUG: last action: {}".format(recent_action))
if recent_action == 'apks':
if len(update.text) < 5:
apk_string = "{}".format("apks")
bot.send_message(
chat_id=update.from_user.id,
text="**📱 Apk Downloader**\n\n__Step 2 of 3__\n\n"
"🔎 **Search Query too short!** Please try again.",
reply_markup=InlineKeyboardMarkup(
[
[
InlineKeyboardButton("⬅️ Go Back", callback_data=apk_string.encode("UTF-8")),
]
]
),
reply_to_message_id=update.message_id,
disable_web_page_preview=True)
return
user_chat['search_query'] = ' '.join(map(lambda x: x.capitalize(),
update.text.split(' ')))
user_chat['link'] = None
command_get_specify_apk(bot, update)
else:
home_string = "{}".format("start")
bot.edit_message_text(text="This action is not supported :(",
chat_id=update.from_user.id,
message_id=update.message_id,
reply_markup=InlineKeyboardMarkup(
[
[
InlineKeyboardButton("⬅️ Go Back", callback_data=home_string.encode("UTF-8")),
]
]
))
def start(bot, update):
global active_chats
if update.from_user.id not in active_chats:
active_chats[update.from_user.id] = {'actions': []}
active_chats[update.from_user.id]['actions'].append('apks')
audio_string = "{}".format("downl")
services = "{}".format("services")
info_string = "{}".format("help")
join_string = "{}".format("join")
sent = bot.send_message(update.chat.id,
text=Translation.START_TEXT,
reply_markup=InlineKeyboardMarkup(
[
[ # First row
# Generates a callback query when pressed
InlineKeyboardButton("🚸 Join Beta group ", callback_data=join_string.encode("UTF-8")),
# Opens a web URL
InlineKeyboardButton("♻️ Services", callback_data=services.encode("UTF-8")),
],
[
InlineKeyboardButton("🆘 Help and Usage", callback_data=info_string.encode("UTF-8")),
]
]
),
reply_to_message_id=update.message_id,
disable_web_page_preview=True).message_id
def ft(bot, update):
global active_chats
if update.from_user.id not in active_chats:
active_chats[update.from_user.id] = {'actions': []}
active_chats[update.from_user.id]['actions'].append('filetransfer')
audio_string = "{}".format("downl")
services = "{}".format("services")
info_string = "{}".format("help")
join_string = "{}".format("join")
if update.reply_to_message is not None:
reply_message = update.reply_to_message
download_location = Config.DOWNLOAD_LOCATION + "/"
a = bot.send_message(
chat_id=update.from_user.id,
text=Translation.DOWNLOAD_START,
reply_to_message_id=update.message_id
)
after_download_file_name = bot.download_media(
message=reply_message,
file_name=download_location, progress = progs, progress_args = (a.message_id, update.from_user.id)
)
filename_w_ext = os.path.basename(after_download_file_name)
filename, download_extension = os.path.splitext(filename_w_ext)
filename = filename.strip('\n').replace(' ','_')
bot.edit_message_text(
text=Translation.SAVED_RECVD_DOC_FILE,
chat_id=update.from_user.id,
message_id=a.message_id
)
url = "https://transfer.sh/{}{}".format(str(filename), str(download_extension))
max_days = "5"
command_to_exec = [
"curl",
# "-H", 'Max-Downloads: 1',
"-H", 'Max-Days: 5', # + max_days + '',
"--upload-file", after_download_file_name,
url
]
bot.edit_message_text(
text=Translation.UPLOAD_START,
chat_id=update.from_user.id,
message_id=a.message_id
)
try:
logger.info(command_to_exec)
t_response = subprocess.check_output(command_to_exec, stderr=subprocess.STDOUT)
except subprocess.CalledProcessError as exc:
logger.info("Status : FAIL", exc.returncode, exc.output)
bot.edit_message_text(
chat_id=update.from_user.id,
text=exc.output.decode("UTF-8"),
message_id=a.message_id
)
else:
t_response_arry = t_response.decode("UTF-8").split("\n")[-1].strip()
bot.edit_message_text(
chat_id=update.from_user.id,
text=Translation.FILETRANSFER_GET_DL_LINK.format(t_response_arry, max_days),
parse_mode=pyrogram.ParseMode.HTML,
message_id=a.message_id,
disable_web_page_preview=True
)
try:
os.remove(after_download_file_name)
except:
pass
else:
bot.send_message(
chat_id=update.from_user.id,
text=Translation.REPLY_TO_DOC_GET_LINK,
reply_to_message_id=update.message_id
)
def fo(bot, update):
global active_chats
if update.from_user.id not in active_chats:
active_chats[update.from_user.id] = {'actions': []}
active_chats[update.from_user.id]['actions'].append('fileio')
audio_string = "{}".format("downl")
services = "{}".format("services")
info_string = "{}".format("help")
join_string = "{}".format("join")
if update.reply_to_message is not None:
reply_message = update.reply_to_message
download_location = Config.DOWNLOAD_LOCATION + "/"
a = bot.send_message(
chat_id=update.from_user.id,
text=Translation.DOWNLOAD_START,
reply_to_message_id=update.message_id
)
after_download_file_name = bot.download_media(
message=reply_message,
file_name=download_location, progress = progs, progress_args = (a.message_id, update.from_user.id)
)
filename_w_ext = os.path.basename(after_download_file_name)
filename, download_extension = os.path.splitext(filename_w_ext)
filename = filename.strip('\n').replace(' ','_')
bot.edit_message_text(
text=Translation.SAVED_RECVD_DOC_FILE,
chat_id=update.from_user.id,
message_id=a.message_id
)
expires = "1w"
bot.edit_message_text(
text=Translation.UPLOAD_START,
chat_id=update.from_user.id,
message_id=a.message_id
)
url = "https://file.io/?expires={expires}"
fin = open(after_download_file_name, 'rb')
files = {'file': fin}
try:
max_days = "7"
r = requests.post(url, files=files).json()
print(r['link'])
bot.edit_message_text(
chat_id=update.from_user.id,
text=Translation.FILEIO_GET_DL_LINK.format(r['link'], max_days),
parse_mode=pyrogram.ParseMode.HTML,
message_id=a.message_id,
disable_web_page_preview=True
)
except:
bot.edit_message_text(
chat_id=update.from_user.id,
text="Error uploading file",
message_id=a.message_id
)
os.remove(after_download_file_name)
pass
finally:
os.remove(after_download_file_name)
fin.close()
else:
bot.send_message(
chat_id=update.from_user.id,
text=Translation.REPLY_TO_DOC_GET_LINK,
reply_to_message_id=update.message_id
)
def help(bot, update):
global active_chats
active_chats[update.from_user.id] = {'actions': []}
start_string = "{}".format("start")
sent = bot.send_message(update.chat.id,
text=Translation.HELP_TEXT,
reply_markup=InlineKeyboardMarkup(
[
[
InlineKeyboardButton("🏡 Return Back Home", callback_data=start_string.encode("UTF-8")),
]
]
),
reply_to_message_id=update.message_id,
disable_web_page_preview=True).message_id
def search(bot, update):
global active_chats
if update.from_user.id not in active_chats:
active_chats[update.from_user.id] = {'actions': []}
active_chats[update.from_user.id]['actions'].append('apks')
start_string = "{}".format("downl")
sent = bot.send_message(update.chat.id,
text="**📱 Apk Downloader Premium**\n\n__Step 1 of 2__\n"
"\nOK! Send me search query in next message.",
reply_markup=InlineKeyboardMarkup(
[
[
InlineKeyboardButton("⬅️ Retrun to Previous menu", callback_data=start_string.encode("UTF-8")),
]
]
),
reply_to_message_id=update.message_id,
disable_web_page_preview=True).message_id
@app.on_callback_query(dynamic_data(b"start"))
def start_data(bot, update):
global active_chats
if update.from_user.id not in active_chats:
active_chats[update.from_user.id] = {'actions': []}
active_chats[update.from_user.id]['actions'].append('apks')
audio_string = "{}".format("downl")
services = "{}".format("services")
info_string = "{}".format("help")
join_string = "{}".format("join")
bot.edit_message_text(
chat_id=update.from_user.id,
text=Translation.START_TEXT,
reply_markup=InlineKeyboardMarkup(
[
[ # First row
# Generates a callback query when pressed
InlineKeyboardButton("🚸 Join Beta group ", callback_data=join_string.encode("UTF-8")),
# Opens a web URL
InlineKeyboardButton("♻️ Services", callback_data=services.encode("UTF-8")),
],
[
InlineKeyboardButton("🆘 Help and Usage", callback_data=info_string.encode("UTF-8")),
]
]
),
message_id=update.message.message_id,
disable_web_page_preview=True
)
@app.on_callback_query(dynamic_data(b"join"))
def pyrogram_data(bot, update):
global active_chats
active_chats[update.from_user.id] = {'actions': []}
start_string = "{}".format("start")
bot.edit_message_text(
text="⚠️ Please Before you join the group bare in mind that its not a group to spam with links and sfw. Hope you understand",
chat_id=update.from_user.id,
reply_markup=InlineKeyboardMarkup(
[
[ # First row
# Generates a callback query when pressed
InlineKeyboardButton("🚹 Join Beta group" , url="https://t.me/joinchat/C74PmEPu2JymxxnUCbPytw"),
# Opens a web URL
InlineKeyboardButton("⬅️ Retrun to Main menu" , callback_data=start_string.encode("UTF-8")),
],
]
),
message_id=update.message.message_id
)
@app.on_callback_query(dynamic_data(b"services"))
def start_data(bot, update):
global active_chats
if update.from_user.id not in active_chats:
active_chats[update.from_user.id] = {'actions': []}
active_chats[update.from_user.id]['actions'].append('services')
start_string = "{}".format("start")
bot.edit_message_text(
text=Translation.SERVICES,
chat_id=update.from_user.id,
parse_mode=pyrogram.ParseMode.HTML,
reply_markup=InlineKeyboardMarkup(
[
[
InlineKeyboardButton("⬅️ " + "Go Back" , callback_data=start_string.encode("UTF-8"))
]
]
),
message_id=update.message.message_id,
disable_web_page_preview=True
)
@app.on_callback_query(dynamic_data(b"downl"))
def pyrogram_data(bot, update):
global active_chats
active_chats[update.from_user.id] = {'actions': []}
start_string = "{}".format("start")
apk_string = "{}".format("apks")
bot.edit_message_text(
text=Translation.DOWNLOAD_TOOLS_TEXT,
chat_id=update.from_user.id,
reply_markup=InlineKeyboardMarkup(
[
[ # First row
# Generates a callback query when pressed
InlineKeyboardButton("🔎 " + " Search Android Apps " , callback_data=apk_string.encode("UTF-8"))
],
[
InlineKeyboardButton("🚫 Cancel" , callback_data=start_string.encode("UTF-8"))
]
]
),
message_id=update.message.message_id
)
@app.on_callback_query(dynamic_data(b"apks"))
def pyrogram_data(bot, update):
global active_chats
if update.from_user.id not in active_chats:
active_chats[update.from_user.id] = {'actions': []}
active_chats[update.from_user.id]['actions'].append('apks')
start_string = "{}".format("downl")
bot.edit_message_text(
text="**📱 Apk Downloader Premium**\n\n__Step 1 of 2__\n"
"\nOK! Send me search query in next message.",
chat_id=update.from_user.id,
reply_markup=InlineKeyboardMarkup(
[
[
# Opens a web URL
InlineKeyboardButton("⬅️ Retrun to Previous menu" , callback_data=start_string.encode("UTF-8")),
],
]
),
message_id=update.message.message_id
)
def command_get_specify_apk(bot, update):
if active_chats.get(update.from_user.id).get('link') is None:
search_query = active_chats.get(update.from_user.id).get('search_query')
query = " ".join(search_query)
try:
sent = update.reply(fetching_download_link.format(query),
quote=True, disable_web_page_preview=True)
print('Searching for: {}'.format(query))
options={}
base_headers = {
'User-Agent': 'Mozilla/6.0 (Macintosh; Intel Mac OS X 10_11_6) AppleWebKit/601.7.5 (KHTML, like Gecko) Version/9.1.2 Safari/601.7.5',
'Accept-Encoding': 'gzip, deflate, sdch',
'Accept-Language': 'zh-CN,zh;q=0.8'
}
headers = dict(base_headers, **options)
res = requests.get('https://apkpure.com/search?q={}®ion='.format(quote_plus(query)), headers=headers).text
APPS = []
soup = BeautifulSoup(res, "html.parser")
for i in soup.find('div', {'id':'search-res'}).findAll('dl', {'class':'search-dl'}):
app = i.find('p', {'class':'search-title'}).find('a')
APPS.append((app.text,
i.findAll('p')[1].find('a').text,
'https://apkpure.com' + app['href']))
time.sleep(2)
inline_keyboard = []
if len(APPS) > 0:
items = ""
for idx, app in enumerate(APPS):
start_string = "{}|{}".format(idx, app[0])
ikeyboard = [
InlineKeyboardButton(
"[{:02d}] - {}".format(idx, app[0]),
callback_data=start_string.encode("UTF-8")
)
]
user_chat = active_chats.get(update.from_user.id, None)
user_chat['Aps'] = APPS
user_chat['Apps'] = None
inline_keyboard.append(ikeyboard)
num=len(APPS)
reply_markup = InlineKeyboardMarkup(inline_keyboard)
sent.edit("📱 <b>Apk Downloader Premium</b> __Step 2 of 2__: <b>{}</b> Results \n\n".format(num), reply_markup=reply_markup, parse_mode="html", disable_web_page_preview=True)
else:
sent.edit("**📱 Apk Downloader Premium**\n\n__Step 1 of 2__\n"
"\n\n❗️ Search Not Found.. Try again")
return
except Exception as e:
update.reply(str(e))
except:
traceback.print_exc()
@app.on_callback_query(dynamic_data(b"help"))
def pyrogram_data(bot, update):
global active_chats
active_chats[update.from_user.id] = {'actions': []}
start_string = "{}".format("start")
bot.edit_message_text(
text=Translation.HELP_TEXT,
chat_id=update.from_user.id,
reply_markup=InlineKeyboardMarkup(
[
[
InlineKeyboardButton("⬅️ " + "Go Back" , callback_data=start_string.encode("UTF-8"))
]
]
),
message_id=update.message.message_id
)
@app.on_callback_query(dynamic_data(b"getapk"))
def pyrogram_data(bot, update):
if active_chats.get(update.from_user.id).get('link') is None:
search_query = active_chats.get(update.from_user.id).get('search_query')
searchs = " ".join(search_query)
sent = bot.send_message(update.from_user.id, fetching_download_link.format(searchs), reply_to_message_id=update.message.message_id).message_id
print('Searching for: {}'.format(searchs))
search(searchs)
time.sleep(5)
if len(APPS) == 0:
bot.edit_message_text(text='Your search returned No results',
chat_id=update.message.chat_id,
parse_mode="Markdown",
message_id=update.message.chat_id,
#reply_markup=reply_markup,
disable_web_page_preview=True)
return
bot.delete_messages(update.from_user.id, update.message.message_id)
inline_keyboard = []
if len(APPS) > 0:
for idx, app in enumerate(APPS):
start_string = "{}|{}".format(idx, app[0])
ikeyboard = [
InlineKeyboardButton(
"[{:02d}] - {}".format(idx, app[0]),
callback_data=start_string.encode("UTF-8")
)
]
user_chat = active_chats.get(update.from_user.id, None)
user_chat['Aps'] = APPS
user_chat['Apps'] = None
inline_keyboard.append(ikeyboard)
num=len(APPS)
reply_markup = InlineKeyboardMarkup(inline_keyboard)
bot.edit_message_text(
text="🔍 Search for *{}* Returned (`{}`) results\n\n Click on your app and i will download it right away".format(search_query, num),
chat_id=update.from_user.id,
reply_markup=reply_markup,
message_id=sent,
disable_web_page_preview=True
)
def button(bot, update):
if active_chats.get(update.from_user.id).get('Apps') is None:
APPS = active_chats.get(update.from_user.id).get('Aps')
chat_id = update.from_user.id
rnd = "123456789abcdefgh-_"
servers = shuffle(rnd)
if update.data.find(b"|") == -1:
return ""
app_num, app_name = update.data.split(b"|")
app_num = int(app_num)
options={}
link = APPS[app_num][2]
first_time = time.time()
try:
bot.edit_message_text(update.from_user.id, update.message.message_id, download_job_started.format(servers, APPS[app_num][2]))
time.sleep(3)
base_headers = {
'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_6) AppleWebKit/601.7.5 (KHTML, like Gecko) Version/9.1.2 Safari/601.7.5',
'Accept-Encoding': 'gzip, deflate, sdch',
'Accept-Language': 'zh-CN,zh;q=0.8'
}
headers = dict(base_headers, **options)
res = requests.get(link + '/download?from=details', headers=headers).text
soup = BeautifulSoup(res, "html.parser").find('a', {'id':'download_link'})
if soup['href']:
r = requests.get(soup['href'], stream=True, allow_redirects=True, headers=headers)
required_file_nam = get_filename_from_cd(r.headers.get('content-disposition'))
required_file_name = required_file_nam.strip('\n').replace('\"','').replace('\'','').replace('?','').replace(" ", "_")
with open(required_file_name, 'wb') as file:
total_length = int(r.headers.get('content-length', 0)) or None
downloaded_size = 0
chunk_size=8192*1024
if total_length is None: # no content length header
file.write(r.content)
else:
dl = 0
total_length = int(total_length)
for chunk in progress.bar(r.iter_content(chunk_size=chunk_size), expected_size=(total_length / 1024) + 1):
if chunk:
dl += len(chunk)
done = int(100 * dl / total_length)
file.write(chunk)
file.flush()
os.fsync(file.fileno())
if chunk:
dl += len(chunk)
done = int(100 * dl / total_length)
file.write(chunk)
file.flush()
os.fsync(file.fileno())
second_time = time.time()
t1 = time.time()
bot.edit_message_text(update.from_user.id, update.message.message_id, download_successfull.format(str(second_time - first_time)[:5]))
time.sleep(3)
#bot.delete_messages(update.from_user.id, update.message.message_id)
t2 = time.time()
description = " " + " \r\n ❤️ @Bfas237Bots "
sent = bot.send_document(update.from_user.id, required_file_name, progress = prog, progress_args = (update.message.message_id, update.from_user.id, required_file_name), caption='**File Size**: {}\n\n**Completed in**: `{}` **Seconds**\n'.format(str(pretty_size(total_length)), str(int(t2 - t1))), reply_to_message_id=update.message.message_id)
time.sleep(1)
bot.edit_message_caption(update.from_user.id,sent.message_id, caption='{}'.format(description))
os.remove(required_file_name)
else:
bot.edit_message_text(update.from_user.id, update.message.message_id, "No valid Download link was found.\n\n The server terminated all request. Kindly try again")
except Exception as Err:
bot.edit_message_text(update.from_user.id, update.message.message_id, Err)
traceback.print_exc()
if __name__ == "__main__" :
# create download directory, if not exist
if not os.path.isdir(Config.DOWNLOAD_LOCATION):
os.makedirs(Config.DOWNLOAD_LOCATION)
app.add_handler(pyrogram.MessageHandler(start, pyrogram.Filters.command(["start"])))
app.add_handler(pyrogram.MessageHandler(ft, pyrogram.Filters.command(["ft"])))
app.add_handler(pyrogram.MessageHandler(fo, pyrogram.Filters.command(["fo"])))
app.add_handler(pyrogram.MessageHandler(search, pyrogram.Filters.command(["search" , "s"])))
app.add_handler(pyrogram.MessageHandler(help, pyrogram.Filters.command(["help"])))
app.add_handler(pyrogram.MessageHandler(messages, pyrogram.Filters.text))
app.add_handler(pyrogram.CallbackQueryHandler(button))
app.run()