Update app.py
Browse files
app.py
CHANGED
|
@@ -1,38 +1,43 @@
|
|
| 1 |
import os
|
| 2 |
import aiohttp
|
| 3 |
import asyncio
|
|
|
|
| 4 |
from flask import Flask, render_template, request, jsonify
|
| 5 |
|
| 6 |
app = Flask(__name__)
|
| 7 |
|
| 8 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
| 9 |
BASE_URL = os.getenv("BASE_URL")
|
| 10 |
MANGA_PATH = os.getenv("MANGA_API_PATH")
|
| 11 |
CHAPTER_PATH = os.getenv("CHAPTER_API_PATH")
|
| 12 |
|
| 13 |
if not all([BASE_URL, MANGA_PATH, CHAPTER_PATH]):
|
| 14 |
-
|
|
|
|
|
|
|
| 15 |
|
| 16 |
PORT = int(os.getenv("PORT", 7860))
|
|
|
|
| 17 |
|
| 18 |
-
HEADERS = {
|
| 19 |
-
"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"
|
| 20 |
-
}
|
| 21 |
-
|
| 22 |
-
# ── 1. SCRAPER LOGIC ──────────────────────────────────────────────────
|
| 23 |
async def get_chapters(manga_url):
|
| 24 |
slug = manga_url.strip().rstrip("/").split("/")[-1]
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
async with session.get(api_url, timeout=15) as resp:
|
|
|
|
| 31 |
if resp.status != 200:
|
| 32 |
return None
|
| 33 |
data = await resp.json()
|
| 34 |
mangas = data.get("mangas", [])
|
| 35 |
if not mangas:
|
|
|
|
| 36 |
return None
|
| 37 |
m = mangas[0]
|
| 38 |
return {
|
|
@@ -43,25 +48,24 @@ async def get_chapters(manga_url):
|
|
| 43 |
for c in m.get("capitulos", [])
|
| 44 |
]
|
| 45 |
}
|
| 46 |
-
|
| 47 |
-
|
|
|
|
| 48 |
|
| 49 |
async def get_images(chapter_id):
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
try:
|
| 55 |
async with session.get(api_url, timeout=15) as resp:
|
| 56 |
-
if resp.status != 200:
|
| 57 |
-
return []
|
| 58 |
data = await resp.json()
|
| 59 |
imgs = data.get("imagenes") or []
|
| 60 |
return [i["src"] if isinstance(i, dict) else i for i in imgs]
|
| 61 |
-
|
| 62 |
-
|
|
|
|
| 63 |
|
| 64 |
-
# ── 2. ROUTES ─────────────────────────────────────────────────────────
|
| 65 |
@app.route("/")
|
| 66 |
def index():
|
| 67 |
return render_template("index.html")
|
|
@@ -73,7 +77,7 @@ def api_manga():
|
|
| 73 |
loop = asyncio.new_event_loop()
|
| 74 |
asyncio.set_event_loop(loop)
|
| 75 |
data = loop.run_until_complete(get_chapters(url))
|
| 76 |
-
if not data: return jsonify({"error": "Manga not found"}), 404
|
| 77 |
return jsonify(data)
|
| 78 |
|
| 79 |
@app.route("/api/chapter/<int:chapter_id>")
|
|
|
|
| 1 |
import os
|
| 2 |
import aiohttp
|
| 3 |
import asyncio
|
| 4 |
+
import logging
|
| 5 |
from flask import Flask, render_template, request, jsonify
|
| 6 |
|
| 7 |
app = Flask(__name__)
|
| 8 |
|
| 9 |
+
# إعداد السجلات (Logging)
|
| 10 |
+
logging.basicConfig(level=logging.INFO)
|
| 11 |
+
logger = logging.getLogger("web_app")
|
| 12 |
+
|
| 13 |
+
# جلب الإعدادات الحساسة
|
| 14 |
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]
|
| 28 |
+
try:
|
| 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 {
|
|
|
|
| 48 |
for c in m.get("capitulos", [])
|
| 49 |
]
|
| 50 |
}
|
| 51 |
+
except Exception as e:
|
| 52 |
+
logger.error(f"💥 Scraper Error: {str(e)}")
|
| 53 |
+
return None
|
| 54 |
|
| 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 []
|
|
|
|
| 62 |
data = await resp.json()
|
| 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("/")
|
| 70 |
def index():
|
| 71 |
return render_template("index.html")
|
|
|
|
| 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>")
|