2 / app.py
iq7se2's picture
Update app.py
033dd74 verified
import gradio as gr
import requests
from bs4 import BeautifulSoup
import img2pdf
import io
from PIL import Image
import os
import re
def fetch_manga_to_pdf(url):
if not url or "http" not in url:
return None, "⚠️ يرجى إدخال رابط صحيح"
try:
# نظام تزييف الهوية لكسر حماية المواقع
headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/119.0.0.0 Safari/537.36',
'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,*/*;q=0.8',
'Referer': 'https://azoramoon.com/',
'Connection': 'keep-alive'
}
session = requests.Session()
response = session.get(url, headers=headers, timeout=15)
soup = BeautifulSoup(response.text, 'html.parser')
img_list = []
# البحث عن منطقة المحتوى
content_div = soup.find('div', {'class': re.compile(r'content|reader|chapter-video-frame|vung-doc|reading-content')})
target = content_div if content_div else soup
images = target.find_all('img')
for img in images:
img_url = img.get('src') or img.get('data-src') or img.get('data-lazy-src')
if img_url:
img_url = img_url.strip()
if not img_url.startswith('http'):
img_url = "https:" + img_url if img_url.startswith('//') else img_url
if "logo" in img_url.lower() or "avatar" in img_url.lower() or "banner" in img_url.lower():
continue
try:
img_res = session.get(img_url, headers=headers, timeout=10)
if img_res.status_code == 200:
image = Image.open(io.BytesIO(img_res.content)).convert('RGB')
img_list.append(image)
except:
continue
if not img_list:
return None, "❌ الموقع محمي أو الرابط غير مدعوم حالياً"
pdf_io = io.BytesIO()
img_list[0].save(pdf_io, save_all=True, append_images=img_list[1:], format='PDF')
file_path = "manga_chapter.pdf"
with open(file_path, "wb") as f:
f.write(pdf_io.getvalue())
return file_path, "✅ تم التجهيز بنجاح! حمل الملف من الأسفل"
except Exception as e:
return None, f"❌ حدث خطأ: {str(e)}"
# تصميم الواجهة
with gr.Blocks(theme=gr.themes.Soft()) as demo:
gr.Markdown("# 📚 محول المانهوا إلى PDF")
gr.Markdown("ضع رابط الفصل، انتظر قليلاً، ثم حمل الملف.")
with gr.Column():
url_input = gr.Textbox(label="رابط الفصل", placeholder="انسخ رابط الفصل من الموقع هنا...")
btn = gr.Button("🚀 ابدأ التحويل الآن", variant="primary")
gr.HTML("""
<div style='background: #fff3cd; padding: 10px; border-radius: 5px; text-align: center;'>
<p>📢 لضمان سرعة التحميل، يرجى زيارة راعي الموقع:</p>
<a href='https://www.google.com' target='_blank'
style='background: #ffc107; padding: 5px 15px; text-decoration: none; border-radius: 3px; color: black; font-weight: bold;'>
اضغط هنا لتفعيل التحميل السريع ⚡
</a>
</div>
""")
status = gr.Markdown()
output_file = gr.File(label="ملف الـ PDF الجاهز")
btn.click(fn=fetch_manga_to_pdf, inputs=url_input, outputs=[output_file, status])
demo.launch()