iq7se2 commited on
Commit
d051734
·
verified ·
1 Parent(s): dea829b

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +38 -54
app.py CHANGED
@@ -1,66 +1,52 @@
1
- import gradio as gr
2
  import cloudscraper
3
  import img2pdf, io, os, re, time, zipfile, telebot, requests, urllib.parse
4
  from PIL import Image
5
  from bs4 import BeautifulSoup
6
  import threading
 
7
 
8
- # --- الإعدادات ---
9
  BOT_TOKEN = os.getenv("TELEGRAM_BOT_TOKEN")
10
- GOFILE_TOKEN = "DmBu4zlQrjRfAOAuzAVk3KaTLQiVeBsi"
11
 
12
- bot = telebot.TeleBot(BOT_TOKEN, threaded=False) # تعطيل تعدد الخيوط في الاستقبال لمنع التصادم
13
-
14
- def upload_to_gofile(file_path):
15
- try:
16
- server_res = requests.get("https://api.gofile.io/getServer", timeout=10).json()
17
- server = server_res['data']['server']
18
- with open(file_path, 'rb') as f:
19
- res = requests.post(f"https://{server}.gofile.io/uploadFile", files={'file': f}, data={'token': GOFILE_TOKEN}, timeout=150).json()
20
- return res['data']['downloadPage'] if res['status'] == 'ok' else None
21
- except: return None
22
 
23
  def process_manga_logic(sample_url, start, end):
24
  scraper = cloudscraper.create_scraper(browser={'browser': 'chrome', 'platform': 'windows', 'desktop': True})
25
 
26
  sample_url = sample_url.strip().rstrip('/')
27
- # استخراج الرابط الأساسي بشكل أدق
28
  base_part = re.sub(r'/(?:chapter-)?\d+$', '', sample_url)
29
  is_azora = "azoramoon" in sample_url
30
 
31
  pdf_files = []
32
 
33
  for i in range(int(start), int(end) + 1):
34
- # بناء الرابط حسب الموقع (أزورا تستخدم /chapter-X وأوليمبوس تستخدم /X)
35
  ch_url = f"{base_part}/chapter-{i}" if is_azora else f"{base_part}/{i}"
36
-
37
  try:
38
- headers = {
39
- 'Referer': sample_url,
40
- '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'
41
- }
42
  response = scraper.get(ch_url, headers=headers, timeout=30)
43
 
44
  if response.status_code == 200:
45
  soup = BeautifulSoup(response.text, 'html.parser')
46
  chapter_imgs = []
47
 
48
- # البحث في كل الوسوم الممكنة للصور
49
- for img in soup.find_all(['img', 'source']):
50
- img_url = img.get('data-src') or img.get('src') or img.get('data-lazy-src') or img.get('srcset')
51
- if img_url:
52
- img_url = img_url.split(' ')[0] # للتعامل مع srcset
53
- img_url = urllib.parse.urljoin(ch_url, img_url.strip())
54
-
55
- if any(ext in img_url.lower() for ext in ['.jpg', '.jpeg', '.png', '.webp']):
56
- if any(x in img_url.lower() for x in ['logo', 'banner', 'icon', 'staff']): continue
57
-
58
- try:
59
- img_res = scraper.get(img_url, headers=headers, timeout=10)
60
- if len(img_res.content) > 15000:
61
- img_data = Image.open(io.BytesIO(img_res.content)).convert('RGB')
62
- chapter_imgs.append(img_data)
63
- except: continue
64
 
65
  if chapter_imgs:
66
  fname = f"Chapter_{i}.pdf"
@@ -69,40 +55,38 @@ def process_manga_logic(sample_url, start, end):
69
  except: continue
70
  time.sleep(2)
71
 
72
- if not pdf_files: return None, "لم يتم العثور على صور في الرابط"
73
 
74
  zip_name = f"Manga_{int(time.time())}.zip"
75
  with zipfile.ZipFile(zip_name, 'w') as zipf:
76
  for f in pdf_files:
77
  zipf.write(f); os.remove(f)
78
- return zip_name, "نجاح"
79
 
80
  @bot.message_handler(func=lambda m: True)
81
  def handle_msg(message):
82
  try:
83
- parts = message.text.strip().split(' ')
84
- url, r_part = parts[0], parts[1]
85
  start, end = r_part.split('-')
86
 
87
- status = bot.reply_to(message, "⏳ جاري المعالجة... (قد يستغرق دقائق لتجاوز الحماية)")
88
 
89
- zip_path, log = process_manga_logic(url, start, end)
90
 
91
  if zip_path:
92
- link = upload_to_gofile(zip_path)
93
- if link: bot.edit_message_text(f" تم الرفع:\n{link}", message.chat.id, status.message_id)
94
- else:
95
- with open(zip_path, 'rb') as f: bot.send_document(message.chat.id, f)
96
- bot.delete_message(message.chat.id, status.message_id)
97
  os.remove(zip_path)
98
  else:
99
- bot.edit_message_text(f"❌ فشل: {log}", message.chat.id, status.message_id)
100
  except: pass
101
 
102
- # --- التشغيل الآمن ---
103
- bot.remove_webhook()
104
- threading.Thread(target=lambda: bot.infinity_polling(timeout=10, long_polling_timeout=5), daemon=True).start()
 
105
 
106
- with gr.Blocks() as demo:
107
- gr.Markdown("# 🚀 Engine Active")
108
- demo.launch()
 
 
1
  import cloudscraper
2
  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
  BOT_TOKEN = os.getenv("TELEGRAM_BOT_TOKEN")
10
+ bot = telebot.TeleBot(BOT_TOKEN, threaded=False)
11
 
12
+ # سيرفر وهمي لإبقاء Hugging Face يعمل دون واجهة رسومية
13
+ app = Flask(__name__)
14
+ @app.route('/')
15
+ def home(): return "Engine is running..."
 
 
 
 
 
 
16
 
17
  def process_manga_logic(sample_url, start, end):
18
  scraper = cloudscraper.create_scraper(browser={'browser': 'chrome', 'platform': 'windows', 'desktop': True})
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 = {'Referer': sample_url, 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36'}
 
 
 
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
+ # البحث عن الصور في كل مكان (img, source, regex)
37
+ html_text = response.text
38
+ found_urls = re.findall(r'(https?://[^\s"\']+?\.(?:jpg|jpeg|png|webp|avif))', html_text)
39
+
40
+ for img_url in list(dict.fromkeys(found_urls)):
41
+ img_url = urllib.parse.urljoin(ch_url, img_url.strip())
42
+ if any(x in img_url.lower() for x in ['logo', 'banner', 'icon', 'staff', 'discord']): continue
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)
49
+ except: continue
 
 
50
 
51
  if chapter_imgs:
52
  fname = f"Chapter_{i}.pdf"
 
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); os.remove(f)
64
+ return zip_name
65
 
66
  @bot.message_handler(func=lambda m: True)
67
  def handle_msg(message):
68
  try:
69
+ url, r_part = message.text.strip().split(' ')
 
70
  start, end = r_part.split('-')
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"📦 تم تجهيز الفصول من {start} إلى {end}")
79
+ bot.delete_message(message.chat.id, status.message_id)
 
 
80
  os.remove(zip_path)
81
  else:
82
+ bot.edit_message_text("❌ فشل السحب. تأكد من الرابط.", message.chat.id, status.message_id)
83
  except: pass
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)