Spaces:
Sleeping
Sleeping
Update terabox_utils.py
Browse files- terabox_utils.py +9 -41
terabox_utils.py
CHANGED
@@ -8,12 +8,11 @@ import logging
|
|
8 |
import os
|
9 |
import time
|
10 |
import math
|
11 |
-
import subprocess
|
12 |
from typing import Optional, Tuple
|
13 |
|
14 |
import config
|
15 |
|
16 |
-
logger = logging.getLogger(
|
17 |
os.makedirs("downloads", exist_ok=True)
|
18 |
|
19 |
# --- Utility Functions ---
|
@@ -42,9 +41,7 @@ async def extract_terabox_short_id(full_url: str) -> Optional[str]:
|
|
42 |
return m.group(1)
|
43 |
return None
|
44 |
|
45 |
-
# --- Main Link Extraction ---
|
46 |
async def get_final_url_and_filename(original_link: str) -> Tuple[Optional[str], Optional[str], Optional[str]]:
|
47 |
-
# First, try WORKER API
|
48 |
payload = {"link": original_link}
|
49 |
headers = {"User-Agent": "Mozilla/5.0"}
|
50 |
|
@@ -66,13 +63,11 @@ async def get_final_url_and_filename(original_link: str) -> Tuple[Optional[str],
|
|
66 |
return dl, fn, None
|
67 |
|
68 |
except Exception as e:
|
69 |
-
|
70 |
-
return None, None, f"Worker API failed: {str(e)}"
|
71 |
|
72 |
-
# --- Downloader ---
|
73 |
async def download_terabox_file(bot_instance, chat_id, msg_id, url, filename):
|
74 |
safe_fn = re.sub(r'[\\/*?:"<>|]', "_", filename)[:200]
|
75 |
-
|
76 |
|
77 |
try:
|
78 |
loop = asyncio.get_event_loop()
|
@@ -93,7 +88,7 @@ async def download_terabox_file(bot_instance, chat_id, msg_id, url, filename):
|
|
93 |
dl_size = 0
|
94 |
last_update = time.time()
|
95 |
|
96 |
-
with open(
|
97 |
for chunk in r.iter_content(chunk_size=1024 * 1024):
|
98 |
if chunk:
|
99 |
f.write(chunk)
|
@@ -102,48 +97,21 @@ async def download_terabox_file(bot_instance, chat_id, msg_id, url, filename):
|
|
102 |
if time.time() - last_update > 2.5:
|
103 |
pct = (dl_size / total_size * 100) if total_size > 0 else 0
|
104 |
prog_text = (
|
105 |
-
f"📥
|
106 |
f"Progress: {format_bytes(dl_size)}/{format_bytes(total_size)} ({pct:.1f}%)"
|
107 |
)
|
108 |
try:
|
109 |
await bot_instance.edit_message_text(
|
110 |
-
chat_id, msg_id, prog_text, parse_mode="
|
111 |
)
|
112 |
except Exception:
|
113 |
pass
|
114 |
|
115 |
last_update = time.time()
|
116 |
|
117 |
-
|
118 |
-
if config.ENABLE_FFMPEG_CONVERT:
|
119 |
-
# Optional ffmpeg convert (if ENABLE_FFMPEG_CONVERT = True)
|
120 |
-
new_filename = os.path.splitext(safe_fn)[0] + "_converted.mp4"
|
121 |
-
converted_path = os.path.join("downloads", f"{chat_id}_{time.time()}_{new_filename}")
|
122 |
-
thumb_path = os.path.join("downloads", f"{chat_id}_{time.time()}_thumb.jpg")
|
123 |
-
|
124 |
-
# Convert to mp4
|
125 |
-
ffmpeg_convert_cmd = [
|
126 |
-
"ffmpeg", "-y", "-i", raw_download_path,
|
127 |
-
"-c:v", "libx264", "-preset", "fast", "-c:a", "aac",
|
128 |
-
"-movflags", "+faststart", converted_path
|
129 |
-
]
|
130 |
-
subprocess.run(ffmpeg_convert_cmd, check=True)
|
131 |
-
|
132 |
-
# Generate thumbnail
|
133 |
-
ffmpeg_thumb_cmd = [
|
134 |
-
"ffmpeg", "-y", "-i", converted_path,
|
135 |
-
"-ss", "00:00:01.000", "-vframes", "1", thumb_path
|
136 |
-
]
|
137 |
-
subprocess.run(ffmpeg_thumb_cmd, check=True)
|
138 |
-
|
139 |
-
# Cleanup raw file
|
140 |
-
os.remove(raw_download_path)
|
141 |
-
return converted_path, thumb_path, None
|
142 |
-
else:
|
143 |
-
# No conversion, return raw path
|
144 |
-
return raw_download_path, None, None
|
145 |
|
146 |
except Exception as e:
|
147 |
-
if os.path.exists(
|
148 |
-
os.remove(
|
149 |
return None, None, str(e)
|
|
|
8 |
import os
|
9 |
import time
|
10 |
import math
|
|
|
11 |
from typing import Optional, Tuple
|
12 |
|
13 |
import config
|
14 |
|
15 |
+
logger = logging.getLogger("terabox_utils")
|
16 |
os.makedirs("downloads", exist_ok=True)
|
17 |
|
18 |
# --- Utility Functions ---
|
|
|
41 |
return m.group(1)
|
42 |
return None
|
43 |
|
|
|
44 |
async def get_final_url_and_filename(original_link: str) -> Tuple[Optional[str], Optional[str], Optional[str]]:
|
|
|
45 |
payload = {"link": original_link}
|
46 |
headers = {"User-Agent": "Mozilla/5.0"}
|
47 |
|
|
|
63 |
return dl, fn, None
|
64 |
|
65 |
except Exception as e:
|
66 |
+
return None, None, str(e)
|
|
|
67 |
|
|
|
68 |
async def download_terabox_file(bot_instance, chat_id, msg_id, url, filename):
|
69 |
safe_fn = re.sub(r'[\\/*?:"<>|]', "_", filename)[:200]
|
70 |
+
download_path = os.path.join("downloads", f"{chat_id}_{time.time()}_{safe_fn}")
|
71 |
|
72 |
try:
|
73 |
loop = asyncio.get_event_loop()
|
|
|
88 |
dl_size = 0
|
89 |
last_update = time.time()
|
90 |
|
91 |
+
with open(download_path, 'wb') as f:
|
92 |
for chunk in r.iter_content(chunk_size=1024 * 1024):
|
93 |
if chunk:
|
94 |
f.write(chunk)
|
|
|
97 |
if time.time() - last_update > 2.5:
|
98 |
pct = (dl_size / total_size * 100) if total_size > 0 else 0
|
99 |
prog_text = (
|
100 |
+
f"📥 <b>Downloading:</b> <code>{filename}</code>\n"
|
101 |
f"Progress: {format_bytes(dl_size)}/{format_bytes(total_size)} ({pct:.1f}%)"
|
102 |
)
|
103 |
try:
|
104 |
await bot_instance.edit_message_text(
|
105 |
+
chat_id, msg_id, prog_text, parse_mode="HTML"
|
106 |
)
|
107 |
except Exception:
|
108 |
pass
|
109 |
|
110 |
last_update = time.time()
|
111 |
|
112 |
+
return download_path, None, None
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
113 |
|
114 |
except Exception as e:
|
115 |
+
if os.path.exists(download_path):
|
116 |
+
os.remove(download_path)
|
117 |
return None, None, str(e)
|