|
| 1 | +# Copyright (C) 2019 The Raphielscape Company LLC. |
| 2 | +# |
| 3 | +# Licensed under the Raphielscape Public License, Version 1.d (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# |
| 6 | +""" Userbot module containing various sites direct links generators""" |
| 7 | + |
| 8 | +import json |
| 9 | +import re |
| 10 | +import urllib.parse |
| 11 | +from random import choice |
| 12 | +from subprocess import PIPE, Popen |
| 13 | + |
| 14 | +import requests |
| 15 | +from humanize import naturalsize |
| 16 | + |
| 17 | +from pyrogram import Client, enums, filters |
| 18 | +from pyrogram.types import Message |
| 19 | + |
| 20 | +from utils.misc import modules_help, prefix |
| 21 | +from utils.scripts import import_library |
| 22 | + |
| 23 | +bs4 = import_library("beautifulsoup4", "BeautifulSoup") |
| 24 | + |
| 25 | + |
| 26 | +def subprocess_run(cmd): |
| 27 | + reply = "" |
| 28 | + cmd_args = cmd.split() |
| 29 | + subproc = Popen( |
| 30 | + cmd_args, |
| 31 | + stdout=PIPE, |
| 32 | + stderr=PIPE, |
| 33 | + universal_newlines=True, |
| 34 | + executable="bash", |
| 35 | + ) |
| 36 | + talk = subproc.communicate() |
| 37 | + exitCode = subproc.returncode |
| 38 | + if exitCode != 0: |
| 39 | + reply += ( |
| 40 | + "```An error was detected while running the subprocess:\n" |
| 41 | + f"exit code: {exitCode}\n" |
| 42 | + f"stdout: {talk[0]}\n" |
| 43 | + f"stderr: {talk[1]}```" |
| 44 | + ) |
| 45 | + return reply |
| 46 | + return talk |
| 47 | + |
| 48 | + |
| 49 | +@Client.on_message(filters.command("direct", prefix) & filters.me) |
| 50 | +async def direct_link_generator(_, m: Message): |
| 51 | + if len(m.command) > 1: |
| 52 | + message = m.text.split(maxsplit=1)[1] |
| 53 | + elif m.reply_to_message: |
| 54 | + message = m.reply_to_message.text |
| 55 | + else: |
| 56 | + await m.edit(f"<b>Usage: </b><code>{prefix}direct [url]</code>") |
| 57 | + return |
| 58 | + reply = "" |
| 59 | + links = re.findall(r"\bhttps?://.*\.\S+", message) |
| 60 | + if not links: |
| 61 | + reply = "`No links found!`" |
| 62 | + await m.edit(reply, parse_mode=enums.ParseMode.MARKDOWN) |
| 63 | + for link in links: |
| 64 | + if "drive.google.com" in link: |
| 65 | + reply += gdrive(link) |
| 66 | + elif "yadi.sk" in link: |
| 67 | + reply += yandex_disk(link) |
| 68 | + elif "cloud.mail.ru" in link: |
| 69 | + reply += cm_ru(link) |
| 70 | + elif "mediafire.com" in link: |
| 71 | + reply += mediafire(link) |
| 72 | + elif "sourceforge.net" in link: |
| 73 | + reply += sourceforge(link) |
| 74 | + elif "osdn.net" in link: |
| 75 | + reply += osdn(link) |
| 76 | + elif "androidfilehost.com" in link: |
| 77 | + reply += androidfilehost(link) |
| 78 | + else: |
| 79 | + reply += re.findall(r"\bhttps?://(.*?[^/]+)", link)[0] + " is not supported" |
| 80 | + await m.edit(reply, parse_mode=enums.ParseMode.MARKDOWN) |
| 81 | + |
| 82 | + |
| 83 | +def gdrive(url: str) -> str: |
| 84 | + """GDrive direct links generator""" |
| 85 | + drive = "https://drive.google.com" |
| 86 | + try: |
| 87 | + link = re.findall(r"\bhttps?://drive\.google\.com\S+", url)[0] |
| 88 | + except IndexError: |
| 89 | + reply = "`No Google drive links found`\n" |
| 90 | + return reply |
| 91 | + file_id = "" |
| 92 | + reply = "" |
| 93 | + if link.find("view") != -1: |
| 94 | + file_id = link.split("/")[-2] |
| 95 | + elif link.find("open?id=") != -1: |
| 96 | + file_id = link.split("open?id=")[1].strip() |
| 97 | + elif link.find("uc?id=") != -1: |
| 98 | + file_id = link.split("uc?id=")[1].strip() |
| 99 | + url = f"{drive}/uc?export=download&id={file_id}" |
| 100 | + download = requests.get(url, stream=True, allow_redirects=False) |
| 101 | + cookies = download.cookies |
| 102 | + try: |
| 103 | + # In case of small file size, Google downloads directly |
| 104 | + dl_url = download.headers["location"] |
| 105 | + page = BeautifulSoup(download.content, "html.parser") |
| 106 | + if "accounts.google.com" in dl_url: # non-public file |
| 107 | + reply += "`Link is not public!`\n" |
| 108 | + return reply |
| 109 | + name = "Direct Download Link" |
| 110 | + except KeyError: |
| 111 | + # In case of download warning page |
| 112 | + page = BeautifulSoup(download.content, "html.parser") |
| 113 | + if download.headers is not None: |
| 114 | + dl_url = download.headers.get("location") |
| 115 | + page_element = page.find("a", {"id": "uc-download-link"}) |
| 116 | + if page_element is not None: |
| 117 | + export = drive + page_element.get("href") |
| 118 | + name = page.find("span", {"class": "uc-name-size"}).text |
| 119 | + response = requests.get( |
| 120 | + export, stream=True, allow_redirects=False, cookies=cookies |
| 121 | + ) |
| 122 | + dl_url = response.headers["location"] |
| 123 | + if "accounts.google.com" in dl_url: |
| 124 | + name = page.find("span", {"class": "uc-name-size"}).text |
| 125 | + reply += "Link is not public!" |
| 126 | + return reply |
| 127 | + if "=sharing" in dl_url: |
| 128 | + name = page.find("span", {"class": "uc-name-size"}).text |
| 129 | + reply += "```Provide GDrive Link not directc sharing of GDrive!```" |
| 130 | + return reply |
| 131 | + |
| 132 | + reply += f"[{name}]({dl_url})\n" |
| 133 | + return reply |
| 134 | + |
| 135 | + |
| 136 | +def yandex_disk(url: str) -> str: |
| 137 | + """Yandex.Disk direct links generator |
| 138 | + Based on https://github.com/wldhx/yadisk-direct""" |
| 139 | + reply = "" |
| 140 | + try: |
| 141 | + link = re.findall(r"\bhttps?://.*yadi\.sk\S+", url)[0] |
| 142 | + except IndexError: |
| 143 | + reply = "`No Yandex.Disk links found`\n" |
| 144 | + return reply |
| 145 | + api = "https://cloud-api.yandex.net/v1/disk/public/resources/download?public_key={}" |
| 146 | + try: |
| 147 | + dl_url = requests.get(api.format(link)).json()["href"] |
| 148 | + name = dl_url.split("filename=")[1].split("&disposition")[0] |
| 149 | + reply += f"[{name}]({dl_url})\n" |
| 150 | + except KeyError: |
| 151 | + reply += "`Error: File not found / Download limit reached`\n" |
| 152 | + return reply |
| 153 | + return reply |
| 154 | + |
| 155 | + |
| 156 | +def cm_ru(url: str) -> str: |
| 157 | + """cloud.mail.ru direct links generator |
| 158 | + Using https://github.com/JrMasterModelBuilder/cmrudl.py""" |
| 159 | + reply = "" |
| 160 | + try: |
| 161 | + link = re.findall(r"\bhttps?://.*cloud\.mail\.ru\S+", url)[0] |
| 162 | + except IndexError: |
| 163 | + reply = "`No cloud.mail.ru links found`\n" |
| 164 | + return reply |
| 165 | + cmd = f"bin/cmrudl -s {link}" |
| 166 | + result = subprocess_run(cmd) |
| 167 | + try: |
| 168 | + result = result[0].splitlines()[-1] |
| 169 | + data = json.loads(result) |
| 170 | + except json.decoder.JSONDecodeError: |
| 171 | + reply += "`Error: Can't extract the link`\n" |
| 172 | + return reply |
| 173 | + except IndexError: |
| 174 | + return reply |
| 175 | + dl_url = data["download"] |
| 176 | + name = data["file_name"] |
| 177 | + size = naturalsize(int(data["file_size"])) |
| 178 | + reply += f"[{name} ({size})]({dl_url})\n" |
| 179 | + return reply |
| 180 | + |
| 181 | + |
| 182 | +def mediafire(url: str) -> str: |
| 183 | + """MediaFire direct links generator""" |
| 184 | + try: |
| 185 | + link = re.findall(r"\bhttps?://.*mediafire\.com\S+", url)[0] |
| 186 | + except IndexError: |
| 187 | + reply = "`No MediaFire links found`\n" |
| 188 | + return reply |
| 189 | + reply = "" |
| 190 | + page = BeautifulSoup(requests.get(link).content, "lxml") |
| 191 | + info = page.find("a", {"aria-label": "Download file"}) |
| 192 | + dl_url = info.get("href") |
| 193 | + size = re.findall(r"\(.*\)", info.text)[0] |
| 194 | + name = page.find("div", {"class": "filename"}).text |
| 195 | + reply += f"[{name} {size}]({dl_url})\n" |
| 196 | + return reply |
| 197 | + |
| 198 | + |
| 199 | +def sourceforge(url: str) -> str: |
| 200 | + """SourceForge direct links generator""" |
| 201 | + try: |
| 202 | + link = re.findall(r"\bhttps?://.*sourceforge\.net\S+", url)[0] |
| 203 | + except IndexError: |
| 204 | + reply = "`No SourceForge links found`\n" |
| 205 | + return reply |
| 206 | + file_path = re.findall(r"files(.*)/download", link)[0] |
| 207 | + reply = f"Mirrors for __{file_path.split('/')[-1]}__\n" |
| 208 | + project = re.findall(r"projects?/(.*?)/files", link)[0] |
| 209 | + mirrors = ( |
| 210 | + f"https://sourceforge.net/settings/mirror_choices?" |
| 211 | + f"projectname={project}&filename={file_path}" |
| 212 | + ) |
| 213 | + page = BeautifulSoup(requests.get(mirrors).content, "html.parser") |
| 214 | + info = page.find("ul", {"id": "mirrorList"}).findAll("li") |
| 215 | + for mirror in info[1:]: |
| 216 | + name = re.findall(r"\((.*)\)", mirror.text.strip())[0] |
| 217 | + dl_url = ( |
| 218 | + f'https://{mirror["id"]}.dl.sourceforge.net/project/{project}/{file_path}' |
| 219 | + ) |
| 220 | + reply += f"[{name}]({dl_url}) " |
| 221 | + return reply |
| 222 | + |
| 223 | + |
| 224 | +def osdn(url: str) -> str: |
| 225 | + """OSDN direct links generator""" |
| 226 | + osdn_link = "https://osdn.net" |
| 227 | + try: |
| 228 | + link = re.findall(r"\bhttps?://.*osdn\.net\S+", url)[0] |
| 229 | + except IndexError: |
| 230 | + reply = "`No OSDN links found`\n" |
| 231 | + return reply |
| 232 | + page = BeautifulSoup(requests.get(link, allow_redirects=True).content, "lxml") |
| 233 | + info = page.find("a", {"class": "mirror_link"}) |
| 234 | + link = urllib.parse.unquote(osdn_link + info["href"]) |
| 235 | + reply = f"Mirrors for __{link.split('/')[-1]}__\n" |
| 236 | + mirrors = page.find("form", {"id": "mirror-select-form"}).findAll("tr") |
| 237 | + for data in mirrors[1:]: |
| 238 | + mirror = data.find("input")["value"] |
| 239 | + name = re.findall(r"\((.*)\)", data.findAll("td")[-1].text.strip())[0] |
| 240 | + dl_url = re.sub(r"m=(.*)&f", f"m={mirror}&f", link) |
| 241 | + reply += f"[{name}]({dl_url}) " |
| 242 | + return reply |
| 243 | + |
| 244 | + |
| 245 | +def androidfilehost(url: str) -> str: |
| 246 | + """AFH direct links generator""" |
| 247 | + try: |
| 248 | + link = re.findall(r"\bhttps?://.*androidfilehost.*fid.*\S+", url)[0] |
| 249 | + except IndexError: |
| 250 | + reply = "`No AFH links found`\n" |
| 251 | + return reply |
| 252 | + fid = re.findall(r"\?fid=(.*)", link)[0] |
| 253 | + session = requests.Session() |
| 254 | + user_agent = useragent() |
| 255 | + headers = {"user-agent": user_agent} |
| 256 | + res = session.get(link, headers=headers, allow_redirects=True) |
| 257 | + headers = { |
| 258 | + "origin": "https://androidfilehost.com", |
| 259 | + "accept-encoding": "gzip, deflate, br", |
| 260 | + "accept-language": "en-US,en;q=0.9", |
| 261 | + "user-agent": user_agent, |
| 262 | + "content-type": "application/x-www-form-urlencoded; charset=UTF-8", |
| 263 | + "x-mod-sbb-ctype": "xhr", |
| 264 | + "accept": "*/*", |
| 265 | + "referer": f"https://androidfilehost.com/?fid={fid}", |
| 266 | + "authority": "androidfilehost.com", |
| 267 | + "x-requested-with": "XMLHttpRequest", |
| 268 | + } |
| 269 | + data = {"submit": "submit", "action": "getdownloadmirrors", "fid": f"{fid}"} |
| 270 | + mirrors = None |
| 271 | + reply = "" |
| 272 | + error = "`Error: Can't find Mirrors for the link`\n" |
| 273 | + try: |
| 274 | + req = session.post( |
| 275 | + "https://androidfilehost.com/libs/otf/mirrors.otf.php", |
| 276 | + headers=headers, |
| 277 | + data=data, |
| 278 | + cookies=res.cookies, |
| 279 | + ) |
| 280 | + mirrors = req.json()["MIRRORS"] |
| 281 | + except (json.decoder.JSONDecodeError, TypeError): |
| 282 | + reply += error |
| 283 | + if not mirrors: |
| 284 | + reply += error |
| 285 | + return reply |
| 286 | + for item in mirrors: |
| 287 | + name = item["name"] |
| 288 | + dl_url = item["url"] |
| 289 | + reply += f"[{name}]({dl_url}) " |
| 290 | + return reply |
| 291 | + |
| 292 | + |
| 293 | +def useragent(): |
| 294 | + """ |
| 295 | + useragent random setter |
| 296 | + """ |
| 297 | + useragents = BeautifulSoup( |
| 298 | + requests.get( |
| 299 | + "https://developers.whatismybrowser.com/" |
| 300 | + "useragents/explore/operating_system_name/android/" |
| 301 | + ).content, |
| 302 | + "lxml", |
| 303 | + ).findAll("td", {"class": "useragent"}) |
| 304 | + if not useragents: |
| 305 | + return "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.3" |
| 306 | + user_agent = choice(useragents) |
| 307 | + return user_agent.text |
| 308 | + |
| 309 | + |
| 310 | +modules_help["direct"] = { |
| 311 | + "direct": "Url/reply to Url\ |
| 312 | +\n\n<b>Syntax : </b><code>.direct [url/reply] </code>\ |
| 313 | +\n<b>Usage :</b> Generates direct download link from supported URL(s)\ |
| 314 | +\n\n<b>Supported websites : </b><code>Google Drive - MEGA.nz - Cloud Mail - Yandex.Disk - AFH - MediaFire - SourceForge - OSDN</code>" |
| 315 | +} |
0 commit comments