Spaces:
iq7se2
/
8
Runtime error

iq7se2 commited on
Commit
7ab346d
ยท
verified ยท
1 Parent(s): e76b526

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +42 -39
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(): 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,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
- # ุงู„ุชู†ุณูŠู‚: ุงู„ุฑุงุจุท ู…ุณุงูุฉ ุงู„ู…ุฏู‰ [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)
 
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)