Neon-AI commited on
Commit
086c0f9
·
verified ·
1 Parent(s): d851fdf

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +20 -14
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
- with sync_playwright() as p:
42
- browser = p.chromium.launch(headless=True)
43
- page = browser.new_page()
44
- url = f"https://hianime.to/search?keyword={keyword}"
45
- page.goto(url, wait_until="networkidle")
46
 
47
- items = page.query_selector_all(".film-detail") # adjust selector if needed
48
- for item in items:
49
- title_elem = item.query_selector(".film-name a")
50
- thumb_elem = item.query_selector("img")
51
- title = title_elem.inner_text().strip() if title_elem else None
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: