Spaces:
Paused
Paused
Update app.py
Browse files
app.py
CHANGED
|
@@ -12,9 +12,6 @@ def home():
|
|
| 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"}
|
|
@@ -23,7 +20,7 @@ def search_anime(keyword: str):
|
|
| 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")
|
| 27 |
|
| 28 |
results = []
|
| 29 |
for item in items:
|
|
@@ -34,7 +31,6 @@ def search_anime(keyword: str):
|
|
| 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")
|
|
@@ -45,7 +41,6 @@ def search_anime(keyword: str):
|
|
| 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 |
|
|
@@ -58,6 +53,19 @@ def search_anime(keyword: str):
|
|
| 58 |
"type": anime_type
|
| 59 |
})
|
| 60 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 61 |
return {"keyword": keyword, "results": results}
|
| 62 |
|
| 63 |
except Exception as e:
|
|
|
|
| 12 |
|
| 13 |
@app.get("/search")
|
| 14 |
def search_anime(keyword: str):
|
|
|
|
|
|
|
|
|
|
| 15 |
try:
|
| 16 |
url = f"https://www.aniwave.se/filter?keyword={keyword}"
|
| 17 |
headers = {"User-Agent": "Mozilla/5.0"}
|
|
|
|
| 20 |
raise HTTPException(status_code=500, detail="Failed to fetch search page")
|
| 21 |
|
| 22 |
soup = BeautifulSoup(resp.text, "html.parser")
|
| 23 |
+
items = soup.select("div.item")
|
| 24 |
|
| 25 |
results = []
|
| 26 |
for item in items:
|
|
|
|
| 31 |
thumb_elem = item.select_one("div.ani.poster img")
|
| 32 |
thumbnail = thumb_elem.get("src") if thumb_elem else None
|
| 33 |
|
|
|
|
| 34 |
sub_elem = item.select_one("span.ep-status.sub span")
|
| 35 |
dub_elem = item.select_one("span.ep-status.dub span")
|
| 36 |
total_elem = item.select_one("span.ep-status.total span")
|
|
|
|
| 41 |
"total": int(total_elem.text.strip()) if total_elem else None
|
| 42 |
}
|
| 43 |
|
|
|
|
| 44 |
type_elem = item.select_one("div.meta .inner .right")
|
| 45 |
anime_type = type_elem.text.strip() if type_elem else None
|
| 46 |
|
|
|
|
| 53 |
"type": anime_type
|
| 54 |
})
|
| 55 |
|
| 56 |
+
# Custom ranking
|
| 57 |
+
keyword_lower = keyword.lower()
|
| 58 |
+
def rank(item):
|
| 59 |
+
t = item["title"].lower()
|
| 60 |
+
if t == keyword_lower:
|
| 61 |
+
return 0 # exact match first
|
| 62 |
+
elif t.startswith(keyword_lower):
|
| 63 |
+
return 1 # starts with keyword second
|
| 64 |
+
else:
|
| 65 |
+
return 2 # the rest
|
| 66 |
+
|
| 67 |
+
results = sorted(results, key=lambda x: (rank(x), x["title"].lower()))
|
| 68 |
+
|
| 69 |
return {"keyword": keyword, "results": results}
|
| 70 |
|
| 71 |
except Exception as e:
|