Skip to content
This repository was archived by the owner on Sep 29, 2021. It is now read-only.

Commit fbc2532

Browse files
committed
Update: Fix issue error mega download
Special thanks to @adekmaulana Signed-off-by: MoveAngel <[email protected]>
1 parent b861a19 commit fbc2532

File tree

4 files changed

+117
-3
lines changed

4 files changed

+117
-3
lines changed

Dockerfile

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@ RUN sed -e 's;^#http\(.*\)/edge/community;http\1/edge/community;g' -i /etc/apk/r
1010
# Installing Packages
1111
#
1212
RUN apk add --no-cache=true --update \
13-
bash \
13+
coreutils \
14+
bash \
1415
build-base \
1516
bzip2-dev \
1617
curl \
@@ -54,8 +55,6 @@ RUN apk add --no-cache=true --update \
5455
zlib-dev \
5556
jpeg
5657

57-
58-
5958

6059
RUN python3 -m ensurepip \
6160
&& pip3 install --upgrade pip setuptools \

requirements.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,3 +43,4 @@ telegraph
4343
urbandict>=0.5
4444
wikipedia>=1.4.0
4545
coffeehouse
46+
wget

userbot/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,8 @@
155155
"bin/megadown",
156156
"https://raw.githubusercontent.com/yshalsager/cmrudl.py/master/cmrudl.py":
157157
"bin/cmrudl"
158+
"https://raw.githubusercontent.com/adekmaulana/python-scripts/master/shell/megadirect":
159+
"bin/megadirect"
158160
}
159161

160162
for binary, path in binaries.items():

userbot/modules/mega_downloader.py

Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
# Copyright (C) 2020 Adek Maulana.
2+
# All rights reserved.
3+
#
4+
# Redistribution and use of this script, with or without modification, is
5+
# permitted provided that the following conditions are met:
6+
#
7+
# 1. Redistributions of this script must retain the above copyright
8+
# notice, this list of conditions and the following disclaimer.
9+
#
10+
# THIS SOFTWARE IS PROVIDED BY THE AUTHOR "AS IS" AND ANY EXPRESS OR IMPLIED
11+
# WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
12+
# MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
13+
# EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
14+
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
15+
# PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
16+
# OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
17+
# WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
18+
# OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
19+
# ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
20+
21+
from humanize import naturalsize
22+
from subprocess import PIPE, Popen
23+
24+
import re
25+
import json
26+
import wget
27+
import os
28+
29+
from userbot import CMD_HELP
30+
from userbot.events import register
31+
32+
33+
def subprocess_run(cmd):
34+
subproc = Popen(cmd, stdout=PIPE, stderr=PIPE,
35+
shell=True, universal_newlines=True)
36+
talk = subproc.communicate()
37+
exitCode = subproc.returncode
38+
if exitCode != 0:
39+
print('An error was detected while running the subprocess:\n'
40+
'exit code: %d\n'
41+
'stdout: %s\n'
42+
'stderr: %s' % (exitCode, talk[0], talk[1]))
43+
return talk
44+
45+
46+
@register(outgoing=True, pattern=r"^.mega(?: |$)(.*)")
47+
async def mega_downloader(event):
48+
await event.edit("`Processing...`")
49+
textx = await event.get_reply_message()
50+
link = event.pattern_match.group(1)
51+
if link:
52+
pass
53+
elif textx:
54+
link = textx.text
55+
else:
56+
await event.edit("`Usage: .mega <mega url>`")
57+
return
58+
reply = ''
59+
if not link:
60+
reply = "`No MEGA.nz link found!`"
61+
await event.edit(reply)
62+
await event.edit("`Downloading...`")
63+
reply += mega_download(link)
64+
await event.edit(reply)
65+
66+
67+
def mega_download(url: str) -> str:
68+
reply = ''
69+
try:
70+
link = re.findall(r'\bhttps?://.*mega.*\.nz\S+', url)[0]
71+
except IndexError:
72+
reply = "`No MEGA.nz link found`\n"
73+
return reply
74+
cmd = f'bin/megadirect {link}'
75+
result = subprocess_run(cmd)
76+
try:
77+
data = json.loads(result[0])
78+
print(data)
79+
except json.JSONDecodeError:
80+
reply += "`Error: Can't extract the link`\n"
81+
return reply
82+
file_name = data['file_name']
83+
file_size = naturalsize(int(data['file_size']))
84+
file_url = data['url']
85+
file_hex = data['hex']
86+
file_raw_hex = data['raw_hex']
87+
if wget.download(file_url, out=file_name):
88+
encrypt_file(file_name, file_hex, file_raw_hex)
89+
reply += (f"`{file_name}`\n"
90+
f"Size: {file_size}\n"
91+
"\n"
92+
"Successfully downloaded...")
93+
else:
94+
reply += "Failed to download..."
95+
return reply
96+
97+
98+
def encrypt_file(file_name, file_hex, file_raw_hex):
99+
os.rename(file_name, f"old_{file_name}")
100+
cmd = f"cat old_{file_name} | openssl enc -d -aes-128-ctr -K {file_hex} -iv {file_raw_hex} > {file_name}"
101+
subprocess_run(cmd)
102+
os.remove(f"old_{file_name}")
103+
return
104+
105+
106+
CMD_HELP.update({
107+
"mega":
108+
".mega <mega url>\"
109+
"\nUsage: Reply to a mega link or paste your mega link to"
110+
"\ndownload the file into your userbot server"
111+
"\n\nOnly support for *FILE* only."
112+
})

0 commit comments

Comments
 (0)