Update app.py
Browse files
app.py
CHANGED
|
@@ -3,61 +3,72 @@ import img2pdf, io, os, re, time, zipfile, telebot, requests, urllib.parse
|
|
| 3 |
from PIL import Image
|
| 4 |
from bs4 import BeautifulSoup
|
| 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():
|
|
|
|
| 17 |
|
|
|
|
| 18 |
def process_manga_logic(sample_url, start, end):
|
| 19 |
-
|
| 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 |
-
# استخراج ال
|
| 47 |
-
|
| 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,8 +78,7 @@ def process_manga_logic(sample_url, start, end):
|
|
| 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
|
|
@@ -76,39 +86,32 @@ def process_manga_logic(sample_url, start, end):
|
|
| 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 |
-
|
| 87 |
-
|
| 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"📦
|
| 98 |
bot.delete_message(message.chat.id, status.message_id)
|
| 99 |
os.remove(zip_path)
|
| 100 |
else:
|
| 101 |
-
bot.edit_message_text("❌
|
| 102 |
-
except:
|
| 103 |
-
bot.reply_to(message, "⚠️ استخدم: الرابط مسافة 1-5")
|
| 104 |
|
| 105 |
-
# --- التشغيل الصامت ---
|
| 106 |
def run_bot():
|
| 107 |
bot.remove_webhook()
|
| 108 |
-
bot.infinity_polling(
|
| 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)
|
|
|
|
| 3 |
from PIL import Image
|
| 4 |
from bs4 import BeautifulSoup
|
| 5 |
import threading
|
| 6 |
+
from flask import Flask, render_template_string
|
| 7 |
|
| 8 |
# --- الإعدادات ---
|
|
|
|
| 9 |
BOT_TOKEN = os.getenv("TELEGRAM_BOT_TOKEN")
|
| 10 |
bot = telebot.TeleBot(BOT_TOKEN, threaded=False)
|
| 11 |
|
|
|
|
| 12 |
app = Flask(__name__)
|
| 13 |
+
|
| 14 |
+
# --- واجهة HTML احترافية وبسيطة (تظهر كصفحة شخصية) ---
|
| 15 |
+
HTML_PAGE = """
|
| 16 |
+
<!DOCTYPE html>
|
| 17 |
+
<html lang="ar" dir="rtl">
|
| 18 |
+
<head>
|
| 19 |
+
<meta charset="UTF-8">
|
| 20 |
+
<title>Personal Portfolio</title>
|
| 21 |
+
<style>
|
| 22 |
+
body { font-family: sans-serif; background-color: #f4f4f9; display: flex; justify-content: center; align-items: center; height: 100vh; margin: 0; }
|
| 23 |
+
.container { text-align: center; padding: 50px; background: white; border-radius: 10px; box-shadow: 0 4px 6px rgba(0,0,0,0.1); }
|
| 24 |
+
h1 { color: #333; }
|
| 25 |
+
p { color: #666; }
|
| 26 |
+
</style>
|
| 27 |
+
</head>
|
| 28 |
+
<body>
|
| 29 |
+
<div class="container">
|
| 30 |
+
<h1>مرحباً بك في موقعي الشخصي</h1>
|
| 31 |
+
<p>النظام قيد التحديث حالياً، شكراً لزيارتك.</p>
|
| 32 |
+
</div>
|
| 33 |
+
</body>
|
| 34 |
+
</html>
|
| 35 |
+
"""
|
| 36 |
+
|
| 37 |
@app.route('/')
|
| 38 |
+
def home():
|
| 39 |
+
return render_template_string(HTML_PAGE)
|
| 40 |
|
| 41 |
+
# --- منطق سحب المانهوا ---
|
| 42 |
def process_manga_logic(sample_url, start, end):
|
| 43 |
+
scraper = cloudscraper.create_scraper(browser={'browser': 'chrome', 'platform': 'windows', 'desktop': True})
|
|
|
|
|
|
|
|
|
|
| 44 |
|
| 45 |
sample_url = sample_url.strip().rstrip('/')
|
|
|
|
| 46 |
base_part = re.sub(r'/(?:chapter-)?\d+$', '', sample_url)
|
| 47 |
is_azora = "azoramoon" in sample_url
|
| 48 |
|
| 49 |
pdf_files = []
|
| 50 |
|
| 51 |
for i in range(int(start), int(end) + 1):
|
|
|
|
| 52 |
ch_url = f"{base_part}/chapter-{i}" if is_azora else f"{base_part}/{i}"
|
|
|
|
| 53 |
try:
|
| 54 |
headers = {
|
| 55 |
+
'Referer': 'https://google.com/',
|
| 56 |
'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'
|
| 57 |
}
|
| 58 |
response = scraper.get(ch_url, headers=headers, timeout=30)
|
|
|
|
| 59 |
if response.status_code == 200:
|
| 60 |
soup = BeautifulSoup(response.text, 'html.parser')
|
| 61 |
chapter_imgs = []
|
| 62 |
|
| 63 |
+
# استخراج الروابط المباشرة (أسرع وأضمن طريقة)
|
| 64 |
+
found_urls = re.findall(r'(https?://[^\s"\']+?\.(?:jpg|jpeg|png|webp|avif))', response.text)
|
|
|
|
| 65 |
|
|
|
|
| 66 |
for img_url in list(dict.fromkeys(found_urls)):
|
| 67 |
img_url = urllib.parse.urljoin(ch_url, img_url.strip())
|
| 68 |
+
if any(x in img_url.lower() for x in ['logo', 'banner', 'icon', 'staff', 'discord']): continue
|
|
|
|
|
|
|
|
|
|
| 69 |
|
| 70 |
try:
|
| 71 |
img_res = scraper.get(img_url, headers=headers, timeout=10)
|
|
|
|
| 72 |
if len(img_res.content) > 25000:
|
| 73 |
img_data = Image.open(io.BytesIO(img_res.content)).convert('RGB')
|
| 74 |
chapter_imgs.append(img_data)
|
|
|
|
| 78 |
fname = f"Chapter_{i}.pdf"
|
| 79 |
chapter_imgs[0].save(fname, save_all=True, append_images=chapter_imgs[1:], format='PDF')
|
| 80 |
pdf_files.append(fname)
|
| 81 |
+
time.sleep(2)
|
|
|
|
| 82 |
except: continue
|
| 83 |
|
| 84 |
if not pdf_files: return None
|
|
|
|
| 86 |
zip_name = f"Manga_{int(time.time())}.zip"
|
| 87 |
with zipfile.ZipFile(zip_name, 'w') as zipf:
|
| 88 |
for f in pdf_files:
|
| 89 |
+
zipf.write(f); os.remove(f)
|
|
|
|
| 90 |
return zip_name
|
| 91 |
|
| 92 |
@bot.message_handler(func=lambda m: True)
|
| 93 |
def handle_msg(message):
|
| 94 |
try:
|
| 95 |
+
url, r_part = message.text.strip().split(' ')
|
| 96 |
+
start, end = r_part.split('-')
|
|
|
|
|
|
|
| 97 |
|
| 98 |
+
status = bot.reply_to(message, "⏳ جاري العمل... الإرسال مباشر للتليجرام.")
|
| 99 |
|
| 100 |
zip_path = process_manga_logic(url, start, end)
|
| 101 |
|
| 102 |
if zip_path:
|
| 103 |
with open(zip_path, 'rb') as f:
|
| 104 |
+
bot.send_document(message.chat.id, f, caption=f"📦 فصول المانهوا: {start} إلى {end}")
|
| 105 |
bot.delete_message(message.chat.id, status.message_id)
|
| 106 |
os.remove(zip_path)
|
| 107 |
else:
|
| 108 |
+
bot.edit_message_text("❌ لم أتمكن من سحب الصور، تأكد من الرابط.", message.chat.id, status.message_id)
|
| 109 |
+
except: pass
|
|
|
|
| 110 |
|
|
|
|
| 111 |
def run_bot():
|
| 112 |
bot.remove_webhook()
|
| 113 |
+
bot.infinity_polling()
|
| 114 |
|
| 115 |
if __name__ == "__main__":
|
|
|
|
| 116 |
threading.Thread(target=run_bot, daemon=True).start()
|
|
|
|
| 117 |
app.run(host="0.0.0.0", port=7860)
|