iq7se2 commited on
Commit
ff23771
·
verified ·
1 Parent(s): e483f93

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +18 -12
app.py CHANGED
@@ -15,13 +15,19 @@ BASE_URL = os.getenv("BASE_URL")
15
  MANGA_PATH = os.getenv("MANGA_API_PATH")
16
  CHAPTER_PATH = os.getenv("CHAPTER_API_PATH")
17
 
18
- if not all([BASE_URL, MANGA_PATH, CHAPTER_PATH]):
19
- logger.error("❌ CRITICAL: Secrets are NOT set correctly in Hugging Face Settings!")
20
- else:
21
- logger.info(f"✅ Configuration loaded. Base URL: {BASE_URL}")
22
-
23
  PORT = int(os.getenv("PORT", 7860))
24
- HEADERS = {"User-Agent": "Mozilla/5.0"}
 
 
 
 
 
 
 
 
 
 
 
25
 
26
  async def get_chapters(manga_url):
27
  slug = manga_url.strip().rstrip("/").split("/")[-1]
@@ -29,16 +35,18 @@ async def get_chapters(manga_url):
29
  api_url = f"{BASE_URL}{MANGA_PATH.format(slug=slug)}"
30
  logger.info(f"🔍 Fetching Manga: {api_url}")
31
 
 
32
  async with aiohttp.ClientSession(headers=HEADERS) as session:
33
  async with session.get(api_url, timeout=15) as resp:
34
  logger.info(f"📡 API Response Status: {resp.status}")
 
 
 
35
  if resp.status != 200:
36
  return None
37
  data = await resp.json()
38
  mangas = data.get("mangas", [])
39
- if not mangas:
40
- logger.warning(f"⚠️ No manga data found in JSON response for {slug}")
41
- return None
42
  m = mangas[0]
43
  return {
44
  "title": m.get("post_title", slug),
@@ -55,7 +63,6 @@ async def get_chapters(manga_url):
55
  async def get_images(chapter_id):
56
  try:
57
  api_url = f"{BASE_URL}{CHAPTER_PATH.format(id=chapter_id)}"
58
- logger.info(f"📖 Fetching Images: {api_url}")
59
  async with aiohttp.ClientSession(headers=HEADERS) as session:
60
  async with session.get(api_url, timeout=15) as resp:
61
  if resp.status != 200: return []
@@ -63,7 +70,6 @@ async def get_images(chapter_id):
63
  imgs = data.get("imagenes") or []
64
  return [i["src"] if isinstance(i, dict) else i for i in imgs]
65
  except Exception as e:
66
- logger.error(f"💥 Image Fetch Error: {str(e)}")
67
  return []
68
 
69
  @app.route("/")
@@ -77,7 +83,7 @@ def api_manga():
77
  loop = asyncio.new_event_loop()
78
  asyncio.set_event_loop(loop)
79
  data = loop.run_until_complete(get_chapters(url))
80
- if not data: return jsonify({"error": "Manga not found or API error"}), 404
81
  return jsonify(data)
82
 
83
  @app.route("/api/chapter/<int:chapter_id>")
 
15
  MANGA_PATH = os.getenv("MANGA_API_PATH")
16
  CHAPTER_PATH = os.getenv("CHAPTER_API_PATH")
17
 
 
 
 
 
 
18
  PORT = int(os.getenv("PORT", 7860))
19
+
20
+ # الهيدرز المحسنة لتخطي الحماية
21
+ HEADERS = {
22
+ "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",
23
+ "Accept": "application/json, text/plain, */*",
24
+ "Accept-Language": "ar,en-US;q=0.9,en;q=0.8",
25
+ "Referer": "https://utoon.net/",
26
+ "Origin": "https://utoon.net",
27
+ "Sec-Fetch-Dest": "empty",
28
+ "Sec-Fetch-Mode": "cors",
29
+ "Sec-Fetch-Site": "same-origin",
30
+ }
31
 
32
  async def get_chapters(manga_url):
33
  slug = manga_url.strip().rstrip("/").split("/")[-1]
 
35
  api_url = f"{BASE_URL}{MANGA_PATH.format(slug=slug)}"
36
  logger.info(f"🔍 Fetching Manga: {api_url}")
37
 
38
+ # استخدام كوكيز وهمية قد يساعد أحياناً
39
  async with aiohttp.ClientSession(headers=HEADERS) as session:
40
  async with session.get(api_url, timeout=15) as resp:
41
  logger.info(f"📡 API Response Status: {resp.status}")
42
+ if resp.status == 403:
43
+ logger.error("🚫 Access Denied (403). Site is blocking Hugging Face.")
44
+ return None
45
  if resp.status != 200:
46
  return None
47
  data = await resp.json()
48
  mangas = data.get("mangas", [])
49
+ if not mangas: return None
 
 
50
  m = mangas[0]
51
  return {
52
  "title": m.get("post_title", slug),
 
63
  async def get_images(chapter_id):
64
  try:
65
  api_url = f"{BASE_URL}{CHAPTER_PATH.format(id=chapter_id)}"
 
66
  async with aiohttp.ClientSession(headers=HEADERS) as session:
67
  async with session.get(api_url, timeout=15) as resp:
68
  if resp.status != 200: return []
 
70
  imgs = data.get("imagenes") or []
71
  return [i["src"] if isinstance(i, dict) else i for i in imgs]
72
  except Exception as e:
 
73
  return []
74
 
75
  @app.route("/")
 
83
  loop = asyncio.new_event_loop()
84
  asyncio.set_event_loop(loop)
85
  data = loop.run_until_complete(get_chapters(url))
86
+ if not data: return jsonify({"error": "Access Denied by Site or Manga not found"}), 404
87
  return jsonify(data)
88
 
89
  @app.route("/api/chapter/<int:chapter_id>")