Update app.py
Browse files
app.py
CHANGED
|
@@ -5,44 +5,59 @@ from bs4 import BeautifulSoup
|
|
| 5 |
import threading
|
| 6 |
from flask import Flask
|
| 7 |
|
| 8 |
-
# --- الإعدادات
|
|
|
|
| 9 |
BOT_TOKEN = os.getenv("TELEGRAM_BOT_TOKEN")
|
| 10 |
bot = telebot.TeleBot(BOT_TOKEN, threaded=False)
|
| 11 |
|
| 12 |
-
# سيرفر
|
| 13 |
app = Flask(__name__)
|
| 14 |
@app.route('/')
|
| 15 |
-
def home(): return "
|
| 16 |
|
| 17 |
def process_manga_logic(sample_url, start, end):
|
| 18 |
-
|
|
|
|
|
|
|
|
|
|
| 19 |
|
| 20 |
sample_url = sample_url.strip().rstrip('/')
|
|
|
|
| 21 |
base_part = re.sub(r'/(?:chapter-)?\d+$', '', sample_url)
|
| 22 |
is_azora = "azoramoon" in sample_url
|
| 23 |
|
| 24 |
pdf_files = []
|
| 25 |
|
| 26 |
for i in range(int(start), int(end) + 1):
|
|
|
|
| 27 |
ch_url = f"{base_part}/chapter-{i}" if is_azora else f"{base_part}/{i}"
|
|
|
|
| 28 |
try:
|
| 29 |
-
headers = {
|
|
|
|
|
|
|
|
|
|
| 30 |
response = scraper.get(ch_url, headers=headers, timeout=30)
|
| 31 |
|
| 32 |
if response.status_code == 200:
|
| 33 |
soup = BeautifulSoup(response.text, 'html.parser')
|
| 34 |
chapter_imgs = []
|
| 35 |
|
| 36 |
-
# ا
|
| 37 |
-
|
| 38 |
-
found_urls = re.findall(r'(https?://[^\s"\']+?\.(?:jpg|jpeg|png|webp|avif))',
|
| 39 |
|
|
|
|
| 40 |
for img_url in list(dict.fromkeys(found_urls)):
|
| 41 |
img_url = urllib.parse.urljoin(ch_url, img_url.strip())
|
| 42 |
-
|
|
|
|
|
|
|
|
|
|
| 43 |
|
| 44 |
try:
|
| 45 |
img_res = scraper.get(img_url, headers=headers, timeout=10)
|
|
|
|
| 46 |
if len(img_res.content) > 25000:
|
| 47 |
img_data = Image.open(io.BytesIO(img_res.content)).convert('RGB')
|
| 48 |
chapter_imgs.append(img_data)
|
|
@@ -52,41 +67,48 @@ def process_manga_logic(sample_url, start, end):
|
|
| 52 |
fname = f"Chapter_{i}.pdf"
|
| 53 |
chapter_imgs[0].save(fname, save_all=True, append_images=chapter_imgs[1:], format='PDF')
|
| 54 |
pdf_files.append(fname)
|
|
|
|
|
|
|
| 55 |
except: continue
|
| 56 |
-
time.sleep(2)
|
| 57 |
|
| 58 |
if not pdf_files: return None
|
| 59 |
|
| 60 |
zip_name = f"Manga_{int(time.time())}.zip"
|
| 61 |
with zipfile.ZipFile(zip_name, 'w') as zipf:
|
| 62 |
for f in pdf_files:
|
| 63 |
-
zipf.write(f)
|
|
|
|
| 64 |
return zip_name
|
| 65 |
|
| 66 |
@bot.message_handler(func=lambda m: True)
|
| 67 |
def handle_msg(message):
|
| 68 |
try:
|
| 69 |
-
|
| 70 |
-
|
|
|
|
|
|
|
| 71 |
|
| 72 |
-
status = bot.reply_to(message, "⏳ جاري السحب والإرسال المباشر...")
|
| 73 |
|
| 74 |
zip_path = process_manga_logic(url, start, end)
|
| 75 |
|
| 76 |
if zip_path:
|
| 77 |
with open(zip_path, 'rb') as f:
|
| 78 |
-
bot.send_document(message.chat.id, f, caption=f"📦 تم تج
|
| 79 |
bot.delete_message(message.chat.id, status.message_id)
|
| 80 |
os.remove(zip_path)
|
| 81 |
else:
|
| 82 |
-
bot.edit_message_text("❌ فشل السحب.
|
| 83 |
-
except:
|
|
|
|
| 84 |
|
| 85 |
-
# --- تشغيل ال
|
| 86 |
def run_bot():
|
| 87 |
bot.remove_webhook()
|
| 88 |
bot.infinity_polling(timeout=20, long_polling_timeout=10)
|
| 89 |
|
| 90 |
if __name__ == "__main__":
|
|
|
|
| 91 |
threading.Thread(target=run_bot, daemon=True).start()
|
|
|
|
| 92 |
app.run(host="0.0.0.0", port=7860)
|
|
|
|
| 5 |
import threading
|
| 6 |
from flask import Flask
|
| 7 |
|
| 8 |
+
# --- الإعدادات ---
|
| 9 |
+
# تأكد من وضع التوكن في Secrets باسم TELEGRAM_BOT_TOKEN [cite: 1]
|
| 10 |
BOT_TOKEN = os.getenv("TELEGRAM_BOT_TOKEN")
|
| 11 |
bot = telebot.TeleBot(BOT_TOKEN, threaded=False)
|
| 12 |
|
| 13 |
+
# سيرفر صامت لإبقاء المنصة تعمل
|
| 14 |
app = Flask(__name__)
|
| 15 |
@app.route('/')
|
| 16 |
+
def home(): return "System Status: Online"
|
| 17 |
|
| 18 |
def process_manga_logic(sample_url, start, end):
|
| 19 |
+
# محاكاة متصفح حديث جداً لتجاوز الحظر [cite: 1]
|
| 20 |
+
scraper = cloudscraper.create_scraper(
|
| 21 |
+
browser={'browser': 'chrome', 'platform': 'windows', 'desktop': True}
|
| 22 |
+
)
|
| 23 |
|
| 24 |
sample_url = sample_url.strip().rstrip('/')
|
| 25 |
+
# تنظيف الرابط لاستخراج الجزء الثابت [cite: 1]
|
| 26 |
base_part = re.sub(r'/(?:chapter-)?\d+$', '', sample_url)
|
| 27 |
is_azora = "azoramoon" in sample_url
|
| 28 |
|
| 29 |
pdf_files = []
|
| 30 |
|
| 31 |
for i in range(int(start), int(end) + 1):
|
| 32 |
+
# بناء الرابط حسب بنية الموقع
|
| 33 |
ch_url = f"{base_part}/chapter-{i}" if is_azora else f"{base_part}/{i}"
|
| 34 |
+
|
| 35 |
try:
|
| 36 |
+
headers = {
|
| 37 |
+
'Referer': 'https://google.com/', # تمويه المصدر
|
| 38 |
+
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/122.0.0.0 Safari/537.36'
|
| 39 |
+
}
|
| 40 |
response = scraper.get(ch_url, headers=headers, timeout=30)
|
| 41 |
|
| 42 |
if response.status_code == 200:
|
| 43 |
soup = BeautifulSoup(response.text, 'html.parser')
|
| 44 |
chapter_imgs = []
|
| 45 |
|
| 46 |
+
# استخراج الصور بالاعتماد على الروابط المباشرة داخل الـ HTML
|
| 47 |
+
html_content = response.text
|
| 48 |
+
found_urls = re.findall(r'(https?://[^\s"\']+?\.(?:jpg|jpeg|png|webp|avif))', html_content)
|
| 49 |
|
| 50 |
+
# تصفية الروابط المكررة
|
| 51 |
for img_url in list(dict.fromkeys(found_urls)):
|
| 52 |
img_url = urllib.parse.urljoin(ch_url, img_url.strip())
|
| 53 |
+
|
| 54 |
+
# استبعاد الإعلانات والشعارات [cite: 1]
|
| 55 |
+
if any(x in img_url.lower() for x in ['logo', 'banner', 'icon', 'staff', 'discord', 'fb-']):
|
| 56 |
+
continue
|
| 57 |
|
| 58 |
try:
|
| 59 |
img_res = scraper.get(img_url, headers=headers, timeout=10)
|
| 60 |
+
# التأكد أن الملف صورة حقيقية وليس أيقونة صغيرة
|
| 61 |
if len(img_res.content) > 25000:
|
| 62 |
img_data = Image.open(io.BytesIO(img_res.content)).convert('RGB')
|
| 63 |
chapter_imgs.append(img_data)
|
|
|
|
| 67 |
fname = f"Chapter_{i}.pdf"
|
| 68 |
chapter_imgs[0].save(fname, save_all=True, append_images=chapter_imgs[1:], format='PDF')
|
| 69 |
pdf_files.append(fname)
|
| 70 |
+
|
| 71 |
+
time.sleep(3) # تأخير لعدم كشف البوت [cite: 1]
|
| 72 |
except: continue
|
|
|
|
| 73 |
|
| 74 |
if not pdf_files: return None
|
| 75 |
|
| 76 |
zip_name = f"Manga_{int(time.time())}.zip"
|
| 77 |
with zipfile.ZipFile(zip_name, 'w') as zipf:
|
| 78 |
for f in pdf_files:
|
| 79 |
+
zipf.write(f)
|
| 80 |
+
os.remove(f)
|
| 81 |
return zip_name
|
| 82 |
|
| 83 |
@bot.message_handler(func=lambda m: True)
|
| 84 |
def handle_msg(message):
|
| 85 |
try:
|
| 86 |
+
# التنسيق: الرابط مسافة المدى [cite: 1]
|
| 87 |
+
parts = message.text.strip().split(' ')
|
| 88 |
+
url = parts[0]
|
| 89 |
+
start, end = parts[1].split('-')
|
| 90 |
|
| 91 |
+
status = bot.reply_to(message, "⏳ جاري السحب والإرسال المباشر للملف... يرجى الانتظار.")
|
| 92 |
|
| 93 |
zip_path = process_manga_logic(url, start, end)
|
| 94 |
|
| 95 |
if zip_path:
|
| 96 |
with open(zip_path, 'rb') as f:
|
| 97 |
+
bot.send_document(message.chat.id, f, caption=f"📦 تم التجميع من {start} إلى {end}")
|
| 98 |
bot.delete_message(message.chat.id, status.message_id)
|
| 99 |
os.remove(zip_path)
|
| 100 |
else:
|
| 101 |
+
bot.edit_message_text("❌ فشل السحب. الموقع قد يكون حظر السيرفر حالياً.", message.chat.id, status.message_id)
|
| 102 |
+
except:
|
| 103 |
+
bot.reply_to(message, "⚠️ استخدم: الرابط مسافة 1-5")
|
| 104 |
|
| 105 |
+
# --- التشغيل الصامت ---
|
| 106 |
def run_bot():
|
| 107 |
bot.remove_webhook()
|
| 108 |
bot.infinity_polling(timeout=20, long_polling_timeout=10)
|
| 109 |
|
| 110 |
if __name__ == "__main__":
|
| 111 |
+
# تشغيل البوت في خلفية السيرفر
|
| 112 |
threading.Thread(target=run_bot, daemon=True).start()
|
| 113 |
+
# تشغيل Flask على المنفذ الافتراضي لـ Hugging Face
|
| 114 |
app.run(host="0.0.0.0", port=7860)
|