From 3ba151c43899a5b16d86356c4bbc0cba0f273fe1 Mon Sep 17 00:00:00 2001 From: Dawn India Date: Thu, 28 Mar 2024 15:37:19 +0530 Subject: [PATCH] Minor fix Fix gofile & qiwi link generator. --- .../download_utils/direct_link_generator.py | 92 +++++++++++-------- 1 file changed, 53 insertions(+), 39 deletions(-) diff --git a/bot/helper/mirror_utils/download_utils/direct_link_generator.py b/bot/helper/mirror_utils/download_utils/direct_link_generator.py index 2ac55d12b393..d70e9eb1e250 100644 --- a/bot/helper/mirror_utils/download_utils/direct_link_generator.py +++ b/bot/helper/mirror_utils/download_utils/direct_link_generator.py @@ -21,6 +21,9 @@ from bot.helper.ext_utils.help_messages import PASSWORD_ERROR_MESSAGE _caches = {} +user_agent = ( + "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/36.0.1985.125 Safari/537.36" +) def direct_link_generator(link): @@ -370,7 +373,7 @@ def solidfiles(url): with create_scraper() as session: try: headers = { - 'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/36.0.1985.125 Safari/537.36' + 'User-Agent': user_agent } pageSource = session.get(url, headers=headers).text mainOptions = str( @@ -562,7 +565,7 @@ def sharer_scraper(url): try: url = cget('GET', url).url raw = urlparse(url) - header = {"useragent": "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US) AppleWebKit/534.10 (KHTML, like Gecko) Chrome/7.0.548.0 Safari/534.10"} + header = {"useragent": user_agent} res = cget('GET', url, headers=header) except Exception as e: raise DirectDownloadLinkException(f'ERROR: {e.__class__.__name__}') @@ -576,7 +579,7 @@ def sharer_scraper(url): headers = { 'Content-Type': f'multipart/form-data; boundary=----WebKitFormBoundary{boundary}', 'x-token': raw.hostname, - 'useragent': 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US) AppleWebKit/534.10 (KHTML, like Gecko) Chrome/7.0.548.0 Safari/534.10' + 'useragent': user_agent } data = f'------WebKitFormBoundary{boundary}\r\nContent-Disposition: form-data; name="action"\r\n\r\ndirect\r\n' \ @@ -755,88 +758,99 @@ def gofile(url): _password = sha256(_password.encode("utf-8")).hexdigest() url = url.split("::")[-2] else: - _password = '' + _password = "" _id = url.split("/")[-1] except Exception as e: raise DirectDownloadLinkException(f"ERROR: {e.__class__.__name__}") def __get_token(session): - if 'gofile_token' in _caches: - __url = f"https://api.gofile.io/getAccountDetails?token={_caches['gofile_token']}" - else: - __url = 'https://api.gofile.io/createAccount' + headers = { + "User-Agent": user_agent, + "Accept-Encoding": "gzip, deflate, br", + "Accept": "*/*", + "Connection": "keep-alive", + } + __url = f"https://api.gofile.io/accounts" try: - __res = session.get(__url, verify=False).json() - if __res["status"] != 'ok': - if 'gofile_token' in _caches: - del _caches['gofile_token'] - return __get_token(session) - _caches['gofile_token'] = __res["data"]["token"] - return _caches['gofile_token'] + __res = session.post(__url, headers=headers).json() + if __res["status"] != "ok": + raise DirectDownloadLinkException(f"ERROR: Failed to get token.") + return __res["data"]["token"] except Exception as e: raise e - def __fetch_links(session, _id, folderPath=''): - _url = f"https://api.gofile.io/getContent?contentId={_id}&token={token}&wt=4fd6sg89d7s6&cache=true" + def __fetch_links(session, _id, folderPath=""): + _url = f"https://api.gofile.io/contents/{_id}?wt=4fd6sg89d7s6&cache=true" + headers = { + "User-Agent": user_agent, + "Accept-Encoding": "gzip, deflate, br", + "Accept": "*/*", + "Connection": "keep-alive", + "Authorization": "Bearer" + " " + token, + } if _password: _url += f"&password={_password}" try: - _json = session.get(_url, verify=False).json() + _json = session.get(_url, headers=headers).json() except Exception as e: raise DirectDownloadLinkException(f"ERROR: {e.__class__.__name__}") - if _json['status'] in 'error-passwordRequired': - raise DirectDownloadLinkException(f"ERROR:\n{PASSWORD_ERROR_MESSAGE.format(url)}") - if _json['status'] in 'error-passwordWrong': - raise DirectDownloadLinkException('ERROR: This password is wrong !') - if _json['status'] in 'error-notFound': - raise DirectDownloadLinkException("ERROR: File not found on gofile's server") - if _json['status'] in 'error-notPublic': + if _json["status"] in "error-passwordRequired": + raise DirectDownloadLinkException( + f"ERROR:\n{PASSWORD_ERROR_MESSAGE.format(url)}" + ) + if _json["status"] in "error-passwordWrong": + raise DirectDownloadLinkException("ERROR: This password is wrong !") + if _json["status"] in "error-notFound": + raise DirectDownloadLinkException( + "ERROR: File not found on gofile's server" + ) + if _json["status"] in "error-notPublic": raise DirectDownloadLinkException("ERROR: This folder is not public") data = _json["data"] - if not details['title']: - details['title'] = data['name'] if data['type'] == "folder" else _id + if not details["title"]: + details["title"] = data["name"] if data["type"] == "folder" else _id - contents = data["contents"] + contents = data["children"] for content in contents.values(): if content["type"] == "folder": - if not content['public']: + if not content["public"]: continue if not folderPath: - newFolderPath = path.join(details['title'], content["name"]) + newFolderPath = path.join(details["title"], content["name"]) else: newFolderPath = path.join(folderPath, content["name"]) __fetch_links(session, content["id"], newFolderPath) else: if not folderPath: - folderPath = details['title'] + folderPath = details["title"] item = { "path": path.join(folderPath), "filename": content["name"], "url": content["link"], } - if 'size' in content: + if "size" in content: size = content["size"] if isinstance(size, str) and size.isdigit(): size = float(size) - details['total_size'] += size - details['contents'].append(item) + details["total_size"] += size + details["contents"].append(item) - details = {'contents':[], 'title': '', 'total_size': 0} + details = {"contents": [], "title": "", "total_size": 0} with Session() as session: try: token = __get_token(session) except Exception as e: raise DirectDownloadLinkException(f"ERROR: {e.__class__.__name__}") - details["header"] = f'Cookie: accountToken={token}' + details["header"] = f"Cookie: accountToken={token}" try: __fetch_links(session, _id) except Exception as e: raise DirectDownloadLinkException(e) - if len(details['contents']) == 1: - return (details['contents'][0]['url'], details['header']) + if len(details["contents"]) == 1: + return (details["contents"][0]["url"], details["header"]) return details def mediafireFolder(url): @@ -1305,7 +1319,7 @@ def qiwi(url): if name := tree.xpath('//h1[@class="page_TextHeading__VsM7r"]/text()'): ext = name[0].split('.')[-1] - return f"https://qiwi.lol/{file_id}.{ext}" + return f"https://spyderrock.com/{file_id}.{ext}" else: raise DirectDownloadLinkException("ERROR: File not found")