Neon-AI commited on
Commit
451e011
·
verified ·
1 Parent(s): fbd240a

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +20 -31
app.py CHANGED
@@ -36,43 +36,32 @@ def get_episode(anime: str, episode: int, dub: str = "sub"):
36
  # ===== PATCH: /search endpoint =====
37
  @app.get("/search")
38
  def search_anime(keyword: str):
39
- if not keyword:
40
- raise HTTPException(status_code=400, detail="Keyword is required")
41
-
42
  try:
43
- url = f"https://hianime.to/search?keyword={keyword}"
44
- headers = {
45
- "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) "
46
- "AppleWebKit/537.36 (KHTML, like Gecko) "
47
- "Chrome/120.0.0.0 Safari/537.36"
48
- }
49
- resp = requests.get(url, headers=headers, timeout=15)
50
- if resp.status_code != 200:
51
- raise HTTPException(status_code=resp.status_code, detail="Failed to fetch search page")
52
-
53
- soup = BeautifulSoup(resp.text, "html.parser")
54
- items = soup.select(".flw-item")
55
  results = []
56
- for item in items:
57
- title_elem = item.select_one(".film-name a")
58
- thumb_elem = item.select_one("img")
59
- thumbnail = None
 
60
 
61
- if thumb_elem:
62
- # Try multiple attributes for lazy-loaded images
63
- for attr in ["src", "data-src", "data-lazy-src", "data-original"]:
64
- thumbnail = thumb_elem.get(attr)
65
- if thumbnail:
66
- break
 
67
 
68
- results.append({
69
- "title": title_elem.text.strip() if title_elem else None,
70
- "url": title_elem["href"] if title_elem else None,
71
- "thumbnail": thumbnail
72
- })
 
73
 
74
- # Filter out incomplete results
75
  results = [r for r in results if r["title"] and r["url"]]
 
 
76
  except Exception as e:
77
  raise HTTPException(status_code=500, detail=f"Failed to search anime: {str(e)}")
78
  # ===== END PATCH =====
 
36
  # ===== PATCH: /search endpoint =====
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:
66
  raise HTTPException(status_code=500, detail=f"Failed to search anime: {str(e)}")
67
  # ===== END PATCH =====