-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathscrapers.py
1536 lines (1385 loc) · 50.1 KB
/
scrapers.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
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# Copyright (C) 2019 The Raphielscape Company LLC.
#
# Licensed under the Raphielscape Public License, Version 1.c (the "License");
# you may not use this file except in compliance with the License.
#
# thanks to the owner of X-tra-Telegram for tts fix
#
# Recode by @mrismanaziz
# FROM Man-Userbot
# t.me/SharingUserbot
#
""" Userbot module containing various scrapers. """
import asyncio
import io
import json
import os
import re
import shutil
import time
from asyncio import sleep
from os import popen
from random import choice
from re import findall, match
from time import sleep
from urllib.parse import quote_plus
import asyncurban
import barcode
import emoji
import qrcode
import requests
from barcode.writer import ImageWriter
from bs4 import BeautifulSoup
from emoji import get_emoji_regexp
from googletrans import LANGUAGES, Translator
from gtts import gTTS
from gtts.lang import tts_langs
from humanize import naturalsize
from requests import exceptions, get, post
from search_engine_parser import YahooSearch as GoogleSearch
from telethon.tl.types import DocumentAttributeAudio, MessageMediaPhoto
from wikipedia import summary
from wikipedia.exceptions import DisambiguationError, PageError
from youtube_dl import YoutubeDL
from youtube_dl.utils import (
ContentTooShortError,
DownloadError,
ExtractorError,
GeoRestrictedError,
MaxDownloadsReached,
PostProcessingError,
UnavailableVideoError,
XAttrMetadataError,
)
from youtube_search import YoutubeSearch
from userbot import (
BOTLOG,
BOTLOG_CHATID,
CMD_HELP,
LOGS,
OCR_SPACE_API_KEY,
REM_BG_API_KEY,
TEMP_DOWNLOAD_DIRECTORY,
bot,
)
from userbot.events import register
from userbot.utils import chrome, googleimagesdownload, options, progress
CARBONLANG = "auto"
TTS_LANG = "id"
TRT_LANG = "id"
TEMP_DOWNLOAD_DIRECTORY = "/root/userbot/.bin"
async def ocr_space_file(
filename, overlay=False, api_key=OCR_SPACE_API_KEY, language="eng"
):
"""OCR.space API request with local file.
Python3.5 - not tested on 2.7
:param filename: Your file path & name.
:param overlay: Is OCR.space overlay required in your response.
Defaults to False.
:param api_key: OCR.space API key.
Defaults to 'helloworld'.
:param language: Language code to be used in OCR.
List of available language codes can be found on https://ocr.space/OCRAPI
Defaults to 'eng'.
:return: Result in JSON format.
"""
payload = {
"isOverlayRequired": overlay,
"apikey": api_key,
"language": language,
}
with open(filename, "rb") as f:
r = requests.post(
"https://api.ocr.space/parse/image",
files={filename: f},
data=payload,
)
return r.json()
DOGBIN_URL = "https://del.dog/"
NEKOBIN_URL = "https://nekobin.com/"
@register(outgoing=True, pattern=r"^\.crblang (.*)")
async def setlang(prog):
global CARBONLANG
CARBONLANG = prog.pattern_match.group(1)
await prog.edit(f"Bahasa untuk carbon.now.sh mulai {CARBONLANG}")
@register(outgoing=True, pattern="^.carbon")
async def carbon_api(e):
"""A Wrapper for carbon.now.sh"""
await e.edit("`Processing..`")
CARBON = "https://carbon.now.sh/?l={lang}&code={code}"
global CARBONLANG
textx = await e.get_reply_message()
pcode = e.text
if pcode[8:]:
pcode = str(pcode[8:])
elif textx:
pcode = str(textx.message) # Importing message to module
code = quote_plus(pcode) # Converting to urlencoded
await e.edit("`Processing..\n25%`")
if os.path.isfile("/root/userbot/.bin/carbon.png"):
os.remove("/root/userbot/.bin/carbon.png")
url = CARBON.format(code=code, lang=CARBONLANG)
chrome_options = Options()
chrome_options.add_argument("--headless")
chrome_options.binary_location = GOOGLE_CHROME_BIN
chrome_options.add_argument("--window-size=1920x1080")
chrome_options.add_argument("--disable-dev-shm-usage")
chrome_options.add_argument("--no-sandbox")
chrome_options.add_argument("--disable-gpu")
prefs = {"download.default_directory": "/root/userbot/.bin"}
chrome_options.add_experimental_option("prefs", prefs)
driver = webdriver.Chrome(executable_path=CHROME_DRIVER, options=chrome_options)
driver.get(url)
await e.edit("`Processing..\n50%`")
download_path = "/root/userbot/.bin"
driver.command_executor._commands["send_command"] = (
"POST",
"/session/$sessionId/chromium/send_command",
)
params = {
"cmd": "Page.setDownloadBehavior",
"params": {"behavior": "allow", "downloadPath": download_path},
}
driver.execute("send_command", params)
driver.find_element_by_xpath("//button[contains(text(),'Export')]").click()
# driver.find_element_by_xpath("//button[contains(text(),'4x')]").click()
# driver.find_element_by_xpath("//button[contains(text(),'PNG')]").click()
await e.edit("`Processing..\n75%`")
# Waiting for downloading
while not os.path.isfile("/root/userbot/.bin/carbon.png"):
await sleep(0.5)
await e.edit("`Processing..\n100%`")
file = "/root/userbot/.bin/carbon.png"
await e.edit("`Uploading..`")
await e.client.send_file(
e.chat_id,
file,
caption="Made using [Carbon](https://carbon.now.sh/about/),\
\na project by [Dawn Labs](https://dawnlabs.io/)",
force_document=True,
reply_to=e.message.reply_to_msg_id,
)
os.remove("/root/userbot/.bin/carbon.png")
driver.quit()
# Removing carbon.png after uploading
await e.delete() # Deleting msg
@register(outgoing=True, pattern=r"^\.img (.*)")
async def img_sampler(event):
"""For .img command, search and return images matching the query."""
await event.edit("`Sedang Mencari Gambar Yang Anda Cari...`")
query = event.pattern_match.group(1)
lim = findall(r"lim=\d+", query)
try:
lim = lim[0]
lim = lim.replace("lim=", "")
query = query.replace("lim=" + lim[0], "")
except IndexError:
lim = 15
response = googleimagesdownload()
# creating list of arguments
arguments = {
"keywords": query,
"limit": lim,
"format": "jpg",
"no_directory": "no_directory",
}
# passing the arguments to the function
paths = response.download(arguments)
lst = paths[0][query]
await event.client.send_file(
await event.client.get_input_entity(event.chat_id), lst
)
shutil.rmtree(os.path.dirname(os.path.abspath(lst[0])))
await event.delete()
@register(outgoing=True, pattern=r"^\.currency (.*)")
async def moni(event):
input_str = event.pattern_match.group(1)
input_sgra = input_str.split(" ")
if len(input_sgra) != 3:
return await event.edit("`Invalid syntax.`")
try:
number = float(input_sgra[0])
currency_from = input_sgra[1].upper()
currency_to = input_sgra[2].upper()
request_url = f"https://api.ratesapi.io/api/latest?base={currency_from}"
current_response = get(request_url).json()
if currency_to in current_response["rates"]:
current_rate = float(current_response["rates"][currency_to])
rebmun = round(number * current_rate, 2)
await event.edit(
"{} {} = {} {}".format(number, currency_from, rebmun, currency_to)
)
else:
await event.edit(
"`Sepertinya ini adalah mata uang asing, yang tidak dapat saya konversi sekarang.`"
)
except Exception as e:
await event.edit(str(e))
@register(outgoing=True, pattern=r"^\.google (.*)")
async def gsearch(q_event):
"""For .google command, do a Google search."""
match = q_event.pattern_match.group(1)
page = findall(r"page=\d+", match)
try:
page = page[0]
page = page.replace("page=", "")
match = match.replace("page=" + page[0], "")
except IndexError:
page = 1
try:
search_args = (str(match), int(page))
gsearch = GoogleSearch()
gresults = await gsearch.async_search(*search_args)
msg = ""
for i in range(5):
try:
title = gresults["titles"][i]
link = gresults["links"][i]
desc = gresults["descriptions"][i]
msg += f"[{title}]({link})\n`{desc}`\n\n"
except IndexError:
break
except BaseException as g_e:
return await q_event.edit(f"**Error : ** `{g_e}`")
await q_event.edit(
"**Search Query:**\n`" + match + "`\n\n**Results:**\n" + msg, link_preview=False
)
@register(outgoing=True, pattern=r"^\.wiki (.*)")
async def wiki(wiki_q):
"""For .wiki command, fetch content from Wikipedia."""
match = wiki_q.pattern_match.group(1)
try:
summary(match)
except DisambiguationError as error:
await wiki_q.edit(f"Ditemukan halaman yang tidak ambigu.\n\n{error}")
return
except PageError as pageerror:
await wiki_q.edit(f"Halaman tidak ditemukan.\n\n{pageerror}")
return
result = summary(match)
if len(result) >= 4096:
with open("output.txt", "w+") as file:
file.write(result)
await wiki_q.client.send_file(
wiki_q.chat_id,
"output.txt",
reply_to=wiki_q.id,
caption="`Output terlalu besar, dikirim sebagai file`",
)
if os.path.exists("output.txt"):
os.remove("output.txt")
return
await wiki_q.edit("**Search:**\n`" + match + "`\n\n**Result:**\n" + result)
@register(outgoing=True, pattern=r"^\.ud (.*)")
async def _(event):
if event.fwd_from:
return
await event.edit("processing...")
word = event.pattern_match.group(1)
urban = asyncurban.UrbanDictionary()
try:
mean = await urban.get_word(word)
await event.edit(
"Text: **{}**\n\nBerarti: **{}**\n\nContoh: __{}__".format(
mean.word, mean.definition, mean.example
)
)
except asyncurban.WordNotFoundError:
await event.edit("Tidak ada hasil untuk **" + word + "**")
@register(outgoing=True, pattern=r"^\.tts(?: |$)([\s\S]*)")
async def text_to_speech(query):
"""For .tts command, a wrapper for Google Text-to-Speech."""
textx = await query.get_reply_message()
message = query.pattern_match.group(1)
if message:
pass
elif textx:
message = textx.text
else:
return await query.edit(
"**Berikan teks atau balas pesan untuk Text-to-Speech!**"
)
try:
gTTS(message, lang=TTS_LANG)
except AssertionError:
return await query.edit(
"**Teksnya kosong.**\n"
"Tidak ada yang tersisa untuk dibicarakan setelah pra-pemrosesan, pembuatan token, dan pembersihan."
)
except ValueError:
return await query.edit("**Bahasa tidak didukung.**")
except RuntimeError:
return await query.edit("**Error saat memuat kamus bahasa.**")
tts = gTTS(message, lang=TTS_LANG)
tts.save("k.mp3")
with open("k.mp3", "rb") as audio:
linelist = list(audio)
linecount = len(linelist)
if linecount == 1:
tts = gTTS(message, lang=TTS_LANG)
tts.save("k.mp3")
with open("k.mp3", "r"):
await query.client.send_file(query.chat_id, "k.mp3", voice_note=True)
os.remove("k.mp3")
await query.delete()
@register(outgoing=True, pattern=r"^\.tr(?: |$)(.*)")
async def _(event):
if event.fwd_from:
return
if "trim" in event.raw_text:
return
input_str = event.pattern_match.group(1)
if event.reply_to_msg_id:
previous_message = await event.get_reply_message()
text = previous_message.message
lan = input_str or "en"
elif "|" in input_str:
lan, text = input_str.split("|")
else:
await event.edit("**.tr <kode bahasa>** sambil reply ke pesan")
return
text = emoji.demojize(text.strip())
lan = lan.strip()
translator = Translator()
try:
translated = translator.translate(text, dest=lan)
after_tr_text = translated.text
output_str = """**DITERJEMAHKAN** dari `{}` ke `{}`
{}""".format(
translated.src, lan, after_tr_text
)
await event.edit(output_str)
except Exception as exc:
await event.edit(str(exc))
@register(pattern=r"\.lang (tr|tts) (.*)", outgoing=True)
async def lang(value):
"""For .lang command, change the default langauge of userbot scrapers."""
util = value.pattern_match.group(1).lower()
if util == "tr":
scraper = "Translator"
global TRT_LANG
arg = value.pattern_match.group(2).lower()
if arg in LANGUAGES:
TRT_LANG = arg
LANG = LANGUAGES[arg]
else:
await value.edit(
f"**Kode Bahasa tidak valid !!**\n**Kode bahasa yang tersedia**:\n\n`{LANGUAGES}`"
)
return
elif util == "tts":
scraper = "Text to Speech"
global TTS_LANG
arg = value.pattern_match.group(2).lower()
if arg in tts_langs():
TTS_LANG = arg
LANG = tts_langs()[arg]
else:
await value.edit(
f"**Kode Bahasa tidak valid!!**\n**Kode bahasa yang tersedia**:\n\n`{tts_langs()}`"
)
return
await value.edit(
f"**Bahasa untuk** `{scraper}` **diganti menjadi** `{LANG.title()}`"
)
if BOTLOG:
await value.client.send_message(
BOTLOG_CHATID,
f"**Bahasa untuk** `{scraper}` **diganti menjadi** `{LANG.title()}`",
)
@register(outgoing=True, pattern=r"^\.yt (\d*) *(.*)")
async def yt_search(video_q):
"""For .yt command, do a YouTube search from Telegram."""
if video_q.pattern_match.group(1) != "":
counter = int(video_q.pattern_match.group(1))
if counter > 10:
counter = int(10)
if counter <= 0:
counter = int(1)
else:
counter = int(5)
query = video_q.pattern_match.group(2)
if not query:
await video_q.edit("`Masukkan keyword untuk dicari`")
await video_q.edit("`Processing...`")
try:
results = json.loads(YoutubeSearch(query, max_results=counter).to_json())
except KeyError:
return await video_q.edit(
"`Pencarian Youtube menjadi lambat.\nTidak dapat mencari keyword ini!`"
)
output = f"**Pencarian Keyword:**\n`{query}`\n\n**Hasil:**\n\n"
for i in results["videos"]:
try:
title = i["title"]
link = "https://youtube.com" + i["url_suffix"]
channel = i["channel"]
duration = i["duration"]
views = i["views"]
output += f"[{title}]({link})\nChannel: `{channel}`\nDuration: {duration} | {views}\n\n"
except IndexError:
break
await video_q.edit(output, link_preview=False)
@register(outgoing=True, pattern=r".yt(audio|video) (.*)")
async def download_video(v_url):
"""For .yt command, download media from YouTube and many other sites."""
dl_type = v_url.pattern_match.group(1).lower()
url = v_url.pattern_match.group(2)
await v_url.edit("`Preparing to download...`")
video = False
audio = False
if dl_type == "audio":
opts = {
"format": "bestaudio",
"addmetadata": True,
"key": "FFmpegMetadata",
"writethumbnail": True,
"prefer_ffmpeg": True,
"geo_bypass": True,
"nocheckcertificate": True,
"postprocessors": [
{
"key": "FFmpegExtractAudio",
"preferredcodec": "mp3",
"preferredquality": "320",
}
],
"outtmpl": "%(id)s.%(ext)s",
"quiet": True,
"logtostderr": False,
}
audio = True
elif dl_type == "video":
opts = {
"format": "best",
"addmetadata": True,
"key": "FFmpegMetadata",
"prefer_ffmpeg": True,
"geo_bypass": True,
"nocheckcertificate": True,
"postprocessors": [
{"key": "FFmpegVideoConvertor", "preferedformat": "mp4"}
],
"outtmpl": "%(id)s.%(ext)s",
"logtostderr": False,
"quiet": True,
}
video = True
try:
await v_url.edit("`Fetching data, please wait..`")
with YoutubeDL(opts) as rip:
rip_data = rip.extract_info(url)
except DownloadError as DE:
return await v_url.edit(f"`{str(DE)}`")
except ContentTooShortError:
return await v_url.edit("`The download content was too short.`")
except GeoRestrictedError:
return await v_url.edit(
"`Video is not available from your geographic location "
"due to geographic restrictions imposed by a website.`"
)
except MaxDownloadsReached:
return await v_url.edit("`Max-downloads limit has been reached.`")
except PostProcessingError:
return await v_url.edit("`There was an error during post processing.`")
except UnavailableVideoError:
return await v_url.edit("`Media is not available in the requested format.`")
except XAttrMetadataError as XAME:
return await v_url.edit(f"`{XAME.code}: {XAME.msg}\n{XAME.reason}`")
except ExtractorError:
return await v_url.edit("`There was an error during info extraction.`")
except Exception as e:
return await v_url.edit(f"{str(type(e)): {str(e)}}")
c_time = time.time()
if audio:
await v_url.edit(
f"**Sedang Mengupload Lagu:**\n`{rip_data.get('title')}`"
f"\nby **{rip_data.get('uploader')}**"
)
f_name = rip_data.get("id") + ".mp3"
with open(f_name, "rb") as f:
result = await upload_file(
client=v_url.client,
file=f,
name=f_name,
progress_callback=lambda d, t: asyncio.get_event_loop().create_task(
progress(
d, t, v_url, c_time, "Uploading..", f"{rip_data['title']}.mp3"
)
),
)
img_extensions = ["jpg", "jpeg", "webp"]
img_filenames = [
fn_img
for fn_img in os.listdir()
if any(fn_img.endswith(ext_img) for ext_img in img_extensions)
]
thumb_image = img_filenames[0]
metadata = extractMetadata(createParser(f_name))
duration = 0
if metadata.has("duration"):
duration = metadata.get("duration").seconds
await v_url.client.send_file(
v_url.chat_id,
result,
supports_streaming=True,
attributes=[
DocumentAttributeAudio(
duration=duration,
title=rip_data.get("title"),
performer=rip_data.get("uploader"),
)
],
thumb=thumb_image,
)
os.remove(thumb_image)
os.remove(f_name)
await v_url.delete()
elif video:
await v_url.edit(
f"**Sedang Mengupload Video:**\n`{rip_data.get('title')}`"
f"\nby **{rip_data.get('uploader')}**"
)
f_name = rip_data.get("id") + ".mp4"
with open(f_name, "rb") as f:
result = await upload_file(
client=v_url.client,
file=f,
name=f_name,
progress_callback=lambda d, t: asyncio.get_event_loop().create_task(
progress(
d, t, v_url, c_time, "Uploading..", f"{rip_data['title']}.mp4"
)
),
)
thumb_image = await get_video_thumb(f_name, "thumb.png")
metadata = extractMetadata(createParser(f_name))
duration = 0
width = 0
height = 0
if metadata.has("duration"):
duration = metadata.get("duration").seconds
if metadata.has("width"):
width = metadata.get("width")
if metadata.has("height"):
height = metadata.get("height")
await v_url.client.send_file(
v_url.chat_id,
result,
thumb=thumb_image,
attributes=[
DocumentAttributeVideo(
duration=duration,
w=width,
h=height,
supports_streaming=True,
)
],
caption=rip_data["title"],
)
os.remove(f_name)
os.remove(thumb_image)
await v_url.delete()
def deEmojify(inputString):
"""Remove emojis and other non-safe characters from string"""
return get_emoji_regexp().sub("", inputString)
@register(outgoing=True, pattern="^.rbg(?: |$)(.*)")
async def kbg(remob):
"""For .rbg command, Remove Image Background."""
if REM_BG_API_KEY is None:
await remob.edit(
"`Error: Remove.BG API key missing! Add it to environment vars or config.env.`"
)
return
input_str = remob.pattern_match.group(1)
message_id = remob.message.id
if remob.reply_to_msg_id:
message_id = remob.reply_to_msg_id
reply_message = await remob.get_reply_message()
await remob.edit("`Processing..`")
try:
if isinstance(
reply_message.media, MessageMediaPhoto
) or "image" in reply_message.media.document.mime_type.split("/"):
downloaded_file_name = await remob.client.download_media(
reply_message, TEMP_DOWNLOAD_DIRECTORY
)
await remob.edit("`Removing background from this image..`")
output_file_name = await ReTrieveFile(downloaded_file_name)
os.remove(downloaded_file_name)
else:
await remob.edit("`Bagaimana cara menghapus latar belakang ini ?`")
except Exception as e:
await remob.edit(str(e))
return
elif input_str:
await remob.edit(
f"`Removing background from online image hosted at`\n{input_str}"
)
output_file_name = await ReTrieveURL(input_str)
else:
await remob.edit("`Saya butuh sesuatu untuk menghapus latar belakang.`")
return
contentType = output_file_name.headers.get("content-type")
if "image" in contentType:
with io.BytesIO(output_file_name.content) as remove_bg_image:
remove_bg_image.name = "removed_bg.png"
await remob.client.send_file(
remob.chat_id,
remove_bg_image,
caption="Support @SharingUserbot",
force_document=True,
reply_to=message_id,
)
await remob.delete()
else:
await remob.edit(
"**Error (Invalid API key, I guess ?)**\n`{}`".format(
output_file_name.content.decode("UTF-8")
)
)
# this method will call the API, and return in the appropriate format
# with the name provided.
async def ReTrieveFile(input_file_name):
headers = {
"X-API-Key": REM_BG_API_KEY,
}
files = {
"image_file": (input_file_name, open(input_file_name, "rb")),
}
return requests.post(
"https://api.remove.bg/v1.0/removebg",
headers=headers,
files=files,
allow_redirects=True,
stream=True,
)
async def ReTrieveURL(input_url):
headers = {
"X-API-Key": REM_BG_API_KEY,
}
data = {"image_url": input_url}
return requests.post(
"https://api.remove.bg/v1.0/removebg",
headers=headers,
data=data,
allow_redirects=True,
stream=True,
)
@register(pattern=r".ocr (.*)", outgoing=True)
async def ocr(event):
if not OCR_SPACE_API_KEY:
return await event.edit(
"`Error: OCR.Space API key is missing! Add it to environment variables or config.env.`"
)
await event.edit("`Sedang Membaca...`")
if not os.path.isdir(TEMP_DOWNLOAD_DIRECTORY):
os.makedirs(TEMP_DOWNLOAD_DIRECTORY)
lang_code = event.pattern_match.group(1)
downloaded_file_name = await bot.download_media(
await event.get_reply_message(), TEMP_DOWNLOAD_DIRECTORY
)
test_file = await ocr_space_file(filename=downloaded_file_name, language=lang_code)
try:
ParsedText = test_file["ParsedResults"][0]["ParsedText"]
except BaseException:
await event.edit(
"`Tidak bisa membacanya.`\n`Saya rasa saya perlu kacamata baru.`"
)
else:
await event.edit(f"`Inilah yang bisa saya baca darinya:`\n\n{ParsedText}")
os.remove(downloaded_file_name)
@register(pattern=r"^.decode$", outgoing=True)
async def parseqr(qr_e):
"""For .decode command, get QR Code/BarCode content from the replied photo."""
downloaded_file_name = await qr_e.client.download_media(
await qr_e.get_reply_message()
)
# parse the Official ZXing webpage to decode the QRCode
command_to_exec = [
"curl",
"-X",
"POST",
"-F",
"f=@" + downloaded_file_name + "",
"https://zxing.org/w/decode",
]
process = await asyncio.create_subprocess_exec(
*command_to_exec,
# stdout must a pipe to be accessible as process.stdout
stdout=asyncio.subprocess.PIPE,
stderr=asyncio.subprocess.PIPE,
)
# Wait for the subprocess to finish
stdout, stderr = await process.communicate()
e_response = stderr.decode().strip()
t_response = stdout.decode().strip()
os.remove(downloaded_file_name)
if not t_response:
LOGS.info(e_response)
LOGS.info(t_response)
return await qr_e.edit("Gagal untuk decode.")
soup = BeautifulSoup(t_response, "html.parser")
qr_contents = soup.find_all("pre")[0].text
await qr_e.edit(qr_contents)
@register(pattern=r".barcode(?: |$)([\s\S]*)", outgoing=True)
async def bq(event):
"""For .barcode command, genrate a barcode containing the given content."""
await event.edit("`Processing..`")
input_str = event.pattern_match.group(1)
message = "SYNTAX: `.barcode <long text to include>`"
reply_msg_id = event.message.id
if input_str:
message = input_str
elif event.reply_to_msg_id:
previous_message = await event.get_reply_message()
reply_msg_id = previous_message.id
if previous_message.media:
downloaded_file_name = await event.client.download_media(previous_message)
m_list = None
with open(downloaded_file_name, "rb") as fd:
m_list = fd.readlines()
message = "".join(m.decode("UTF-8") + "\r\n" for m in m_list)
os.remove(downloaded_file_name)
else:
message = previous_message.message
else:
return event.edit("SYNTAX: `.barcode <long text to include>`")
bar_code_type = "code128"
try:
bar_code_mode_f = barcode.get(bar_code_type, message, writer=ImageWriter())
filename = bar_code_mode_f.save(bar_code_type)
await event.client.send_file(event.chat_id, filename, reply_to=reply_msg_id)
os.remove(filename)
except Exception as e:
return await event.edit(str(e))
await event.delete()
@register(pattern=r".makeqr(?: |$)([\s\S]*)", outgoing=True)
async def make_qr(makeqr):
"""For .makeqr command, make a QR Code containing the given content."""
input_str = makeqr.pattern_match.group(1)
message = "SYNTAX: `.makeqr <long text to include>`"
reply_msg_id = None
if input_str:
message = input_str
elif makeqr.reply_to_msg_id:
previous_message = await makeqr.get_reply_message()
reply_msg_id = previous_message.id
if previous_message.media:
downloaded_file_name = await makeqr.client.download_media(previous_message)
m_list = None
with open(downloaded_file_name, "rb") as file:
m_list = file.readlines()
message = "".join(media.decode("UTF-8") + "\r\n" for media in m_list)
os.remove(downloaded_file_name)
else:
message = previous_message.message
qr = qrcode.QRCode(
version=1,
error_correction=qrcode.constants.ERROR_CORRECT_L,
box_size=10,
border=4,
)
qr.add_data(message)
qr.make(fit=True)
img = qr.make_image(fill_color="black", back_color="white")
img.save("img_file.webp", "PNG")
await makeqr.client.send_file(
makeqr.chat_id, "img_file.webp", reply_to=reply_msg_id
)
os.remove("img_file.webp")
await makeqr.delete()
@register(outgoing=True, pattern=r"^.direct(?: |$)([\s\S]*)")
async def direct_link_generator(request):
"""direct links generator"""
await request.edit("`Processing...`")
textx = await request.get_reply_message()
message = request.pattern_match.group(1)
if message:
pass
elif textx:
message = textx.text
else:
await request.edit("`Usage: .direct <url>`")
return
reply = ""
links = re.findall(r"\bhttps?://.*\.\S+", message)
if not links:
reply = "`No links found!`"
await request.edit(reply)
for link in links:
if "drive.google.com" in link:
reply += gdrive(link)
elif "zippyshare.com" in link:
reply += zippy_share(link)
elif "yadi.sk" in link:
reply += yandex_disk(link)
elif "cloud.mail.ru" in link:
reply += cm_ru(link)
elif "mediafire.com" in link:
reply += mediafire(link)
elif "sourceforge.net" in link:
reply += sourceforge(link)
elif "osdn.net" in link:
reply += osdn(link)
elif "github.com" in link:
reply += github(link)
elif "androidfilehost.com" in link:
reply += androidfilehost(link)
else:
reply += re.findall(r"\bhttps?://(.*?[^/]+)", link)[0] + "is not supported"
await request.edit(reply)
def gdrive(url: str) -> str:
"""GDrive direct links generator"""
drive = "https://drive.google.com"
try:
link = re.findall(r"\bhttps?://drive\.google\.com\S+", url)[0]
except IndexError:
reply = "`No Google drive links found`\n"
return reply
file_id = ""
reply = ""
if link.find("view") != -1:
file_id = link.split("/")[-2]
elif link.find("open?id=") != -1:
file_id = link.split("open?id=")[1].strip()
elif link.find("uc?id=") != -1:
file_id = link.split("uc?id=")[1].strip()
url = f"{drive}/uc?export=download&id={file_id}"
download = requests.get(url, stream=True, allow_redirects=False)
cookies = download.cookies
try:
# In case of small file size, Google downloads directly
dl_url = download.headers["location"]
if "accounts.google.com" in dl_url: # non-public file
reply += "`Link is not public!`\n"
return reply
name = "Direct Download Link"
except KeyError:
# In case of download warning page
page = BeautifulSoup(download.content, "lxml")
export = drive + page.find("a", {"id": "uc-download-link"}).get("href")
name = page.find("span", {"class": "uc-name-size"}).text
response = requests.get(
export, stream=True, allow_redirects=False, cookies=cookies
)
dl_url = response.headers["location"]
if "accounts.google.com" in dl_url:
reply += "Link is not public!"
return reply
reply += f"[{name}]({dl_url})\n"
return reply
def zippy_share(url: str) -> str:
link = re.findall("https:/.(.*?).zippyshare", url)[0]
response_content = (requests.get(url)).content
bs_obj = BeautifulSoup(response_content, "lxml")
try:
js_script = bs_obj.find("div", {"class": "center",}).find_all(
"script"
)[1]
except BaseException:
js_script = bs_obj.find("div", {"class": "right",}).find_all(
"script"
)[0]
js_content = re.findall(r'\.href.=."/(.*?)";', str(js_script))
js_content = 'var x = "/' + js_content[0] + '"'
evaljs = EvalJs()
setattr(evaljs, "x", None)
evaljs.execute(js_content)
js_content = getattr(evaljs, "x")
dl_url = f"https://{link}.zippyshare.com{js_content}"
file_name = basename(dl_url)
return f"[{urllib.parse.unquote_plus(file_name)}]({dl_url})"
def yandex_disk(url: str) -> str:
"""Yandex.Disk direct links generator
Based on https://github.com/wldhx/yadisk-direct"""
reply = ""
try:
link = re.findall(r"\bhttps?://.*yadi\.sk\S+", url)[0]
except IndexError:
reply = "`No Yandex.Disk links found`\n"
return reply
api = "https://cloud-api.yandex.net/v1/disk/public/resources/download?public_key={}"
try:
dl_url = requests.get(api.format(link)).json()["href"]
name = dl_url.split("filename=")[1].split("&disposition")[0]
reply += f"[{name}]({dl_url})\n"
except KeyError:
reply += "`Error: File not found / Download limit reached`\n"
return reply
return reply
def cm_ru(url: str) -> str:
"""cloud.mail.ru direct links generator
Using https://github.com/JrMasterModelBuilder/cmrudl.py"""
reply = ""
try:
link = re.findall(r"\bhttps?://.*cloud\.mail\.ru\S+", url)[0]
except IndexError:
reply = "`No cloud.mail.ru links found`\n"
return reply