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

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +11 -2
app.py CHANGED
@@ -44,7 +44,13 @@ def search_anime(keyword: str):
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:
@@ -53,7 +59,7 @@ def search_anime(keyword: str):
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:
@@ -66,6 +72,9 @@ def search_anime(keyword: str):
66
  "thumbnail": thumbnail
67
  })
68
 
 
 
 
69
  return {"keyword": keyword, "results": results}
70
 
71
  except Exception as e:
 
44
  raise HTTPException(status_code=500, detail="Failed to fetch search page")
45
 
46
  soup = BeautifulSoup(resp.text, "html.parser")
47
+
48
+ # Only grab the actual search results container
49
+ search_section = soup.select_one(".search-result") # <-- correct container class
50
+ if not search_section:
51
+ return {"keyword": keyword, "results": []}
52
+
53
+ items = search_section.select(".film-name") # only titles inside search results
54
 
55
  results = []
56
  for item in items:
 
59
  title = title_elem.text.strip() if title_elem else None
60
  url = title_elem["href"] if title_elem else None
61
 
62
+ # thumbnail (lazy-loaded)
63
  thumb_elem = item.find_previous("img", class_="film-poster-img")
64
  thumbnail = None
65
  if thumb_elem:
 
72
  "thumbnail": thumbnail
73
  })
74
 
75
+ # Optional: move exact matches to the top
76
+ results.sort(key=lambda r: 0 if r["title"].lower().startswith(keyword.lower()) else 1)
77
+
78
  return {"keyword": keyword, "results": results}
79
 
80
  except Exception as e: