Spaces:
Paused
Paused
Update app.py
Browse files
app.py
CHANGED
|
@@ -12,34 +12,50 @@ def home():
|
|
| 12 |
|
| 13 |
@app.get("/search")
|
| 14 |
def search_anime(keyword: str):
|
|
|
|
|
|
|
|
|
|
| 15 |
try:
|
| 16 |
-
url = f"https://
|
| 17 |
headers = {"User-Agent": "Mozilla/5.0"}
|
| 18 |
resp = requests.get(url, headers=headers)
|
| 19 |
if resp.status_code != 200:
|
| 20 |
raise HTTPException(status_code=500, detail="Failed to fetch search page")
|
| 21 |
|
| 22 |
soup = BeautifulSoup(resp.text, "html.parser")
|
| 23 |
-
items = soup.select(".
|
| 24 |
|
| 25 |
results = []
|
| 26 |
for item in items:
|
| 27 |
-
|
| 28 |
-
title_elem = item.select_one("a")
|
| 29 |
title = title_elem.text.strip() if title_elem else None
|
| 30 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 31 |
|
| 32 |
-
#
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
if thumb_elem:
|
| 36 |
-
thumbnail = thumb_elem.get("data-src") or thumb_elem.get("src")
|
| 37 |
|
| 38 |
-
if title and
|
| 39 |
results.append({
|
| 40 |
"title": title,
|
| 41 |
-
"url":
|
| 42 |
-
"thumbnail": thumbnail
|
|
|
|
|
|
|
| 43 |
})
|
| 44 |
|
| 45 |
return {"keyword": keyword, "results": results}
|
|
|
|
| 12 |
|
| 13 |
@app.get("/search")
|
| 14 |
def search_anime(keyword: str):
|
| 15 |
+
"""
|
| 16 |
+
Example call: /search?keyword=naruto
|
| 17 |
+
"""
|
| 18 |
try:
|
| 19 |
+
url = f"https://www.aniwave.se/filter?keyword={keyword}"
|
| 20 |
headers = {"User-Agent": "Mozilla/5.0"}
|
| 21 |
resp = requests.get(url, headers=headers)
|
| 22 |
if resp.status_code != 200:
|
| 23 |
raise HTTPException(status_code=500, detail="Failed to fetch search page")
|
| 24 |
|
| 25 |
soup = BeautifulSoup(resp.text, "html.parser")
|
| 26 |
+
items = soup.select("div.item") # each anime container
|
| 27 |
|
| 28 |
results = []
|
| 29 |
for item in items:
|
| 30 |
+
title_elem = item.select_one("a.name.d-title")
|
|
|
|
| 31 |
title = title_elem.text.strip() if title_elem else None
|
| 32 |
+
path = title_elem["href"] if title_elem else None
|
| 33 |
+
|
| 34 |
+
thumb_elem = item.select_one("div.ani.poster img")
|
| 35 |
+
thumbnail = thumb_elem.get("src") if thumb_elem else None
|
| 36 |
+
|
| 37 |
+
# Episodes
|
| 38 |
+
sub_elem = item.select_one("span.ep-status.sub span")
|
| 39 |
+
dub_elem = item.select_one("span.ep-status.dub span")
|
| 40 |
+
total_elem = item.select_one("span.ep-status.total span")
|
| 41 |
+
|
| 42 |
+
episodes = {
|
| 43 |
+
"sub": int(sub_elem.text.strip()) if sub_elem else None,
|
| 44 |
+
"dub": int(dub_elem.text.strip()) if dub_elem else None,
|
| 45 |
+
"total": int(total_elem.text.strip()) if total_elem else None
|
| 46 |
+
}
|
| 47 |
|
| 48 |
+
# Type (Tv, Special, Movie)
|
| 49 |
+
type_elem = item.select_one("div.meta .inner .right")
|
| 50 |
+
anime_type = type_elem.text.strip() if type_elem else None
|
|
|
|
|
|
|
| 51 |
|
| 52 |
+
if title and path:
|
| 53 |
results.append({
|
| 54 |
"title": title,
|
| 55 |
+
"url": path,
|
| 56 |
+
"thumbnail": thumbnail,
|
| 57 |
+
"episodes": episodes,
|
| 58 |
+
"type": anime_type
|
| 59 |
})
|
| 60 |
|
| 61 |
return {"keyword": keyword, "results": results}
|