Spaces:
Sleeping
Sleeping
Update terabox_utils.py
Browse files- terabox_utils.py +10 -18
terabox_utils.py
CHANGED
@@ -8,11 +8,10 @@ import logging
|
|
8 |
import os
|
9 |
import time
|
10 |
import math
|
11 |
-
from typing import Optional, Tuple
|
12 |
|
13 |
import config
|
14 |
|
15 |
-
logger = logging.getLogger(
|
16 |
os.makedirs("downloads", exist_ok=True)
|
17 |
|
18 |
# --- Utility Functions ---
|
@@ -25,7 +24,7 @@ def format_bytes(size_bytes: int) -> str:
|
|
25 |
s = round(size_bytes / p, 2)
|
26 |
return f"{s} {size_name[i]}"
|
27 |
|
28 |
-
async def extract_terabox_short_id(full_url: str)
|
29 |
patterns = [
|
30 |
r'terabox\.com/s/([a-zA-Z0-9_-]+)',
|
31 |
r'teraboxapp\.com/s/([a-zA-Z0-9_-]+)',
|
@@ -41,7 +40,7 @@ async def extract_terabox_short_id(full_url: str) -> Optional[str]:
|
|
41 |
return m.group(1)
|
42 |
return None
|
43 |
|
44 |
-
async def get_final_url_and_filename(original_link: str)
|
45 |
payload = {"link": original_link}
|
46 |
headers = {"User-Agent": "Mozilla/5.0"}
|
47 |
|
@@ -65,9 +64,9 @@ async def get_final_url_and_filename(original_link: str) -> Tuple[Optional[str],
|
|
65 |
except Exception as e:
|
66 |
return None, None, str(e)
|
67 |
|
68 |
-
async def download_terabox_file(
|
69 |
safe_fn = re.sub(r'[\\/*?:"<>|]', "_", filename)[:200]
|
70 |
-
download_path = os.path.join("downloads", f"{
|
71 |
|
72 |
try:
|
73 |
loop = asyncio.get_event_loop()
|
@@ -94,24 +93,17 @@ async def download_terabox_file(bot_instance, chat_id, msg_id, url, filename):
|
|
94 |
f.write(chunk)
|
95 |
dl_size += len(chunk)
|
96 |
|
|
|
97 |
if time.time() - last_update > 2.5:
|
98 |
pct = (dl_size / total_size * 100) if total_size > 0 else 0
|
99 |
-
|
100 |
-
f"
|
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
|
113 |
|
114 |
except Exception as e:
|
115 |
if os.path.exists(download_path):
|
116 |
os.remove(download_path)
|
117 |
-
return None,
|
|
|
8 |
import os
|
9 |
import time
|
10 |
import math
|
|
|
11 |
|
12 |
import config
|
13 |
|
14 |
+
logger = logging.getLogger(__name__)
|
15 |
os.makedirs("downloads", exist_ok=True)
|
16 |
|
17 |
# --- Utility Functions ---
|
|
|
24 |
s = round(size_bytes / p, 2)
|
25 |
return f"{s} {size_name[i]}"
|
26 |
|
27 |
+
async def extract_terabox_short_id(full_url: str):
|
28 |
patterns = [
|
29 |
r'terabox\.com/s/([a-zA-Z0-9_-]+)',
|
30 |
r'teraboxapp\.com/s/([a-zA-Z0-9_-]+)',
|
|
|
40 |
return m.group(1)
|
41 |
return None
|
42 |
|
43 |
+
async def get_final_url_and_filename(original_link: str):
|
44 |
payload = {"link": original_link}
|
45 |
headers = {"User-Agent": "Mozilla/5.0"}
|
46 |
|
|
|
64 |
except Exception as e:
|
65 |
return None, None, str(e)
|
66 |
|
67 |
+
async def download_terabox_file(url: str, filename: str):
|
68 |
safe_fn = re.sub(r'[\\/*?:"<>|]', "_", filename)[:200]
|
69 |
+
download_path = os.path.join("downloads", f"{time.time()}_{safe_fn}")
|
70 |
|
71 |
try:
|
72 |
loop = asyncio.get_event_loop()
|
|
|
93 |
f.write(chunk)
|
94 |
dl_size += len(chunk)
|
95 |
|
96 |
+
# For debug:
|
97 |
if time.time() - last_update > 2.5:
|
98 |
pct = (dl_size / total_size * 100) if total_size > 0 else 0
|
99 |
+
logger.info(
|
100 |
+
f"Downloading: {safe_fn} → {format_bytes(dl_size)}/{format_bytes(total_size)} ({pct:.1f}%)"
|
|
|
101 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
102 |
last_update = time.time()
|
103 |
|
104 |
+
return download_path, None
|
105 |
|
106 |
except Exception as e:
|
107 |
if os.path.exists(download_path):
|
108 |
os.remove(download_path)
|
109 |
+
return None, str(e)
|