Spaces:
Paused
Paused
Update app.py
Browse files
app.py
CHANGED
|
@@ -37,29 +37,35 @@ def get_episode(anime: str, episode: int, dub: str = "sub"):
|
|
| 37 |
@app.get("/search")
|
| 38 |
def search_anime(keyword: str):
|
| 39 |
try:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 40 |
results = []
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
url = title_elem.get_attribute("href") if title_elem else None
|
| 53 |
-
thumbnail = thumb_elem.get_attribute("src") if thumb_elem else None
|
| 54 |
|
|
|
|
| 55 |
results.append({
|
| 56 |
"title": title,
|
| 57 |
"url": url,
|
| 58 |
"thumbnail": thumbnail
|
| 59 |
})
|
| 60 |
-
browser.close()
|
| 61 |
|
| 62 |
-
results = [r for r in results if r["title"] and r["url"]]
|
| 63 |
return {"keyword": keyword, "results": results}
|
| 64 |
|
| 65 |
except Exception as e:
|
|
|
|
| 37 |
@app.get("/search")
|
| 38 |
def search_anime(keyword: str):
|
| 39 |
try:
|
| 40 |
+
url = f"https://hianime.to/search?keyword={keyword}"
|
| 41 |
+
headers = {"User-Agent": "Mozilla/5.0"}
|
| 42 |
+
resp = requests.get(url, headers=headers)
|
| 43 |
+
if resp.status_code != 200:
|
| 44 |
+
raise HTTPException(status_code=500, detail="Failed to fetch search page")
|
| 45 |
+
|
| 46 |
+
soup = BeautifulSoup(resp.text, "html.parser")
|
| 47 |
+
items = soup.select(".film-name") # each item container
|
| 48 |
+
|
| 49 |
results = []
|
| 50 |
+
for item in items:
|
| 51 |
+
# title and URL
|
| 52 |
+
title_elem = item.select_one("a")
|
| 53 |
+
title = title_elem.text.strip() if title_elem else None
|
| 54 |
+
url = title_elem["href"] if title_elem else None
|
| 55 |
|
| 56 |
+
# thumbnail
|
| 57 |
+
thumb_elem = item.find_previous("img", class_="film-poster-img")
|
| 58 |
+
thumbnail = None
|
| 59 |
+
if thumb_elem:
|
| 60 |
+
thumbnail = thumb_elem.get("data-src") or thumb_elem.get("src")
|
|
|
|
|
|
|
| 61 |
|
| 62 |
+
if title and url:
|
| 63 |
results.append({
|
| 64 |
"title": title,
|
| 65 |
"url": url,
|
| 66 |
"thumbnail": thumbnail
|
| 67 |
})
|
|
|
|
| 68 |
|
|
|
|
| 69 |
return {"keyword": keyword, "results": results}
|
| 70 |
|
| 71 |
except Exception as e:
|