-
Notifications
You must be signed in to change notification settings - Fork 54
Expand file tree
/
Copy pathmain.py
More file actions
401 lines (339 loc) Β· 15.1 KB
/
main.py
File metadata and controls
401 lines (339 loc) Β· 15.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
# Don't Remove Credit Tg - https://t.me/roxybasicneedbot1
# Subscribe YouTube Channel For Amazing Bot https://youtube.com/@roxybasicneedbot
# Ask Doubt on telegram https://t.me/roxybasicneedbot1
import os
import re
import sys
import json
import time
import asyncio
import requests
import subprocess
import core as helper
from utils import progress_bar
from vars import API_ID, API_HASH, BOT_TOKEN, FORCE_SUB_CHANNEL, FORCE_SUB_CHANNEL_LINK, ADMINS, OWNER_ID
from aiohttp import ClientSession
from pyromod import listen
from subprocess import getstatusoutput
from pyrogram import Client, filters
from pyrogram.types import Message
from pyrogram.errors import FloodWait, UserNotParticipant, ChatAdminRequired
from pyrogram.errors.exceptions.bad_request_400 import StickerEmojiInvalid
from pyrogram.types.messages_and_media import message
from pyrogram.types import InlineKeyboardButton, InlineKeyboardMarkup, CallbackQuery
from pyrogram.enums import ParseMode, ChatMemberStatus
bot = Client(
"bot",
api_id=API_ID,
api_hash=API_HASH,
bot_token=BOT_TOKEN)
# Welcome image file path
WELCOME_IMAGE_PATH = "welcome.jpg"
# Force Subscribe Check Function
async def is_subscribed(bot, user_id):
if not FORCE_SUB_CHANNEL:
return True
try:
member = await bot.get_chat_member(chat_id=FORCE_SUB_CHANNEL, user_id=user_id)
if member.status in [ChatMemberStatus.OWNER, ChatMemberStatus.ADMINISTRATOR, ChatMemberStatus.MEMBER]:
return True
else:
return False
except UserNotParticipant:
return False
except Exception as e:
print(f"Error checking subscription: {e}")
return False
# Force Subscribe Decorator
def force_subscribe(func):
async def wrapper(bot, message):
if FORCE_SUB_CHANNEL:
is_sub = await is_subscribed(bot, message.from_user.id)
if not is_sub:
keyboard = InlineKeyboardMarkup([
[InlineKeyboardButton("π Join Channel", url="https://t.me/roxybasicneedbot1")],
[InlineKeyboardButton("π Refresh", callback_data="refresh_sub")]
])
await message.reply_text(
f"<b>π Access Denied!</b>\n\n"
f"You must join our channel to use this bot.\n\n"
f"π Click the button below to join:",
reply_markup=keyboard,
parse_mode=ParseMode.HTML
)
return
await func(bot, message)
return wrapper
# Enhanced URL validation function
def is_valid_url(url):
"""Check if URL is valid and accessible"""
url_pattern = re.compile(
r'^https?://' # http:// or https://
r'(?:(?:[A-Z0-9](?:[A-Z0-9-]{0,61}[A-Z0-9])?\.)+[A-Z]{2,6}\.?|' # domain...
r'localhost|' # localhost...
r'\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})' # ...or ip
r'(?::\d+)?' # optional port
r'(?:/?|[/?]\S+)$', re.IGNORECASE)
return url_pattern.match(url) is not None
def extract_url_from_line(line):
"""Extract and validate URL from a line of text"""
line = line.strip()
if not line:
return None, None
# Try to find URL in the line
url_match = re.search(r'https?://[^\s]+', line)
if url_match:
url = url_match.group()
# Extract title (everything before the URL)
title = line.replace(url, '').strip()
if not title:
title = f"File_{hash(url) % 1000}"
return title, url
# If line doesn't contain http/https, check if it's a valid domain
if '.' in line and not line.startswith('/'):
# Assume it's a URL without protocol
url = 'https://' + line
if is_valid_url(url):
return f"File_{hash(line) % 1000}", url
return None, None
@bot.on_message(filters.command(["start"]))
@force_subscribe
async def start(bot: Client, m: Message):
welcome_text = f"<b>π Hello {m.from_user.mention}!</b>\n\n<blockquote>π I am a bot for downloading files from your <b>.TXT</b> file and uploading them to Telegram.\n\nπ To get started, send /upload command and follow the steps.</blockquote>"
# Create inline keyboard
keyboard = InlineKeyboardMarkup([
[
InlineKeyboardButton("β‘ Upload Files", callback_data="upload_files")
],
[
InlineKeyboardButton("π Channel", url="https://t.me/roxybasicneedbot1"),
InlineKeyboardButton("π¨βπ» Developer", url="https://t.me/roxycontactbot")
]
])
# Check if the welcome image file exists
if os.path.exists(WELCOME_IMAGE_PATH):
await m.reply_photo(
photo=WELCOME_IMAGE_PATH,
caption=welcome_text,
reply_markup=keyboard,
parse_mode=ParseMode.HTML
)
else:
await m.reply_text(
welcome_text,
reply_markup=keyboard,
parse_mode=ParseMode.HTML
)
@bot.on_callback_query()
async def callback_handler(bot: Client, query: CallbackQuery):
data = query.data
if data == "refresh_sub":
if FORCE_SUB_CHANNEL:
is_sub = await is_subscribed(bot, query.from_user.id)
if is_sub:
await query.message.delete()
await bot.send_message(
query.from_user.id,
"β
**Subscription Verified!**\n\nYou can now use the bot. Send /start to begin."
)
else:
await query.answer("β You haven't joined the channel yet!", show_alert=True)
else:
await query.answer("β
No subscription required!")
elif data == "upload_files":
await query.answer("Send /upload command to start!", show_alert=True)
@bot.on_message(filters.command("stop"))
async def restart_handler(_, m):
await m.reply_text("**π Stopped**", True)
os.execl(sys.executable, sys.executable, *sys.argv)
@bot.on_message(filters.command(["upload"]))
@force_subscribe
async def upload(bot: Client, m: Message):
editable = await m.reply_text('π€ Send your TXT file with links β‘οΈ')
input: Message = await bot.listen(editable.chat.id)
x = await input.download()
await input.delete(True)
path = f"./downloads/{m.chat.id}"
os.makedirs(path, exist_ok=True)
try:
with open(x, "r", encoding='utf-8', errors='ignore') as f:
content = f.read()
lines = content.split("\n")
links = []
for line in lines:
title, url = extract_url_from_line(line)
if title and url and is_valid_url(url):
links.append([title, url])
os.remove(x)
if not links:
await editable.edit("β **No valid links found in the file!**\n\nPlease make sure your file contains valid URLs.")
return
except Exception as e:
await editable.edit(f"β **Error reading file:** {str(e)}")
if os.path.exists(x):
os.remove(x)
return
await editable.edit(f"π **Total Links Found:** {len(links)}\n\nπ **Send starting number** (default: 1)")
input0: Message = await bot.listen(editable.chat.id)
raw_text = input0.text
await input0.delete(True)
await editable.edit("π **Enter your batch name:**")
input1: Message = await bot.listen(editable.chat.id)
raw_text0 = input1.text
await input1.delete(True)
await editable.edit("π¬ **Select video quality:**\n\n360, 480, 720, 1080, 4k")
input2: Message = await bot.listen(editable.chat.id)
raw_text2 = input2.text
await input2.delete(True)
quality_map = {
"144": "256x144", "240": "426x240", "360": "640x360",
"480": "854x480", "720": "1280x720", "1080": "1920x1080"
}
res = quality_map.get(raw_text2, "UN")
await editable.edit("π¬ **Enter caption for files:**")
input3: Message = await bot.listen(editable.chat.id)
raw_text3 = input3.text
await input3.delete(True)
MR = raw_text3
await editable.edit("πΌ **Send thumbnail URL** (or send 'no' to skip):")
input6 = await bot.listen(editable.chat.id)
thumb_input = input6.text
await input6.delete(True)
await editable.delete()
thumb = "no"
if thumb_input.startswith(("http://", "https://")):
try:
getstatusoutput(f"wget '{thumb_input}' -O 'thumb.jpg'")
if os.path.exists('thumb.jpg'):
thumb = "thumb.jpg"
except:
thumb = "no"
try:
count = max(1, int(raw_text)) if raw_text.isdigit() else 1
except:
count = 1
successful_downloads = 0
failed_downloads = 0
try:
for i in range(count - 1, len(links)):
if i >= len(links):
break
try:
title, url = links[i]
# Process URL for different platforms
if "drive.google.com" in url:
url = url.replace("file/d/","uc?export=download&id=").replace("/view?usp=sharing","")
elif "youtube.com/watch" in url or "youtu.be/" in url:
pass # Keep as is for yt-dlp
elif "visionias" in url:
try:
async with ClientSession() as session:
headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36'
}
async with session.get(url, headers=headers) as resp:
text = await resp.text()
m3u8_match = re.search(r"(https://.*?playlist\.m3u8.*?)\"", text)
if m3u8_match:
url = m3u8_match.group(1)
except:
pass
name1 = re.sub(r'[<>:"/\\|?*]', '', title)[:50]
name = f'{str(count).zfill(3)}) {name1}'
# Determine download strategy
if "youtu" in url:
ytf = 'bv*[height<={raw_text2}][ext=mp4]+ba[ext=m4a]/b[height<={raw_text2}][ext=mp4]'
cmd = (
f'yt-dlp '
f'--force-ipv4 '
f'--retries infinite '
f'--http-chunk-size 10M '
f'--downloader ffmpeg '
f'--concurrent-fragments 20 '
f'--cookies-from-browser chrome '
f'--user-agent "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36" '
f'-f "{ytf}" "{url}" '
f'-o "{name}.%(ext)s"'
)
elif url.endswith('.pdf'):
cmd = f'yt-dlp -o "{name}.pdf" "{url}"'
else:
cmd = (
f'yt-dlp '
f'--downloader ffmpeg '
f'--concurrent-fragments 32 '
f'--user-agent "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36" '
f'--add-header "Accept-Language: en-US,en;q=0.9" '
f'--add-header "Connection: keep-alive" '
f'-f "bestvideo[height<={raw_text2}]+bestaudio/best[height<={raw_text2}]" "{url}" '
f'-o "{name}.%(ext)s"'
)
cc = f'**πΉ Video #{str(count).zfill(3)}**\n**π Title:** {name1}\n**π¦ Batch:** {raw_text0}\n{MR}'
cc1 = f'**π Document #{str(count).zfill(3)}**\n**π Title:** {name1}\n**π¦ Batch:** {raw_text0}\n{MR}'
# Show download progress
prog = await m.reply_text(
f"β¬οΈ **Downloading...**\n\n"
f"π **Name:** `{name1}`\n"
f"π **URL:** `{url[:50]}...`\n"
f"π **Progress:** {count}/{len(links)}"
)
try:
if "drive.google.com" in url:
filename = await helper.download(url, name)
if filename and os.path.exists(filename):
await bot.send_document(chat_id=m.chat.id, document=filename, caption=cc1)
os.remove(filename)
successful_downloads += 1
elif ".pdf" in url:
result = subprocess.run(cmd, shell=True, capture_output=True, text=True)
expected_file = f"{name}.pdf"
if os.path.exists(expected_file):
await bot.send_document(chat_id=m.chat.id, document=expected_file, caption=cc1)
os.remove(expected_file)
successful_downloads += 1
else:
# Video download
result = subprocess.run(cmd, shell=True, capture_output=True, text=True)
# Find downloaded file
possible_extensions = ['.mp4', '.mkv', '.avi', '.webm', '.mov']
filename = None
for ext in possible_extensions:
test_file = f"{name}{ext}"
if os.path.exists(test_file):
filename = test_file
break
if filename and os.path.exists(filename):
await helper.send_vid(bot, m, cc, filename, thumb, name, prog)
successful_downloads += 1
else:
failed_downloads += 1
await prog.edit(f"β **Failed:** {name1}")
await asyncio.sleep(2)
await prog.delete()
count += 1
time.sleep(1)
except FloodWait as e:
await m.reply_text(f"β οΈ **Rate limited. Waiting {e.x} seconds...**")
time.sleep(e.x)
continue
except Exception as download_error:
failed_downloads += 1
await prog.edit(f"β **Error:** {str(download_error)[:100]}")
await asyncio.sleep(3)
continue
except Exception as e:
failed_downloads += 1
await m.reply_text(f"β **Processing error:** {str(e)[:200]}")
continue
except Exception as e:
await m.reply_text(f"β **Fatal error:** {str(e)}")
# Final summary
summary_text = (
f"π **Download Complete!**\n\n"
f"β
**Successful:** {successful_downloads}\n"
f"β **Failed:** {failed_downloads}\n"
f"π **Total:** {successful_downloads + failed_downloads}"
)
await m.reply_text(summary_text)
if __name__ == "__main__":
bot.run()