Update app.py
Browse files
app.py
CHANGED
|
@@ -29,6 +29,7 @@ class StartPayload(BaseModel):
|
|
| 29 |
|
| 30 |
def download_video(anime_id: str, episode: int, quality: str) -> str | None:
|
| 31 |
url = f"{HF_AYANO_BASE}/anime/download?id={anime_id}&episode={episode}&quality={quality}"
|
|
|
|
| 32 |
headers = {
|
| 33 |
"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",
|
| 34 |
"Referer": "https://animepahe.si/"
|
|
@@ -40,21 +41,27 @@ def download_video(anime_id: str, episode: int, quality: str) -> str | None:
|
|
| 40 |
if data.get("status") == 422:
|
| 41 |
raise EpisodeExceedsAvailableCount()
|
| 42 |
if data.get("status") != 200:
|
|
|
|
| 43 |
return None
|
| 44 |
video_url = data["direct_link"]
|
|
|
|
| 45 |
except EpisodeExceedsAvailableCount:
|
| 46 |
raise
|
| 47 |
-
except:
|
|
|
|
| 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:
|
|
|
|
| 58 |
if os.path.exists(tmp_path):
|
| 59 |
os.remove(tmp_path)
|
| 60 |
return None
|
|
@@ -64,13 +71,16 @@ def get_filename(anime_name: str, ep: int, quality: str) -> str:
|
|
| 64 |
return f"nt-animes_{slug}_ep{ep}_{quality}.mp4"
|
| 65 |
|
| 66 |
def upload_to_0x0(file_path: str, file_name: str) -> str:
|
|
|
|
| 67 |
try:
|
| 68 |
with open(file_path, "rb") as f:
|
| 69 |
r = requests.post(UPLOAD_URL, files={"file": (file_name, f)}, timeout=180)
|
| 70 |
r.raise_for_status()
|
| 71 |
-
|
|
|
|
|
|
|
| 72 |
except Exception as e:
|
| 73 |
-
log(f"
|
| 74 |
raise
|
| 75 |
|
| 76 |
def notify_render(anime_id: str, episode: int, quality: str, file_url: str, file_name: str, status: int):
|
|
@@ -82,10 +92,11 @@ def notify_render(anime_id: str, episode: int, quality: str, file_url: str, file
|
|
| 82 |
"file_name": file_name,
|
| 83 |
"status": status
|
| 84 |
}
|
|
|
|
| 85 |
try:
|
| 86 |
requests.post(RENDER_UPDATE_ENDPOINT, json=payload, timeout=20)
|
| 87 |
except Exception as e:
|
| 88 |
-
log(f"
|
| 89 |
|
| 90 |
def blur_video(input_path: str) -> str:
|
| 91 |
output = tempfile.mktemp(suffix="_blurred.mp4")
|
|
@@ -100,11 +111,17 @@ def blur_video(input_path: str) -> str:
|
|
| 100 |
"-c:v", "libx264", "-preset", "fast", "-crf", "23",
|
| 101 |
"-c:a", "copy", output
|
| 102 |
]
|
| 103 |
-
|
| 104 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 105 |
|
| 106 |
def process_anime(anime_id: str, anime_name: str):
|
| 107 |
-
log(f"Started {anime_id} - {anime_name}")
|
| 108 |
episode = 1
|
| 109 |
while True:
|
| 110 |
processed = False
|
|
@@ -121,15 +138,18 @@ def process_anime(anime_id: str, anime_name: str):
|
|
| 121 |
notify_render(anime_id, episode, quality, file_url, file_name, 2)
|
| 122 |
processed = True
|
| 123 |
except EpisodeExceedsAvailableCount:
|
|
|
|
| 124 |
notify_render(anime_id, 0, "", "", "", 5)
|
| 125 |
return
|
| 126 |
-
except:
|
|
|
|
| 127 |
notify_render(anime_id, episode, quality, "", "", 3)
|
| 128 |
if not processed:
|
|
|
|
| 129 |
notify_render(anime_id, 0, "", "", "", 5)
|
| 130 |
break
|
| 131 |
episode += 1
|
| 132 |
-
log("
|
| 133 |
|
| 134 |
@app.post("/start")
|
| 135 |
def start(payload: StartPayload, bg: BackgroundTasks):
|
|
|
|
| 29 |
|
| 30 |
def download_video(anime_id: str, episode: int, quality: str) -> str | None:
|
| 31 |
url = f"{HF_AYANO_BASE}/anime/download?id={anime_id}&episode={episode}&quality={quality}"
|
| 32 |
+
log(f"Fetching link ep {episode} {quality}")
|
| 33 |
headers = {
|
| 34 |
"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",
|
| 35 |
"Referer": "https://animepahe.si/"
|
|
|
|
| 41 |
if data.get("status") == 422:
|
| 42 |
raise EpisodeExceedsAvailableCount()
|
| 43 |
if data.get("status") != 200:
|
| 44 |
+
log(f"No link available ep {episode} {quality}")
|
| 45 |
return None
|
| 46 |
video_url = data["direct_link"]
|
| 47 |
+
log(f"Got direct link ep {episode} {quality}")
|
| 48 |
except EpisodeExceedsAvailableCount:
|
| 49 |
raise
|
| 50 |
+
except Exception as e:
|
| 51 |
+
log(f"Link error ep {episode} {quality}: {e}")
|
| 52 |
return None
|
| 53 |
|
| 54 |
tmp_path = tempfile.mktemp(suffix=".mp4")
|
| 55 |
+
log(f"Downloading ep {episode} {quality}")
|
| 56 |
try:
|
| 57 |
with requests.get(video_url, headers=headers, stream=True, timeout=120) as r:
|
| 58 |
r.raise_for_status()
|
| 59 |
with open(tmp_path, "wb") as f:
|
| 60 |
shutil.copyfileobj(r.raw, f)
|
| 61 |
+
log(f"Downloaded ep {episode} {quality}")
|
| 62 |
return tmp_path
|
| 63 |
+
except Exception as e:
|
| 64 |
+
log(f"Download failed ep {episode} {quality}: {e}")
|
| 65 |
if os.path.exists(tmp_path):
|
| 66 |
os.remove(tmp_path)
|
| 67 |
return None
|
|
|
|
| 71 |
return f"nt-animes_{slug}_ep{ep}_{quality}.mp4"
|
| 72 |
|
| 73 |
def upload_to_0x0(file_path: str, file_name: str) -> str:
|
| 74 |
+
log(f"Uploading {file_name}")
|
| 75 |
try:
|
| 76 |
with open(file_path, "rb") as f:
|
| 77 |
r = requests.post(UPLOAD_URL, files={"file": (file_name, f)}, timeout=180)
|
| 78 |
r.raise_for_status()
|
| 79 |
+
url = r.text.strip()
|
| 80 |
+
log(f"Uploaded {file_name}: {url}")
|
| 81 |
+
return url
|
| 82 |
except Exception as e:
|
| 83 |
+
log(f"Upload failed: {e}")
|
| 84 |
raise
|
| 85 |
|
| 86 |
def notify_render(anime_id: str, episode: int, quality: str, file_url: str, file_name: str, status: int):
|
|
|
|
| 92 |
"file_name": file_name,
|
| 93 |
"status": status
|
| 94 |
}
|
| 95 |
+
log(f"Notifying Render status={status} ep={episode} {quality}")
|
| 96 |
try:
|
| 97 |
requests.post(RENDER_UPDATE_ENDPOINT, json=payload, timeout=20)
|
| 98 |
except Exception as e:
|
| 99 |
+
log(f"Notify failed: {e}")
|
| 100 |
|
| 101 |
def blur_video(input_path: str) -> str:
|
| 102 |
output = tempfile.mktemp(suffix="_blurred.mp4")
|
|
|
|
| 111 |
"-c:v", "libx264", "-preset", "fast", "-crf", "23",
|
| 112 |
"-c:a", "copy", output
|
| 113 |
]
|
| 114 |
+
log("Blurring video")
|
| 115 |
+
try:
|
| 116 |
+
subprocess.run(cmd, check=True, stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)
|
| 117 |
+
log("Blur complete")
|
| 118 |
+
return output
|
| 119 |
+
except Exception as e:
|
| 120 |
+
log(f"Blur failed: {e}")
|
| 121 |
+
raise
|
| 122 |
|
| 123 |
def process_anime(anime_id: str, anime_name: str):
|
| 124 |
+
log(f"Started processing {anime_id} - {anime_name}")
|
| 125 |
episode = 1
|
| 126 |
while True:
|
| 127 |
processed = False
|
|
|
|
| 138 |
notify_render(anime_id, episode, quality, file_url, file_name, 2)
|
| 139 |
processed = True
|
| 140 |
except EpisodeExceedsAvailableCount:
|
| 141 |
+
log("All episodes processed")
|
| 142 |
notify_render(anime_id, 0, "", "", "", 5)
|
| 143 |
return
|
| 144 |
+
except Exception as e:
|
| 145 |
+
log(f"Error ep {episode} {quality}: {e}")
|
| 146 |
notify_render(anime_id, episode, quality, "", "", 3)
|
| 147 |
if not processed:
|
| 148 |
+
log("No more episodes")
|
| 149 |
notify_render(anime_id, 0, "", "", "", 5)
|
| 150 |
break
|
| 151 |
episode += 1
|
| 152 |
+
log("Processing finished")
|
| 153 |
|
| 154 |
@app.post("/start")
|
| 155 |
def start(payload: StartPayload, bg: BackgroundTasks):
|