Update app.py
Browse files
app.py
CHANGED
|
@@ -16,8 +16,11 @@ HF_AYANO_BASE = "https://a-y-a-n-o-k-o-j-i-dnd-api.hf.space"
|
|
| 16 |
QUALITIES = ["360p", "720p", "1080p"]
|
| 17 |
queue_lock = Lock()
|
| 18 |
|
| 19 |
-
def
|
| 20 |
-
print(f"[
|
|
|
|
|
|
|
|
|
|
| 21 |
|
| 22 |
class StartPayload(BaseModel):
|
| 23 |
anime_id: str
|
|
@@ -25,33 +28,34 @@ class StartPayload(BaseModel):
|
|
| 25 |
|
| 26 |
def download_video(anime_id: str, episode: int, quality: str) -> str | None:
|
| 27 |
url = f"{HF_AYANO_BASE}/anime/download?id={anime_id}&episode={episode}&quality={quality}"
|
| 28 |
-
log(f"Fetching link ep {episode} {quality}")
|
| 29 |
headers = {
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
}
|
| 33 |
-
try:
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
|
|
|
|
|
|
| 38 |
return None
|
| 39 |
-
|
| 40 |
-
except:
|
| 41 |
-
|
|
|
|
|
|
|
| 42 |
return None
|
| 43 |
|
| 44 |
tmp_path = tempfile.mktemp(suffix=".mp4")
|
| 45 |
-
log(f"Downloading ep {episode} {quality}")
|
| 46 |
try:
|
| 47 |
with requests.get(video_url, headers=headers, stream=True, timeout=120) as r:
|
| 48 |
r.raise_for_status()
|
| 49 |
with open(tmp_path, "wb") as f:
|
| 50 |
shutil.copyfileobj(r.raw, f)
|
| 51 |
-
log(f"Downloaded ep {episode} {quality}")
|
| 52 |
return tmp_path
|
| 53 |
-
except:
|
| 54 |
-
|
| 55 |
if os.path.exists(tmp_path):
|
| 56 |
os.remove(tmp_path)
|
| 57 |
return None
|
|
@@ -61,16 +65,13 @@ def get_filename(anime_name: str, ep: int, quality: str) -> str:
|
|
| 61 |
return f"nt-animes_{slug}_ep{ep}_{quality}.mp4"
|
| 62 |
|
| 63 |
def upload_to_0x0(file_path: str, file_name: str) -> str:
|
| 64 |
-
log(f"Uploading {file_name}")
|
| 65 |
try:
|
| 66 |
with open(file_path, "rb") as f:
|
| 67 |
r = requests.post(UPLOAD_URL, files={"file": (file_name, f)}, timeout=180)
|
| 68 |
r.raise_for_status()
|
| 69 |
-
|
| 70 |
-
log(f"Uploaded: {url}")
|
| 71 |
-
return url
|
| 72 |
except Exception as e:
|
| 73 |
-
|
| 74 |
raise
|
| 75 |
|
| 76 |
def notify_render(anime_id: str, episode: int, quality: str, file_url: str, file_name: str, status: int):
|
|
@@ -82,11 +83,10 @@ def notify_render(anime_id: str, episode: int, quality: str, file_url: str, file
|
|
| 82 |
"file_name": file_name,
|
| 83 |
"status": status
|
| 84 |
}
|
| 85 |
-
log(f"Notifying Render status={status} ep={episode} {quality}")
|
| 86 |
try:
|
| 87 |
requests.post(RENDER_UPDATE_ENDPOINT, json=payload, timeout=10)
|
| 88 |
except Exception as e:
|
| 89 |
-
|
| 90 |
|
| 91 |
def blur_video(input_path: str) -> str:
|
| 92 |
output = tempfile.mktemp(suffix="_blurred.mp4")
|
|
@@ -101,39 +101,41 @@ def blur_video(input_path: str) -> str:
|
|
| 101 |
"-c:v", "libx264", "-preset", "fast", "-crf", "23",
|
| 102 |
"-c:a", "copy", output
|
| 103 |
]
|
| 104 |
-
|
| 105 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 106 |
|
| 107 |
def process_anime(anime_id: str, anime_name: str):
|
| 108 |
-
log(f"Started {anime_id} - {anime_name}")
|
| 109 |
episode = 1
|
| 110 |
while True:
|
| 111 |
processed = False
|
| 112 |
for quality in QUALITIES:
|
| 113 |
try:
|
| 114 |
-
|
| 115 |
-
if not
|
| 116 |
continue
|
| 117 |
-
|
| 118 |
-
|
| 119 |
-
|
| 120 |
-
|
| 121 |
-
|
| 122 |
-
|
| 123 |
-
notify_render(anime_id, episode, quality, url, name, 2)
|
| 124 |
processed = True
|
| 125 |
-
except:
|
|
|
|
|
|
|
|
|
|
| 126 |
notify_render(anime_id, episode, quality, "", "", 3)
|
| 127 |
if not processed:
|
| 128 |
-
log("No more episodes")
|
| 129 |
notify_render(anime_id, 0, "", "", "", 5)
|
| 130 |
break
|
| 131 |
episode += 1
|
| 132 |
-
log("Complete")
|
| 133 |
|
| 134 |
@app.post("/start")
|
| 135 |
-
def
|
| 136 |
-
log(f"Queueing {payload.anime_id} - {payload.anime_name}")
|
| 137 |
with queue_lock:
|
| 138 |
-
|
| 139 |
-
return {"code": 4, "message": "
|
|
|
|
| 16 |
QUALITIES = ["360p", "720p", "1080p"]
|
| 17 |
queue_lock = Lock()
|
| 18 |
|
| 19 |
+
def log_error(msg: str):
|
| 20 |
+
print(f"[ERROR] {msg}", flush=True)
|
| 21 |
+
|
| 22 |
+
class EpisodeExceedsAvailableCount(Exception):
|
| 23 |
+
pass
|
| 24 |
|
| 25 |
class StartPayload(BaseModel):
|
| 26 |
anime_id: str
|
|
|
|
| 28 |
|
| 29 |
def download_video(anime_id: str, episode: int, quality: str) -> str | None:
|
| 30 |
url = f"{HF_AYANO_BASE}/anime/download?id={anime_id}&episode={episode}&quality={quality}"
|
|
|
|
| 31 |
headers = {
|
| 32 |
+
"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",
|
| 33 |
+
"Referer": "https://animepahe.si/"
|
| 34 |
+
}
|
| 35 |
+
try:
|
| 36 |
+
resp = requests.get(url, headers=headers, timeout=20)
|
| 37 |
+
resp.raise_for_status()
|
| 38 |
+
data = resp.json()
|
| 39 |
+
if data.get("status") == 422:
|
| 40 |
+
raise EpisodeExceedsAvailableCount()
|
| 41 |
+
if data.get("status") != 200:
|
| 42 |
return None
|
| 43 |
+
video_url = data["direct_link"]
|
| 44 |
+
except EpisodeExceedsAvailableCount:
|
| 45 |
+
raise
|
| 46 |
+
except Exception as e:
|
| 47 |
+
log_error(f"Link failed ep {episode} {quality}: {e}")
|
| 48 |
return None
|
| 49 |
|
| 50 |
tmp_path = tempfile.mktemp(suffix=".mp4")
|
|
|
|
| 51 |
try:
|
| 52 |
with requests.get(video_url, headers=headers, stream=True, timeout=120) as r:
|
| 53 |
r.raise_for_status()
|
| 54 |
with open(tmp_path, "wb") as f:
|
| 55 |
shutil.copyfileobj(r.raw, f)
|
|
|
|
| 56 |
return tmp_path
|
| 57 |
+
except Exception as e:
|
| 58 |
+
log_error(f"Download failed ep {episode} {quality}: {e}")
|
| 59 |
if os.path.exists(tmp_path):
|
| 60 |
os.remove(tmp_path)
|
| 61 |
return None
|
|
|
|
| 65 |
return f"nt-animes_{slug}_ep{ep}_{quality}.mp4"
|
| 66 |
|
| 67 |
def upload_to_0x0(file_path: str, file_name: str) -> str:
|
|
|
|
| 68 |
try:
|
| 69 |
with open(file_path, "rb") as f:
|
| 70 |
r = requests.post(UPLOAD_URL, files={"file": (file_name, f)}, timeout=180)
|
| 71 |
r.raise_for_status()
|
| 72 |
+
return r.text.strip()
|
|
|
|
|
|
|
| 73 |
except Exception as e:
|
| 74 |
+
log_error(f"Upload failed: {e}")
|
| 75 |
raise
|
| 76 |
|
| 77 |
def notify_render(anime_id: str, episode: int, quality: str, file_url: str, file_name: str, status: int):
|
|
|
|
| 83 |
"file_name": file_name,
|
| 84 |
"status": status
|
| 85 |
}
|
|
|
|
| 86 |
try:
|
| 87 |
requests.post(RENDER_UPDATE_ENDPOINT, json=payload, timeout=10)
|
| 88 |
except Exception as e:
|
| 89 |
+
log_error(f"Notify failed: {e}")
|
| 90 |
|
| 91 |
def blur_video(input_path: str) -> str:
|
| 92 |
output = tempfile.mktemp(suffix="_blurred.mp4")
|
|
|
|
| 101 |
"-c:v", "libx264", "-preset", "fast", "-crf", "23",
|
| 102 |
"-c:a", "copy", output
|
| 103 |
]
|
| 104 |
+
try:
|
| 105 |
+
subprocess.run(cmd, check=True, stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)
|
| 106 |
+
return output
|
| 107 |
+
except Exception as e:
|
| 108 |
+
log_error(f"Blur failed: {e}")
|
| 109 |
+
raise
|
| 110 |
|
| 111 |
def process_anime(anime_id: str, anime_name: str):
|
|
|
|
| 112 |
episode = 1
|
| 113 |
while True:
|
| 114 |
processed = False
|
| 115 |
for quality in QUALITIES:
|
| 116 |
try:
|
| 117 |
+
local_file = download_video(anime_id, episode, quality)
|
| 118 |
+
if not local_file:
|
| 119 |
continue
|
| 120 |
+
blurred_file = blur_video(local_file)
|
| 121 |
+
os.remove(local_file)
|
| 122 |
+
file_name = get_filename(anime_name, episode, quality)
|
| 123 |
+
file_url = upload_to_0x0(blurred_file, file_name)
|
| 124 |
+
os.remove(blurred_file)
|
| 125 |
+
notify_render(anime_id, episode, quality, file_url, file_name, 2)
|
|
|
|
| 126 |
processed = True
|
| 127 |
+
except EpisodeExceedsAvailableCount:
|
| 128 |
+
notify_render(anime_id, 0, "", "", "", 5)
|
| 129 |
+
return
|
| 130 |
+
except Exception:
|
| 131 |
notify_render(anime_id, episode, quality, "", "", 3)
|
| 132 |
if not processed:
|
|
|
|
| 133 |
notify_render(anime_id, 0, "", "", "", 5)
|
| 134 |
break
|
| 135 |
episode += 1
|
|
|
|
| 136 |
|
| 137 |
@app.post("/start")
|
| 138 |
+
def start_endpoint(payload: StartPayload, background_tasks: BackgroundTasks):
|
|
|
|
| 139 |
with queue_lock:
|
| 140 |
+
background_tasks.add_task(process_anime, payload.anime_id, payload.anime_name)
|
| 141 |
+
return {"code": 4, "message": "Job queued and processing in background"}
|