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+ " \n Usage : Reply to a mega link or paste your mega link to "
110+ " \n download the file into your userbot server "
111+ " \n \n Only support for * FILE * only ."
112+ })
0 commit comments