teamx-cloner commited on
Commit
4d322fb
·
1 Parent(s): 86254c2

Create download-upload.py

Browse files
Files changed (1) hide show
  1. plugins/download-upload.py +223 -0
plugins/download-upload.py ADDED
@@ -0,0 +1,223 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Ultroid - UserBot
2
+ # Copyright (C) 2021-2023 TeamUltroid
3
+ #
4
+ # This file is a part of < https://github.com/TeamUltroid/Ultroid/ >
5
+ # PLease read the GNU Affero General Public License in
6
+ # <https://www.github.com/TeamUltroid/Ultroid/blob/main/LICENSE/>.
7
+
8
+ from . import get_help
9
+
10
+ __doc__ = get_help("help_dlup")
11
+
12
+ import asyncio
13
+ import glob
14
+ import os
15
+ import time
16
+ from datetime import datetime as dt
17
+
18
+ from aiohttp.client_exceptions import InvalidURL
19
+ from telethon.errors.rpcerrorlist import MessageNotModifiedError
20
+
21
+ from xteam.fns.helper import time_formatter
22
+ from xteam.fns.tools import get_chat_and_msgid, set_attributes
23
+
24
+ from . import (
25
+ LOGS,
26
+ ULTConfig,
27
+ downloader,
28
+ eor,
29
+ fast_download,
30
+ get_all_files,
31
+ get_string,
32
+ progress,
33
+ time_formatter,
34
+ ultroid_cmd,
35
+ )
36
+
37
+
38
+ @ultroid_cmd(
39
+ pattern="download( (.*)|$)",
40
+ )
41
+ async def down(event):
42
+ matched = event.pattern_match.group(1).strip()
43
+ msg = await event.eor(get_string("udl_4"))
44
+ if not matched:
45
+ return await eor(msg, get_string("udl_5"), time=5)
46
+ try:
47
+ splited = matched.split(" | ")
48
+ link = splited[0]
49
+ filename = splited[1]
50
+ except IndexError:
51
+ filename = None
52
+ s_time = time.time()
53
+ try:
54
+ filename, d = await fast_download(
55
+ link,
56
+ filename,
57
+ progress_callback=lambda d, t: asyncio.get_event_loop().create_task(
58
+ progress(
59
+ d,
60
+ t,
61
+ msg,
62
+ s_time,
63
+ f"Downloading from {link}",
64
+ )
65
+ ),
66
+ )
67
+ except InvalidURL:
68
+ return await msg.eor("`Invalid URL provided :(`", time=5)
69
+ await msg.eor(f"`{filename}` `downloaded in {time_formatter(d*1000)}.`")
70
+
71
+
72
+ @ultroid_cmd(
73
+ pattern="dl( (.*)|$)",
74
+ )
75
+ async def download(event):
76
+ match = event.pattern_match.group(1).strip()
77
+ if match and "t.me/" in match:
78
+ chat, msg = get_chat_and_msgid(match)
79
+ if not (chat and msg):
80
+ return await event.eor(get_string("gms_1"))
81
+ match = ""
82
+ ok = await event.client.get_messages(chat, ids=msg)
83
+ elif event.reply_to_msg_id:
84
+ ok = await event.get_reply_message()
85
+ else:
86
+ return await event.eor(get_string("cvt_3"), time=8)
87
+ xx = await event.eor(get_string("com_1"))
88
+ if not (ok and ok.media):
89
+ return await xx.eor(get_string("udl_1"), time=5)
90
+ s = dt.now()
91
+ k = time.time()
92
+ if hasattr(ok.media, "document"):
93
+ file = ok.media.document
94
+ mime_type = file.mime_type
95
+ filename = match or ok.file.name
96
+ if not filename:
97
+ if "audio" in mime_type:
98
+ filename = "audio_" + dt.now().isoformat("_", "seconds") + ".ogg"
99
+ elif "video" in mime_type:
100
+ filename = "video_" + dt.now().isoformat("_", "seconds") + ".mp4"
101
+ try:
102
+ result = await downloader(
103
+ f"resources/downloads/{filename}",
104
+ file,
105
+ xx,
106
+ k,
107
+ f"Downloading {filename}...",
108
+ )
109
+
110
+ except MessageNotModifiedError as err:
111
+ return await xx.edit(str(err))
112
+ file_name = result.name
113
+ else:
114
+ d = "resources/downloads/"
115
+ file_name = await event.client.download_media(
116
+ ok,
117
+ d,
118
+ progress_callback=lambda d, t: asyncio.get_event_loop().create_task(
119
+ progress(
120
+ d,
121
+ t,
122
+ xx,
123
+ k,
124
+ get_string("com_5"),
125
+ ),
126
+ ),
127
+ )
128
+ e = dt.now()
129
+ t = time_formatter(((e - s).seconds) * 1000)
130
+ await xx.eor(get_string("udl_2").format(file_name, t))
131
+
132
+
133
+ @ultroid_cmd(
134
+ pattern="ul( (.*)|$)",
135
+ )
136
+ async def _(event):
137
+ msg = await event.eor(get_string("com_1"))
138
+ match = event.pattern_match.group(1)
139
+ if match:
140
+ match = match.strip()
141
+ if not event.out and match == ".env":
142
+ return await event.reply("`You can't do this...`")
143
+ stream, force_doc, delete, thumb = (
144
+ False,
145
+ True,
146
+ False,
147
+ ULTConfig.thumb,
148
+ )
149
+ if "--stream" in match:
150
+ stream = True
151
+ force_doc = False
152
+ if "--delete" in match:
153
+ delete = True
154
+ if "--no-thumb" in match:
155
+ thumb = None
156
+ arguments = ["--stream", "--delete", "--no-thumb"]
157
+ if any(item in match for item in arguments):
158
+ match = (
159
+ match.replace("--stream", "")
160
+ .replace("--delete", "")
161
+ .replace("--no-thumb", "")
162
+ .strip()
163
+ )
164
+ if match.endswith("/"):
165
+ match += "*"
166
+ results = glob.glob(match)
167
+ if not results and os.path.exists(match):
168
+ results = [match]
169
+ if not results:
170
+ try:
171
+ await event.reply(file=match)
172
+ return await event.try_delete()
173
+ except Exception as er:
174
+ LOGS.exception(er)
175
+ return await msg.eor(get_string("ls1"))
176
+ for result in results:
177
+ if os.path.isdir(result):
178
+ c, s = 0, 0
179
+ for files in get_all_files(result):
180
+ attributes = None
181
+ if stream:
182
+ try:
183
+ attributes = await set_attributes(files)
184
+ except KeyError as er:
185
+ LOGS.exception(er)
186
+ try:
187
+ file, _ = await event.client.fast_uploader(
188
+ files, show_progress=True, event=msg, to_delete=delete
189
+ )
190
+ await event.client.send_file(
191
+ event.chat_id,
192
+ file,
193
+ supports_streaming=stream,
194
+ force_document=force_doc,
195
+ thumb=thumb,
196
+ attributes=attributes,
197
+ caption=f"`Uploaded` `{files}` `in {time_formatter(_*1000)}`",
198
+ reply_to=event.reply_to_msg_id or event,
199
+ )
200
+ s += 1
201
+ except (ValueError, IsADirectoryError):
202
+ c += 1
203
+ break
204
+ attributes = None
205
+ if stream:
206
+ try:
207
+ attributes = await set_attributes(result)
208
+ except KeyError as er:
209
+ LOGS.exception(er)
210
+ file, _ = await event.client.fast_uploader(
211
+ result, show_progress=True, event=msg, to_delete=delete
212
+ )
213
+ await event.client.send_file(
214
+ event.chat_id,
215
+ file,
216
+ supports_streaming=stream,
217
+ force_document=force_doc,
218
+ thumb=thumb,
219
+ attributes=attributes,
220
+ caption=f"`Uploaded` `{result}` `in {time_formatter(_*1000)}`",
221
+ )
222
+ await msg.try_delete()
223
+