Neon-AI commited on
Commit
e398dc5
·
verified ·
1 Parent(s): 3eebe73

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +73 -12
app.py CHANGED
@@ -19,6 +19,9 @@ HF_AYANO_BASE = "https://a-y-a-n-o-k-o-j-i-dnd-api.hf.space"
19
  QUALITIES = ["360p", "720p", "1080p"]
20
  queue_lock = Lock()
21
 
 
 
 
22
  class EpisodeExceedsAvailableCount(Exception):
23
  pass
24
 
@@ -28,23 +31,36 @@ class StartPayload(BaseModel):
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
- resp = requests.get(url)
 
 
 
 
32
  if resp.status_code != 200:
 
33
  return None
 
34
  data = resp.json()
 
35
 
36
  if data.get("status") == 422:
37
- raise EpisodeExceedsAvailableCount(f"Episode {episode} exceeds available count")
38
 
39
  if data.get("status") != 200:
 
40
  return None
41
 
42
  video_url = data["direct_link"]
 
 
43
  tmp_path = tempfile.mktemp(suffix=".mp4")
44
- with requests.get(video_url, stream=True) as r:
 
45
  r.raise_for_status()
46
  with open(tmp_path, "wb") as f:
47
  shutil.copyfileobj(r.raw, f)
 
 
48
  return tmp_path
49
 
50
  def get_filename(anime_name: str, ep: int, quality: str) -> str:
@@ -52,14 +68,20 @@ def get_filename(anime_name: str, ep: int, quality: str) -> str:
52
  return f"nt-animes_{slug}_ep{ep}_{quality}.mp4"
53
 
54
  def upload_to_catbox_public(file_path: str, file_name: str) -> str:
 
 
55
  with open(file_path, "rb") as f:
56
  files = {"fileToUpload": (file_name, f)}
57
  data = {"reqtype": "fileupload"}
58
- r = requests.post(CATBOX_UPLOAD_URL, data=data, files=files)
 
 
 
 
59
  r.raise_for_status()
60
  return r.text.strip()
61
 
62
- def notify_render(anime_id: str, episode: int, quality: str, file_url: str, file_name: str, status: int):
63
  payload = {
64
  "anime_id": anime_id,
65
  "episode": episode,
@@ -68,10 +90,14 @@ def notify_render(anime_id: str, episode: int, quality: str, file_url: str, file
68
  "file_name": file_name,
69
  "status": status
70
  }
 
 
 
71
  try:
72
- requests.post(RENDER_UPDATE_ENDPOINT, json=payload)
 
73
  except Exception as e:
74
- print("Failed to notify Render:", e)
75
 
76
  def blur_video(input_path: str) -> str:
77
  tmp_video = tempfile.mktemp(suffix="_video.mp4")
@@ -122,38 +148,73 @@ def blur_video(input_path: str) -> str:
122
  return final_video
123
 
124
  def process_anime(anime_id: str, anime_name: str):
 
 
125
  episode = 1
126
  while True:
 
127
  episode_processed = False
 
128
  for quality in QUALITIES:
 
 
129
  try:
130
- result = download_video(anime_id, episode, quality)
131
- if result is None:
 
 
132
  continue
133
 
134
- local_file = result
 
135
  blurred_file = blur_video(local_file)
 
 
136
  os.remove(local_file)
 
137
 
138
  new_name = get_filename(anime_name, episode, quality)
 
 
139
  catbox_url = upload_to_catbox_public(blurred_file, new_name)
 
 
140
  os.remove(blurred_file)
 
 
 
 
 
 
 
141
 
142
- notify_render(anime_id, episode, quality, catbox_url, new_name, status=2)
143
  episode_processed = True
144
 
145
  except EpisodeExceedsAvailableCount:
 
 
 
146
  return
 
147
  except Exception as e:
148
- print(f"Error ep {episode} quality {quality}: {e}")
149
  notify_render(anime_id, episode, quality, "", "", status=3)
150
 
151
  if not episode_processed:
 
152
  break
 
153
  episode += 1
154
 
 
 
155
  @app.post("/start")
156
  def start_endpoint(payload: StartPayload, background_tasks: BackgroundTasks):
 
 
157
  with queue_lock:
 
158
  background_tasks.add_task(process_anime, payload.anime_id, payload.anime_name)
 
 
159
  return {"code": 4, "message": "Job queued and processing in background"}
 
19
  QUALITIES = ["360p", "720p", "1080p"]
20
  queue_lock = Lock()
21
 
22
+ def log(msg: str):
23
+ print(f"[HF] {msg}", flush=True)
24
+
25
  class EpisodeExceedsAvailableCount(Exception):
26
  pass
27
 
 
31
 
32
  def download_video(anime_id: str, episode: int, quality: str) -> str | None:
33
  url = f"{HF_AYANO_BASE}/anime/download?id={anime_id}&episode={episode}&quality={quality}"
34
+ log(f"Calling Ayano: {url}")
35
+
36
+ resp = requests.get(url, timeout=30)
37
+ log(f"Ayano HTTP {resp.status_code}")
38
+
39
  if resp.status_code != 200:
40
+ log("Ayano non-200 HTTP")
41
  return None
42
+
43
  data = resp.json()
44
+ log(f"Ayano JSON: {data}")
45
 
46
  if data.get("status") == 422:
47
+ raise EpisodeExceedsAvailableCount()
48
 
49
  if data.get("status") != 200:
50
+ log("Ayano returned non-200 status field")
51
  return None
52
 
53
  video_url = data["direct_link"]
54
+ log(f"Downloading video from: {video_url}")
55
+
56
  tmp_path = tempfile.mktemp(suffix=".mp4")
57
+
58
+ with requests.get(video_url, stream=True, timeout=60) as r:
59
  r.raise_for_status()
60
  with open(tmp_path, "wb") as f:
61
  shutil.copyfileobj(r.raw, f)
62
+
63
+ log(f"Video saved to {tmp_path}")
64
  return tmp_path
65
 
66
  def get_filename(anime_name: str, ep: int, quality: str) -> str:
 
68
  return f"nt-animes_{slug}_ep{ep}_{quality}.mp4"
69
 
70
  def upload_to_catbox_public(file_path: str, file_name: str) -> str:
71
+ log(f"Uploading to Catbox: {file_name}")
72
+
73
  with open(file_path, "rb") as f:
74
  files = {"fileToUpload": (file_name, f)}
75
  data = {"reqtype": "fileupload"}
76
+
77
+ r = requests.post(CATBOX_UPLOAD_URL, data=data, files=files, timeout=60)
78
+ log(f"Catbox response status {r.status_code}")
79
+ log(f"Catbox response text: {r.text}")
80
+
81
  r.raise_for_status()
82
  return r.text.strip()
83
 
84
+ def notify_render(anime_id, episode, quality, file_url, file_name, status):
85
  payload = {
86
  "anime_id": anime_id,
87
  "episode": episode,
 
90
  "file_name": file_name,
91
  "status": status
92
  }
93
+
94
+ log(f"Notify render → {payload}")
95
+
96
  try:
97
+ r = requests.post(RENDER_UPDATE_ENDPOINT, json=payload, timeout=10)
98
+ log(f"Render response {r.status_code}")
99
  except Exception as e:
100
+ log(f"⚠️ Render notify failed: {repr(e)}")
101
 
102
  def blur_video(input_path: str) -> str:
103
  tmp_video = tempfile.mktemp(suffix="_video.mp4")
 
148
  return final_video
149
 
150
  def process_anime(anime_id: str, anime_name: str):
151
+ log(f"PROCESS STARTED for anime_id={anime_id}")
152
+
153
  episode = 1
154
  while True:
155
+ log(f"--- EPISODE LOOP START ep={episode} ---")
156
  episode_processed = False
157
+
158
  for quality in QUALITIES:
159
+ log(f"Trying ep={episode}, quality={quality}")
160
+
161
  try:
162
+ local_file = download_video(anime_id, episode, quality)
163
+
164
+ if local_file is None:
165
+ log(f"No video for ep={episode} quality={quality}")
166
  continue
167
 
168
+ log(f"Downloaded file: {local_file}")
169
+
170
  blurred_file = blur_video(local_file)
171
+ log(f"Blurred file created: {blurred_file}")
172
+
173
  os.remove(local_file)
174
+ log(f"Removed original file")
175
 
176
  new_name = get_filename(anime_name, episode, quality)
177
+ log(f"Renamed to: {new_name}")
178
+
179
  catbox_url = upload_to_catbox_public(blurred_file, new_name)
180
+ log(f"Uploaded to Catbox: {catbox_url}")
181
+
182
  os.remove(blurred_file)
183
+ log("Removed blurred temp file")
184
+
185
+ notify_render(
186
+ anime_id, episode, quality,
187
+ catbox_url, new_name, status=2
188
+ )
189
+ log(f"Notified render SUCCESS ep={episode} quality={quality}")
190
 
 
191
  episode_processed = True
192
 
193
  except EpisodeExceedsAvailableCount:
194
+ log("🚨 EPISODE LIMIT REACHED (422)")
195
+ notify_render(anime_id, episode, "", "", "", status=5)
196
+ log("Sent FINAL status=5 to render, stopping")
197
  return
198
+
199
  except Exception as e:
200
+ log(f"❌ ERROR ep={episode} quality={quality}: {repr(e)}")
201
  notify_render(anime_id, episode, quality, "", "", status=3)
202
 
203
  if not episode_processed:
204
+ log("No qualities processed → stopping episode loop")
205
  break
206
+
207
  episode += 1
208
 
209
+ log("PROCESS FINISHED NORMALLY")
210
+
211
  @app.post("/start")
212
  def start_endpoint(payload: StartPayload, background_tasks: BackgroundTasks):
213
+ log(f"/start called with anime_id={payload.anime_id}, anime_name={payload.anime_name}")
214
+
215
  with queue_lock:
216
+ log("Queue lock acquired, scheduling background task")
217
  background_tasks.add_task(process_anime, payload.anime_id, payload.anime_name)
218
+
219
+ log("Background task scheduled, returning code 4")
220
  return {"code": 4, "message": "Job queued and processing in background"}