Spaces:
No application file
No application file
Create tools.py
Browse files
tools.py
ADDED
@@ -0,0 +1,180 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
+
import re
|
3 |
+
import traceback
|
4 |
+
import uuid
|
5 |
+
from contextlib import suppress
|
6 |
+
from io import BytesIO
|
7 |
+
from urllib.parse import parse_qs, urlparse
|
8 |
+
|
9 |
+
import requests
|
10 |
+
from PIL import Image
|
11 |
+
from telethon import TelegramClient
|
12 |
+
|
13 |
+
from config import BOT_USERNAME, SHORTENER_API
|
14 |
+
from redis_db import db
|
15 |
+
|
16 |
+
|
17 |
+
def check_url_patterns(url: str) -> bool:
|
18 |
+
patterns = [
|
19 |
+
r"ww\.mirrobox\.com",
|
20 |
+
r"www\.nephobox\.com",
|
21 |
+
r"freeterabox\.com",
|
22 |
+
r"www\.freeterabox\.com",
|
23 |
+
r"1024tera\.com",
|
24 |
+
r"4funbox\.co",
|
25 |
+
r"www\.4funbox\.com",
|
26 |
+
r"mirrobox\.com",
|
27 |
+
r"nephobox\.com",
|
28 |
+
r"terabox\.app",
|
29 |
+
r"terabox\.com",
|
30 |
+
r"www\.terabox\.ap",
|
31 |
+
r"www\.terabox\.com",
|
32 |
+
r"www\.1024tera\.co",
|
33 |
+
r"www\.momerybox\.com",
|
34 |
+
r"teraboxapp\.com",
|
35 |
+
r"momerybox\.com",
|
36 |
+
r"tibibox\.com",
|
37 |
+
r"www\.tibibox\.com",
|
38 |
+
r"www\.teraboxapp\.com",
|
39 |
+
]
|
40 |
+
|
41 |
+
return any(re.search(pattern, url) for pattern in patterns)
|
42 |
+
|
43 |
+
|
44 |
+
def extract_code_from_url(url: str) -> str | None:
|
45 |
+
pattern1 = r"/s/(\w+)"
|
46 |
+
pattern2 = r"surl=(\w+)"
|
47 |
+
|
48 |
+
for pattern in (pattern1, pattern2):
|
49 |
+
match = re.search(pattern, url)
|
50 |
+
if match:
|
51 |
+
return match.group(1)
|
52 |
+
|
53 |
+
return None
|
54 |
+
|
55 |
+
|
56 |
+
def get_urls_from_string(string: str) -> str | None:
|
57 |
+
pattern = r"(https?://\S+)"
|
58 |
+
urls = re.findall(pattern, string)
|
59 |
+
urls = [url for url in urls if check_url_patterns(url)]
|
60 |
+
return urls[0] if urls else None
|
61 |
+
|
62 |
+
|
63 |
+
def extract_surl_from_url(url: str) -> str:
|
64 |
+
parsed_url = urlparse(url)
|
65 |
+
query_params = parse_qs(parsed_url.query)
|
66 |
+
surl = query_params.get("surl", [])
|
67 |
+
return surl[0] if surl else False
|
68 |
+
|
69 |
+
|
70 |
+
def get_formatted_size(size_bytes: int) -> str:
|
71 |
+
if size_bytes >= 1024 * 1024:
|
72 |
+
size = size_bytes / (1024 * 1024)
|
73 |
+
unit = "MB"
|
74 |
+
elif size_bytes >= 1024:
|
75 |
+
size = size_bytes / 1024
|
76 |
+
unit = "KB"
|
77 |
+
else:
|
78 |
+
size = size_bytes
|
79 |
+
unit = "b"
|
80 |
+
return f"{size:.2f} {unit}"
|
81 |
+
|
82 |
+
|
83 |
+
def convert_seconds(seconds: int) -> str:
|
84 |
+
seconds = int(seconds)
|
85 |
+
hours = seconds // 3600
|
86 |
+
minutes = (seconds % 3600) // 60
|
87 |
+
remaining = seconds % 60
|
88 |
+
|
89 |
+
if hours > 0:
|
90 |
+
return f"{hours}h:{minutes}m:{remaining}s"
|
91 |
+
elif minutes > 0:
|
92 |
+
return f"{minutes}m:{remaining}s"
|
93 |
+
else:
|
94 |
+
return f"{remaining}s"
|
95 |
+
|
96 |
+
|
97 |
+
async def is_user_on_chat(bot: TelegramClient, chat_id: int, user_id: int) -> bool:
|
98 |
+
try:
|
99 |
+
check = await bot.get_permissions(chat_id, user_id)
|
100 |
+
return check
|
101 |
+
except Exception:
|
102 |
+
return False
|
103 |
+
|
104 |
+
|
105 |
+
async def download_file(url: str, filename: str, callback=None) -> str | bool:
|
106 |
+
try:
|
107 |
+
response = requests.get(url, stream=True)
|
108 |
+
response.raise_for_status()
|
109 |
+
|
110 |
+
with suppress(requests.exceptions.ChunkedEncodingError):
|
111 |
+
with open(filename, "wb") as file:
|
112 |
+
for chunk in response.iter_content(chunk_size=1024):
|
113 |
+
file.write(chunk)
|
114 |
+
if callback:
|
115 |
+
downloaded_size = file.tell()
|
116 |
+
total_size = int(response.headers.get("content-length", 0))
|
117 |
+
await callback(downloaded_size, total_size, "Downloading")
|
118 |
+
return filename
|
119 |
+
except Exception as e:
|
120 |
+
traceback.print_exc()
|
121 |
+
print(f"Error downloading file: {e}")
|
122 |
+
raise Exception(e)
|
123 |
+
|
124 |
+
|
125 |
+
def save_image_from_bytesio(image_bytesio, filename):
|
126 |
+
try:
|
127 |
+
image_bytesio.seek(0)
|
128 |
+
image = Image.open(image_bytesio)
|
129 |
+
image.save(filename)
|
130 |
+
image.close()
|
131 |
+
return filename
|
132 |
+
except Exception as e:
|
133 |
+
print(f"Error saving image: {e}")
|
134 |
+
return False
|
135 |
+
|
136 |
+
|
137 |
+
def download_image_to_bytesio(url: str, filename: str) -> BytesIO | None:
|
138 |
+
try:
|
139 |
+
response = requests.get(url)
|
140 |
+
content = BytesIO()
|
141 |
+
content.name = filename
|
142 |
+
if response.status_code == 200:
|
143 |
+
for chunk in response.iter_content(chunk_size=1024):
|
144 |
+
content.write(chunk)
|
145 |
+
else:
|
146 |
+
return None
|
147 |
+
content.seek(0)
|
148 |
+
return content
|
149 |
+
except Exception:
|
150 |
+
return None
|
151 |
+
|
152 |
+
|
153 |
+
def remove_all_videos():
|
154 |
+
video_extensions = [".mp4", ".mkv", ".webm"]
|
155 |
+
current_directory = os.getcwd()
|
156 |
+
|
157 |
+
try:
|
158 |
+
for file_name in os.listdir(current_directory):
|
159 |
+
if any(file_name.lower().endswith(ext) for ext in video_extensions):
|
160 |
+
os.remove(os.path.join(current_directory, file_name))
|
161 |
+
except Exception as e:
|
162 |
+
print(f"Error: {e}")
|
163 |
+
|
164 |
+
|
165 |
+
def generate_shortenedUrl(sender_id: int) -> str | None:
|
166 |
+
try:
|
167 |
+
uid = str(uuid.uuid4())
|
168 |
+
long_url = f"https://t.me/{BOT_USERNAME}?start=token_{uid}"
|
169 |
+
|
170 |
+
response = requests.get(SHORTENER_API, params={"url": long_url})
|
171 |
+
response.raise_for_status()
|
172 |
+
|
173 |
+
short_url = response.text.strip()
|
174 |
+
if "http" in short_url:
|
175 |
+
db.set(f"token_{uid}", f"{sender_id}|{short_url}", ex=21600)
|
176 |
+
return short_url
|
177 |
+
return None
|
178 |
+
except Exception as e:
|
179 |
+
print(f"Error shortening URL: {e}")
|
180 |
+
return None
|