2626import wget
2727import os
2828
29+ from os .path import exists
30+
2931from userbot import CMD_HELP
3032from userbot .events import register
3133
3234
3335def subprocess_run (cmd ):
36+ reply = ''
3437 subproc = Popen (cmd , stdout = PIPE , stderr = PIPE ,
3538 shell = True , universal_newlines = True )
3639 talk = subproc .communicate ()
3740 exitCode = subproc .returncode
3841 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 ]))
42+ reply += ('An error was detected while running the subprocess:\n '
43+ f'exit code: { exitCode } \n '
44+ f'stdout: { talk [0 ]} \n '
45+ f'stderr: { talk [1 ]} ' )
46+ return reply
4347 return talk
4448
4549
@@ -75,7 +79,6 @@ def mega_download(url: str) -> str:
7579 result = subprocess_run (cmd )
7680 try :
7781 data = json .loads (result [0 ])
78- print (data )
7982 except json .JSONDecodeError :
8083 reply += "`Error: Can't extract the link`\n "
8184 return reply
@@ -84,22 +87,26 @@ def mega_download(url: str) -> str:
8487 file_url = data ['url' ]
8588 file_hex = data ['hex' ]
8689 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..."
90+ if exists (file_name ):
91+ os .remove (file_name )
92+ if not exists (file_name ):
93+ wget .download (file_url , out = file_name )
94+ if exists (file_name ):
95+ encrypt_file (file_name , file_hex , file_raw_hex )
96+ reply += (f"`{ file_name } `\n "
97+ f"Size: { file_size } \n \n "
98+ "Successfully downloaded..." )
99+ else :
100+ reply += "Failed to download..."
95101 return reply
96102
97103
98104def 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 } "
105+ os .rename (file_name , r"old_{}" .format (file_name ))
106+ cmd = ("cat 'old_{}' | openssl enc -d -aes-128-ctr -K {} -iv {} > '{}'"
107+ .format (file_name , file_hex , file_raw_hex , file_name ))
101108 subprocess_run (cmd )
102- os .remove (f "old_{ file_name } " )
109+ os .remove (r "old_{}" . format ( file_name ) )
103110 return
104111
105112
0 commit comments