understanding commited on
Commit
3ec2de0
·
verified ·
1 Parent(s): 10051fb

Create terabox_utils.py

Browse files
Files changed (1) hide show
  1. terabox_utils.py +38 -0
terabox_utils.py ADDED
@@ -0,0 +1,38 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # terabox_utils.py
2
+ import aiohttp
3
+ import os
4
+ import uuid
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(original_link: str):
13
+ try:
14
+ async with aiohttp.ClientSession() as session:
15
+ async with session.post(config.TERABOX_WORKER_URL, json={"url": original_link}) as resp:
16
+ if resp.status != 200:
17
+ return None, None, f"Worker returned status {resp.status}"
18
+ data = await resp.json()
19
+ return data.get("url"), data.get("filename"), None
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
+ local_path = f"downloads/{uuid.uuid4()}_{filename}"
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
+ return local_path, None, None
37
+ except Exception as e:
38
+ return None, None, str(e)