Spaces:
Sleeping
Sleeping
Update terabox_utils.py
Browse files- terabox_utils.py +16 -9
terabox_utils.py
CHANGED
|
@@ -1,38 +1,45 @@
|
|
| 1 |
# terabox_utils.py
|
|
|
|
| 2 |
import aiohttp
|
|
|
|
| 3 |
import os
|
| 4 |
-
import
|
| 5 |
import config
|
| 6 |
|
| 7 |
async def extract_terabox_short_id(link: str) -> str:
|
|
|
|
| 8 |
import re
|
| 9 |
match = re.search(r'/s/([a-zA-Z0-9]+)', link)
|
| 10 |
return match.group(1) if match else None
|
| 11 |
|
| 12 |
-
async def get_final_url_and_filename(
|
|
|
|
| 13 |
try:
|
| 14 |
async with aiohttp.ClientSession() as session:
|
| 15 |
-
async with session.post(config.TERABOX_WORKER_URL, json={"
|
| 16 |
if resp.status != 200:
|
| 17 |
-
return None, None, f"Worker
|
| 18 |
data = await resp.json()
|
| 19 |
-
return data
|
| 20 |
except Exception as e:
|
|
|
|
| 21 |
return None, None, str(e)
|
| 22 |
|
| 23 |
async def download_terabox_file(bot, chat_id, message_id, download_url, filename):
|
| 24 |
-
|
|
|
|
| 25 |
try:
|
| 26 |
async with aiohttp.ClientSession() as session:
|
| 27 |
async with session.get(download_url) as resp:
|
| 28 |
-
if resp.status != 200:
|
| 29 |
-
return None, None, f"Download failed with status {resp.status}"
|
| 30 |
with open(local_path, "wb") as f:
|
| 31 |
while True:
|
| 32 |
chunk = await resp.content.read(1024 * 1024)
|
| 33 |
if not chunk:
|
| 34 |
break
|
| 35 |
f.write(chunk)
|
| 36 |
-
|
|
|
|
|
|
|
| 37 |
except Exception as e:
|
|
|
|
| 38 |
return None, None, str(e)
|
|
|
|
| 1 |
# terabox_utils.py
|
| 2 |
+
|
| 3 |
import aiohttp
|
| 4 |
+
import asyncio
|
| 5 |
import os
|
| 6 |
+
import logging
|
| 7 |
import config
|
| 8 |
|
| 9 |
async def extract_terabox_short_id(link: str) -> str:
|
| 10 |
+
# Simple example: extract ID from Terabox link (you can customize this!)
|
| 11 |
import re
|
| 12 |
match = re.search(r'/s/([a-zA-Z0-9]+)', link)
|
| 13 |
return match.group(1) if match else None
|
| 14 |
|
| 15 |
+
async def get_final_url_and_filename(link: str):
|
| 16 |
+
# Example worker call
|
| 17 |
try:
|
| 18 |
async with aiohttp.ClientSession() as session:
|
| 19 |
+
async with session.post(config.TERABOX_WORKER_URL, json={"link": link}) as resp:
|
| 20 |
if resp.status != 200:
|
| 21 |
+
return None, None, f"Worker error: {resp.status}"
|
| 22 |
data = await resp.json()
|
| 23 |
+
return data["url"], data["name"], None
|
| 24 |
except Exception as e:
|
| 25 |
+
logging.exception("Error in get_final_url_and_filename")
|
| 26 |
return None, None, str(e)
|
| 27 |
|
| 28 |
async def download_terabox_file(bot, chat_id, message_id, download_url, filename):
|
| 29 |
+
# Fast simple downloader
|
| 30 |
+
local_path = f"downloads/{filename}"
|
| 31 |
try:
|
| 32 |
async with aiohttp.ClientSession() as session:
|
| 33 |
async with session.get(download_url) as resp:
|
|
|
|
|
|
|
| 34 |
with open(local_path, "wb") as f:
|
| 35 |
while True:
|
| 36 |
chunk = await resp.content.read(1024 * 1024)
|
| 37 |
if not chunk:
|
| 38 |
break
|
| 39 |
f.write(chunk)
|
| 40 |
+
# Simulate thumbnail creation (skip FFMPEG encoding!)
|
| 41 |
+
thumb_path = None
|
| 42 |
+
return local_path, thumb_path, None
|
| 43 |
except Exception as e:
|
| 44 |
+
logging.exception("Error in download_terabox_file")
|
| 45 |
return None, None, str(e)
|