Spaces:
Paused
Paused
Update app.py
Browse files
app.py
CHANGED
|
@@ -46,6 +46,64 @@ def search_anime(keyword: str):
|
|
| 46 |
|
| 47 |
except Exception as e:
|
| 48 |
raise HTTPException(status_code=500, detail=f"Failed to search anime: {str(e)}")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 49 |
# ===== END PATCH =====
|
| 50 |
|
| 51 |
if __name__ == "__main__":
|
|
|
|
| 46 |
|
| 47 |
except Exception as e:
|
| 48 |
raise HTTPException(status_code=500, detail=f"Failed to search anime: {str(e)}")
|
| 49 |
+
|
| 50 |
+
@app.get("/api/anime")
|
| 51 |
+
def anime_metadata(url: str):
|
| 52 |
+
"""
|
| 53 |
+
Returns metadata and all episodes for a given anime page URL on HiAnime.
|
| 54 |
+
Example URL: https://hianime.to/naruto
|
| 55 |
+
"""
|
| 56 |
+
try:
|
| 57 |
+
headers = {"User-Agent": "Mozilla/5.0"}
|
| 58 |
+
resp = requests.get(url, headers=headers)
|
| 59 |
+
if resp.status_code != 200:
|
| 60 |
+
raise HTTPException(status_code=500, detail="Failed to fetch anime page")
|
| 61 |
+
|
| 62 |
+
soup = BeautifulSoup(resp.text, "html.parser")
|
| 63 |
+
|
| 64 |
+
# Title
|
| 65 |
+
title_elem = soup.select_one("h1.film-name")
|
| 66 |
+
title = title_elem.text.strip() if title_elem else None
|
| 67 |
+
|
| 68 |
+
# Description
|
| 69 |
+
desc_elem = soup.select_one(".film-description")
|
| 70 |
+
description = desc_elem.text.strip() if desc_elem else None
|
| 71 |
+
|
| 72 |
+
# Thumbnail
|
| 73 |
+
thumb_elem = soup.select_one(".film-poster-img")
|
| 74 |
+
thumbnail = thumb_elem.get("data-src") or thumb_elem.get("src") if thumb_elem else None
|
| 75 |
+
|
| 76 |
+
# Genres
|
| 77 |
+
genres = [g.text.strip() for g in soup.select(".film-genres a")]
|
| 78 |
+
|
| 79 |
+
# Rating
|
| 80 |
+
rating_elem = soup.select_one(".film-rate")
|
| 81 |
+
rating = rating_elem.text.strip() if rating_elem else None
|
| 82 |
+
|
| 83 |
+
# Episodes
|
| 84 |
+
ep_list = []
|
| 85 |
+
episode_items = soup.select(".film-episodes a")
|
| 86 |
+
for ep in episode_items:
|
| 87 |
+
ep_number = ep.text.strip()
|
| 88 |
+
ep_url = ep.get("href")
|
| 89 |
+
ep_type = "dub" if "dub" in ep_url.lower() else "sub"
|
| 90 |
+
ep_list.append({
|
| 91 |
+
"episode": ep_number,
|
| 92 |
+
"url": ep_url,
|
| 93 |
+
"type": ep_type
|
| 94 |
+
})
|
| 95 |
+
|
| 96 |
+
return {
|
| 97 |
+
"title": title,
|
| 98 |
+
"description": description,
|
| 99 |
+
"thumbnail": thumbnail,
|
| 100 |
+
"genres": genres,
|
| 101 |
+
"rating": rating,
|
| 102 |
+
"episodes": ep_list
|
| 103 |
+
}
|
| 104 |
+
|
| 105 |
+
except Exception as e:
|
| 106 |
+
raise HTTPException(status_code=500, detail=f"Failed to get anime metadata: {str(e)}")
|
| 107 |
# ===== END PATCH =====
|
| 108 |
|
| 109 |
if __name__ == "__main__":
|