Neon-AI commited on
Commit
bf5c2f2
·
verified ·
1 Parent(s): f54e46e

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +35 -0
app.py CHANGED
@@ -31,6 +31,41 @@ def get_episode(anime: str, episode: int, dub: str = "sub"):
31
  except Exception as e:
32
  raise HTTPException(status_code=500, detail=f"Failed to scrape episode: {str(e)}")
33
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
34
  if __name__ == "__main__":
35
  import uvicorn
36
  port = int(os.environ.get("PORT", 7860)) # HF Spaces sets this automatically
 
31
  except Exception as e:
32
  raise HTTPException(status_code=500, detail=f"Failed to scrape episode: {str(e)}")
33
 
34
+ # ===== PATCH: /search endpoint =====
35
+ @app.get("/search")
36
+ def search_anime(keyword: str):
37
+ if not keyword:
38
+ raise HTTPException(status_code=400, detail="Keyword is required")
39
+
40
+ try:
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
+ results = page.evaluate("""
48
+ () => {
49
+ const items = Array.from(document.querySelectorAll(".flw-item"));
50
+ return items.map(item => {
51
+ const titleElem = item.querySelector(".film-name a");
52
+ const thumbElem = item.querySelector("img");
53
+ return {
54
+ title: titleElem ? titleElem.textContent.trim() : null,
55
+ url: titleElem ? titleElem.href : null,
56
+ thumbnail: thumbElem ? thumbElem.src : null
57
+ };
58
+ });
59
+ }
60
+ """)
61
+ browser.close()
62
+
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 =====
68
+
69
  if __name__ == "__main__":
70
  import uvicorn
71
  port = int(os.environ.get("PORT", 7860)) # HF Spaces sets this automatically