Spaces:
Sleeping
Sleeping
Update terabox_utils.py
Browse files- terabox_utils.py +71 -21
terabox_utils.py
CHANGED
|
@@ -1,37 +1,87 @@
|
|
| 1 |
# terabox_utils.py
|
| 2 |
|
|
|
|
|
|
|
| 3 |
import os
|
| 4 |
import asyncio
|
|
|
|
|
|
|
| 5 |
|
| 6 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 7 |
async def extract_terabox_short_id(url: str) -> str | None:
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 11 |
return None
|
| 12 |
|
| 13 |
-
#
|
| 14 |
-
async def get_final_url_and_filename(original_link: str) -> tuple[str, str, str | None]:
|
| 15 |
try:
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 21 |
except Exception as e:
|
| 22 |
-
|
|
|
|
| 23 |
|
| 24 |
-
#
|
| 25 |
-
async def download_terabox_file(bot, chat_id,
|
| 26 |
try:
|
| 27 |
-
|
| 28 |
-
local_filepath = f"downloads/{raw_filename}"
|
| 29 |
-
with open(local_filepath, "wb") as f:
|
| 30 |
-
f.write(b"dummy content") # Replace with actual file download
|
| 31 |
|
| 32 |
-
|
| 33 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 34 |
|
| 35 |
return local_filepath, thumb_path, None
|
|
|
|
| 36 |
except Exception as e:
|
| 37 |
-
|
|
|
|
|
|
| 1 |
# terabox_utils.py
|
| 2 |
|
| 3 |
+
import re
|
| 4 |
+
import aiohttp
|
| 5 |
import os
|
| 6 |
import asyncio
|
| 7 |
+
import logging
|
| 8 |
+
import uuid
|
| 9 |
|
| 10 |
+
logger = logging.getLogger(__name__)
|
| 11 |
+
|
| 12 |
+
# List of supported domains
|
| 13 |
+
TERABOX_DOMAINS = [
|
| 14 |
+
"terabox.com",
|
| 15 |
+
"teraboxapp.com",
|
| 16 |
+
"terasharelink.com",
|
| 17 |
+
"1024tera.com",
|
| 18 |
+
"freeterabox.com",
|
| 19 |
+
"4funbox.com",
|
| 20 |
+
"box-links.com",
|
| 21 |
+
]
|
| 22 |
+
|
| 23 |
+
# --- Extract short_id ---
|
| 24 |
async def extract_terabox_short_id(url: str) -> str | None:
|
| 25 |
+
pattern = re.compile(
|
| 26 |
+
r"https?://(?:"
|
| 27 |
+
+ "|".join(re.escape(domain) for domain in TERABOX_DOMAINS)
|
| 28 |
+
+ r")/s/([a-zA-Z0-9_-]+)"
|
| 29 |
+
)
|
| 30 |
+
match = pattern.search(url)
|
| 31 |
+
if match:
|
| 32 |
+
return match.group(1)
|
| 33 |
return None
|
| 34 |
|
| 35 |
+
# --- Get direct URL and filename ---
|
| 36 |
+
async def get_final_url_and_filename(original_link: str) -> tuple[str | None, str | None, str | None]:
|
| 37 |
try:
|
| 38 |
+
async with aiohttp.ClientSession() as session:
|
| 39 |
+
async with session.get(original_link, allow_redirects=True) as resp:
|
| 40 |
+
if resp.status != 200:
|
| 41 |
+
return None, None, f"Failed to access link: HTTP {resp.status}"
|
| 42 |
+
html = await resp.text()
|
| 43 |
+
|
| 44 |
+
# Try to extract filename and direct URL (depends on Terabox's HTML structure — needs to be robust!)
|
| 45 |
+
file_name_match = re.search(r'"file_name":"(.*?)"', html)
|
| 46 |
+
dlink_match = re.search(r'"dlink":"(https:[^"]+)"', html)
|
| 47 |
+
|
| 48 |
+
if not dlink_match:
|
| 49 |
+
return None, None, "Failed to extract direct link (dlink) from Terabox page."
|
| 50 |
+
|
| 51 |
+
download_url = dlink_match.group(1).encode('utf-8').decode('unicode_escape')
|
| 52 |
+
|
| 53 |
+
raw_filename = "file_from_terabox_" + str(uuid.uuid4()) if not file_name_match else file_name_match.group(1)
|
| 54 |
+
|
| 55 |
+
logger.info(f"Resolved direct URL: {download_url}, filename: {raw_filename}")
|
| 56 |
+
|
| 57 |
+
return download_url, raw_filename, None
|
| 58 |
+
|
| 59 |
except Exception as e:
|
| 60 |
+
logger.exception("Error in get_final_url_and_filename()")
|
| 61 |
+
return None, None, str(e)
|
| 62 |
|
| 63 |
+
# --- Download file ---
|
| 64 |
+
async def download_terabox_file(bot, chat_id: int, status_message_id: int, download_url: str, raw_filename: str) -> tuple[str | None, str | None, str | None]:
|
| 65 |
try:
|
| 66 |
+
local_filepath = os.path.join("downloads", raw_filename)
|
|
|
|
|
|
|
|
|
|
| 67 |
|
| 68 |
+
async with aiohttp.ClientSession() as session:
|
| 69 |
+
async with session.get(download_url) as resp:
|
| 70 |
+
if resp.status != 200:
|
| 71 |
+
return None, None, f"Failed to download file: HTTP {resp.status}"
|
| 72 |
+
|
| 73 |
+
with open(local_filepath, "wb") as f:
|
| 74 |
+
while True:
|
| 75 |
+
chunk = await resp.content.read(1024 * 1024)
|
| 76 |
+
if not chunk:
|
| 77 |
+
break
|
| 78 |
+
f.write(chunk)
|
| 79 |
+
|
| 80 |
+
# You may implement optional fast thumbnail here — for now we'll just skip thumb
|
| 81 |
+
thumb_path = None
|
| 82 |
|
| 83 |
return local_filepath, thumb_path, None
|
| 84 |
+
|
| 85 |
except Exception as e:
|
| 86 |
+
logger.exception("Error in download_terabox_file()")
|
| 87 |
+
return None, None, str(e)
|