Skip to content

Commit 71f52fa

Browse files
committed
feat: enhance search functionality
- Combined `if not_found` and `if not_found.text == "Nothing Found"` into a single condition. - Added a check to ensure `results` is a list before iterating. - Appended a book emoji (📖) before each title in the search results. - Added "🔍 Search again" and "❌ Exit" options to the `titles` and `urls` lists. - Handled "Search again" selection by calling `search_book()` recursively. - Handled "Exit" selection by exiting the function gracefully. - Added the ability to navigate to the next page and previous page. - Added "➡️ Next page" and "⬅️ Previous page" options to the `titles` and `urls` lists. - Handled "Next page" and "Previous page" selections to navigate accordingly. - Implemented caching of previously visited pages to avoid re-fetching them.
1 parent 261f6e8 commit 71f52fa

File tree

1 file changed

+55
-11
lines changed

1 file changed

+55
-11
lines changed

tokysnatcher/search.py

+55-11
Original file line numberDiff line numberDiff line change
@@ -5,27 +5,71 @@
55
from pick import pick
66

77

8-
def search_book():
9-
SEARCH_URL = "https://tokybook.com/?s="
8+
def fetch_results(query: str, page: int = 1):
9+
SEARCH_URL = f"https://tokybook.com/page/{page}/?s="
10+
html = get(SEARCH_URL + quote(query))
11+
soup = Soup(html)
12+
return soup
13+
14+
15+
def search_book(query: str = None, page: int = 1, previous_pages: dict = None):
16+
if previous_pages is None:
17+
previous_pages = {}
18+
19+
if query is None:
20+
query = input("Enter search query: ")
21+
1022
spinner = Halo(text="Searching...", spinner="dots")
11-
query = input("Search query: ")
1223
spinner.start()
1324

14-
html = get(SEARCH_URL + quote(query))
15-
soup = Soup(html)
25+
if page in previous_pages:
26+
soup = previous_pages[page]
27+
else:
28+
soup = fetch_results(query, page)
29+
previous_pages[page] = soup
1630

1731
not_found = soup.find("h1", {"class": "entry-title"})
1832

19-
if not_found:
20-
if not_found.text == "Nothing Found":
21-
print("No results found!")
22-
return search_book()
33+
if not_found and not_found.text == "Nothing Found":
34+
print("No results found!")
35+
spinner.stop()
36+
return search_book()
2337

2438
results = soup.find("h2", {"class": "entry-title"})
2539

26-
titles = [x.text for x in results]
40+
if not isinstance(results, list):
41+
print("No results found. Exiting...")
42+
spinner.stop()
43+
return None
44+
45+
titles = [f"📖 {x.text}" for x in results]
2746
urls = [x.find("a").attrs.get("href") for x in results]
2847

48+
next_page = soup.find("a", {"class": "next page-numbers"})
49+
if next_page:
50+
titles.append("➡️ Next page")
51+
urls.append("next")
52+
53+
if page > 1:
54+
titles.append("⬅️ Previous page")
55+
urls.append("previous")
56+
2957
spinner.stop()
30-
_, idx = pick(titles, "Search results:")
58+
titles.append("🔍 Search again")
59+
urls.append("search_again")
60+
titles.append("❌ Exit")
61+
urls.append("exit")
62+
63+
_, idx = pick(titles, "Search results:", indicator="*")
64+
65+
if urls[idx] == "next":
66+
return search_book(query, page + 1, previous_pages)
67+
elif urls[idx] == "previous":
68+
return search_book(query, page - 1, previous_pages)
69+
elif urls[idx] == "search_again":
70+
return search_book()
71+
elif urls[idx] == "exit":
72+
print("Exiting...")
73+
return None
74+
3175
return urls[idx]

0 commit comments

Comments
 (0)