-
Notifications
You must be signed in to change notification settings - Fork 5
/
direct_link.py
361 lines (331 loc) · 12.1 KB
/
direct_link.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
# 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.
import json
import re
import urllib.parse
from os import popen
from random import choice
import requests
from bs4 import BeautifulSoup
from humanize import naturalsize
from ULTRA.utils import admin_cmd, edit_or_reply, sudo_cmd
from ULTRA import CMD_HELP
@bot.on(admin_cmd(outgoing=True, pattern=r"direct(?: |$)([\s\S]*)"))
@bot.on(sudo_cmd(allow_sudo=True, pattern=r"direct(?: |$)([\s\S]*)"))
async def direct_link_generator(request):
""" direct links generator """
hellevent = await edit_or_reply(request, "`Processing...`")
textx = await request.get_reply_message()
message = request.pattern_match.group(1)
if message:
pass
elif textx:
message = textx.text
else:
await hellevent.edit("`Usage: .direct <url>`")
return
reply = ""
links = re.findall(r"\bhttps?://.*\.\S+", message)
if not links:
reply = "`No links found!`"
await hellevent.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 "mega." in link:
reply += mega_dl(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 hellevent.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:
"""ZippyShare direct links generator
Based on https://github.com/LameLemon/ziggy"""
reply = ""
dl_url = ""
try:
link = re.findall(r"\bhttps?://.*zippyshare\.com\S+", url)[0]
except IndexError:
reply = "`No ZippyShare links found`\n"
return reply
session = requests.Session()
base_url = re.search("http.+.com", link).group()
response = session.get(link)
page_soup = BeautifulSoup(response.content, "lxml")
scripts = page_soup.find_all("script", {"type": "text/javascript"})
for script in scripts:
if "getElementById('dlbutton')" in script.text:
url_raw = re.search(
r"= (?P<url>\".+\" \+ (?P<math>\(.+\)) .+);", script.text
).group("url")
math = re.search(
r"= (?P<url>\".+\" \+ (?P<math>\(.+\)) .+);", script.text
).group("math")
dl_url = url_raw.replace(math, '"' + str(eval(math)) + '"')
break
dl_url = base_url + eval(dl_url)
name = urllib.parse.unquote(dl_url.split("/")[-1])
reply += f"[{name}]({dl_url})\n"
return reply
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 mega_dl(url: str) -> str:
"""MEGA.nz direct links generator
Using https://github.com/tonikelope/megadown"""
reply = ""
try:
link = re.findall(r"\bhttps?://.*mega.*\.nz\S+", url)[0]
except IndexError:
reply = "`No MEGA.nz links found`\n"
return reply
command = f"bin/megadown -q -m {link}"
result = popen(command).read()
try:
data = json.loads(result)
print(data)
except json.JSONDecodeError:
reply += "`Error: Can't extract the link`\n"
return reply
dl_url = data["url"]
name = data["file_name"]
size = naturalsize(int(data["file_size"]))
reply += f"[{name} ({size})]({dl_url})\n"
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
command = f"bin/cmrudl -s {link}"
result = popen(command).read()
result = result.splitlines()[-1]
try:
data = json.loads(result)
except json.decoder.JSONDecodeError:
reply += "`Error: Can't extract the link`\n"
return reply
dl_url = data["download"]
name = data["file_name"]
size = naturalsize(int(data["file_size"]))
reply += f"[{name} ({size})]({dl_url})\n"
return reply
def mediafire(url: str) -> str:
""" MediaFire direct links generator """
try:
link = re.findall(r"\bhttps?://.*mediafire\.com\S+", url)[0]
except IndexError:
reply = "`No MediaFire links found`\n"
return reply
reply = ""
page = BeautifulSoup(requests.get(link).content, "lxml")
info = page.find("a", {"aria-label": "Download file"})
dl_url = info.get("href")
size = re.findall(r"\(.*\)", info.text)[0]
name = page.find("div", {"class": "filename"}).text
reply += f"[{name} {size}]({dl_url})\n"
return reply
def sourceforge(url: str) -> str:
""" SourceForge direct links generator """
try:
link = re.findall(r"\bhttps?://.*sourceforge\.net\S+", url)[0]
except IndexError:
reply = "`No SourceForge links found`\n"
return reply
file_path = re.findall(r"files(.*)/download", link)[0]
reply = f"Mirrors for __{file_path.split('/')[-1]}__\n"
project = re.findall(r"projects?/(.*?)/files", link)[0]
mirrors = (
f"https://sourceforge.net/settings/mirror_choices?"
f"projectname={project}&filename={file_path}"
)
page = BeautifulSoup(requests.get(mirrors).content, "html.parser")
info = page.find("ul", {"id": "mirrorList"}).findAll("li")
for mirror in info[1:]:
name = re.findall(r"\((.*)\)", mirror.text.strip())[0]
dl_url = (
f'https://{mirror["id"]}.dl.sourceforge.net/project/{project}/{file_path}'
)
reply += f"[{name}]({dl_url}) "
return reply
def osdn(url: str) -> str:
""" OSDN direct links generator """
osdn_link = "https://osdn.net"
try:
link = re.findall(r"\bhttps?://.*osdn\.net\S+", url)[0]
except IndexError:
reply = "`No OSDN links found`\n"
return reply
page = BeautifulSoup(requests.get(link, allow_redirects=True).content, "lxml")
info = page.find("a", {"class": "mirror_link"})
link = urllib.parse.unquote(osdn_link + info["href"])
reply = f"Mirrors for __{link.split('/')[-1]}__\n"
mirrors = page.find("form", {"id": "mirror-select-form"}).findAll("tr")
for data in mirrors[1:]:
mirror = data.find("input")["value"]
name = re.findall(r"\((.*)\)", data.findAll("td")[-1].text.strip())[0]
dl_url = re.sub(r"m=(.*)&f", f"m={mirror}&f", link)
reply += f"[{name}]({dl_url}) "
return reply
def github(url: str) -> str:
""" GitHub direct links generator """
try:
link = re.findall(r"\bhttps?://.*github\.com.*releases\S+", url)[0]
except IndexError:
reply = "`No GitHub Releases links found`\n"
return reply
reply = ""
dl_url = ""
download = requests.get(url, stream=True, allow_redirects=False)
try:
dl_url = download.headers["location"]
except KeyError:
reply += "`Error: Can't extract the link`\n"
name = link.split("/")[-1]
reply += f"[{name}]({dl_url}) "
return reply
def androidfilehost(url: str) -> str:
""" AFH direct links generator """
try:
link = re.findall(r"\bhttps?://.*androidfilehost.*fid.*\S+", url)[0]
except IndexError:
reply = "`No AFH links found`\n"
return reply
fid = re.findall(r"\?fid=(.*)", link)[0]
session = requests.Session()
user_agent = useragent()
headers = {"user-agent": user_agent}
res = session.get(link, headers=headers, allow_redirects=True)
headers = {
"origin": "https://androidfilehost.com",
"accept-encoding": "gzip, deflate, br",
"accept-language": "en-US,en;q=0.9",
"user-agent": user_agent,
"content-type": "application/x-www-form-urlencoded; charset=UTF-8",
"x-mod-sbb-ctype": "xhr",
"accept": "*/*",
"referer": f"https://androidfilehost.com/?fid={fid}",
"authority": "androidfilehost.com",
"x-requested-with": "XMLHttpRequest",
}
data = {"submit": "submit", "action": "getdownloadmirrors", "fid": f"{fid}"}
mirrors = None
reply = ""
error = "`Error: Can't find Mirrors for the link`\n"
try:
req = session.post(
"https://androidfilehost.com/libs/otf/mirrors.otf.php",
headers=headers,
data=data,
cookies=res.cookies,
)
mirrors = req.json()["MIRRORS"]
except (json.decoder.JSONDecodeError, TypeError):
reply += error
if not mirrors:
reply += error
return reply
for item in mirrors:
name = item["name"]
dl_url = item["url"]
reply += f"[{name}]({dl_url}) "
return reply
def useragent():
"""
useragent random setter
"""
useragents = BeautifulSoup(
requests.get(
"https://developers.whatismybrowser.com/"
"useragents/explore/operating_system_name/android/"
).content,
"lxml",
).findAll("td", {"class": "useragent"})
user_agent = choice(useragents)
return user_agent.text
CMD_HELP.update(
{
"direct_links": "**Plugin : **`direct`\
\n\n**Syntax : **`.direct <url>`\n"
"**Function : **Reply to a link or paste a URL to\n"
"generate a direct download link\n\n"
"List of supported URLs:\n"
"`Google Drive - Cloud Mail - Yandex.Disk - AFH - "
"ZippyShare - MediaFire - SourceForge - OSDN - GitHub`"
}
)