Skip to content

Commit

Permalink
Terabox fix
Browse files Browse the repository at this point in the history
Added 2 more apis for redundancy

Signed-off-by: Dawn India <[email protected]>
  • Loading branch information
Dawn-India committed Aug 18, 2024
1 parent cb8dcfc commit 065617b
Showing 1 changed file with 50 additions and 30 deletions.
80 changes: 50 additions & 30 deletions bot/helper/task_utils/download_utils/direct_link_generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,17 @@
from json import loads
from lxml.etree import HTML
from os import path as ospath
from re import findall, match, search
from requests import Session, post, get
from re import (
findall,
match,
search
)
from requests import (
Session,
post,
get,
RequestException
)
from requests.adapters import HTTPAdapter
from time import sleep
from urllib.parse import (
Expand All @@ -16,7 +25,7 @@
from uuid import uuid4
from base64 import b64decode

from bot import LOGGER, config_dict
from bot import config_dict
from bot.helper.ext_utils.exceptions import DirectDownloadLinkException
from bot.helper.ext_utils.help_messages import PASSWORD_ERROR_MESSAGE
from bot.helper.ext_utils.links_utils import is_share_link
Expand Down Expand Up @@ -640,32 +649,25 @@ def uploadee(url):
raise DirectDownloadLinkException("ERROR: Direct Link not found")


def terabox(url):
def terabox(url, video_quality="HD Video", save_dir="HD_Video"):
"""Terabox direct link generator
By: https://github.com/Dawn-India/Z-Mirror"""
https://github.com/Dawn-India/Z-Mirror"""

pattern1 = r"/s/(\w+)"
pattern2 = r"surl=(\w+)"

if not (
search(
pattern1,
url
) or search(
pattern2,
url
)
):
pattern = r"/s/(\w+)|surl=(\w+)"
if not search(pattern, url):
raise DirectDownloadLinkException("ERROR: Invalid terabox URL")

netloc = urlparse(url).netloc
url = url.replace(
terabox_url = url.replace(
netloc,
"1024tera.com"
)
response = get(url)
if response.status_code != 200:
raise DirectDownloadLinkException("ERROR: Unable to fetch the webpage")

urls = [
"https://ytshorts.savetube.me/api/v1/terabox-downloader",
f"https://teraboxvideodownloader.nepcoderdevs.workers.dev/?url={terabox_url}",
f"https://terabox.udayscriptsx.workers.dev/?url={terabox_url}"
]

headers = {
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:126.0) Gecko/20100101 Firefox/126.0",
Expand All @@ -679,37 +681,55 @@ def terabox(url):
"Sec-Fetch-Site": "same-origin"
}

response = post(
"https://ytshorts.savetube.me/api/v1/terabox-downloader",
headers=headers,
json={"url": url}
)
if response.status_code != 200:
for base_url in urls:
try:
if "api/v1" in base_url:
response = post(
base_url,
headers=headers,
json={"url": terabox_url}
)
else:
response = get(base_url)

if response.status_code == 200:
break
except RequestException as e:
raise DirectDownloadLinkException(f"ERROR: {e.__class__.__name__}") from e
else:
raise DirectDownloadLinkException("ERROR: Unable to fetch the JSON data")

data = response.json()
details = {"contents": [], "title": "", "total_size": 0}
details = {
"contents": [],
"title": "",
"total_size": 0
}

for item in data["response"]:
title = item["title"]
resolutions = item.get(
"resolutions",
{}
)
zlink = resolutions.get("HD Video")
zlink = resolutions.get(video_quality)
if zlink:
details["contents"].append({
"url": zlink,
"filename": title,
"path": ospath.join(
title,
"HD_Video"
save_dir
)
})
details["title"] = title

if not details["contents"]:
raise DirectDownloadLinkException("ERROR: No valid download links found")

if len(details["contents"]) == 1:
return details["contents"][0]["url"]

return details


Expand Down

0 comments on commit 065617b

Please sign in to comment.