diff --git a/ozgursozluk/api.py b/ozgursozluk/api.py index 699a2b7..83daa8b 100644 --- a/ozgursozluk/api.py +++ b/ozgursozluk/api.py @@ -6,7 +6,7 @@ from bs4 import BeautifulSoup from fake_useragent import UserAgent from ozgursozluk.configs import EKSI_SOZLUK_BASE_URL -from ozgursozluk.models import Entry, EntryTopic, Topic, Author, Gundem, Debe +from ozgursozluk.models import Entry, EntryTopic, Topic, Author, Gundem, Debe, SearchResult class EksiSozluk: @@ -20,13 +20,11 @@ class EksiSozluk: headers = headers or {"User-Agent": UserAgent().random} self.session.headers.update(headers) - def request(self, method: str, path: str = "/", **params) -> BeautifulSoup: + def request(self, method: str, path: str = "/", params: dict = {}) -> BeautifulSoup: """Make a request.""" response = self.session.request( - method, - f"{self.base_url}/{path}", - params=params, + method, f"{self.base_url}{path}", params=params, ) if response.status_code != 200: @@ -49,28 +47,31 @@ class EksiSozluk: int(entry.attrs["data-favorite-count"]), ) - def search_topic(self, query: str) -> Topic: - """Search topic for the given query.""" + def search_topic(self, keywords: str) -> Iterator[SearchResult]: + """Search topic for the given keywords.""" - response = self.request("GET", q=query) - h1 = response.find("h1", id="title") - pager = response.find("div", class_="pager") + payload = { + "SearchForm.Keywords": keywords, + "SearchForm.NiceOnly": True, + "SearchForm.SortOrder": "Count", + } + response = self.request("GET", "/basliklar/ara", payload) + topic_list = response.find("ul", class_="topic-list").find_all("a", href=True) - return Topic( - int(h1.attrs["data-id"]), - h1.attrs["data-title"], - h1.find("a")["href"][1:], - self.entrys(response), - int(pager.attrs["data-pagecount"]) if pager else 0, - ) + for topic in topic_list: + yield SearchResult( + topic.contents[0], + topic["href"].split("?")[0], + None if len(topic.contents) < 2 else topic.contents[1].text, + ) def get_topic(self, path: str, page: int = 1, a: Optional[str] = None) -> Topic: """Get topic for the given path.""" if a is None: - response = self.request("GET", f"/{path}", p=page) + response = self.request("GET", f"/{path}", {"p": page}) else: - response = self.request("GET", f"/{path}", p=page, a=a) + response = self.request("GET", f"/{path}", {"p": page, "a": a}) h1 = response.find("h1", id="title") pager = response.find("div", class_="pager") @@ -127,7 +128,7 @@ class EksiSozluk: https://eksisozluk.com/basliklar/gundem """ - response = self.request("GET", "/basliklar/gundem", p=page) + response = self.request("GET", "/basliklar/gundem", {"p": page}) topic_list = response.find("ul", class_="topic-list").find_all("a", href=True) for topic in topic_list: diff --git a/ozgursozluk/models.py b/ozgursozluk/models.py index 728c8ab..50c9d1c 100644 --- a/ozgursozluk/models.py +++ b/ozgursozluk/models.py @@ -53,3 +53,9 @@ class Gundem: class Debe: id: int title: str + +@dataclass +class SearchResult: + title: str + path: str + entry_count: Union[str, None] = None diff --git a/ozgursozluk/templates/search.html b/ozgursozluk/templates/search.html new file mode 100644 index 0000000..9ebede0 --- /dev/null +++ b/ozgursozluk/templates/search.html @@ -0,0 +1,27 @@ +{% extends "base.html" %} +{% block title %}search - özgürsözlük{% endblock %} +{% block meta %} + + +{% endblock %} +{% block main %} + +
search result: {{ q }}
") -def search(q: str): +@ozgursozluk.app.route("/search") +def search(): """Search route.""" - return render_template("topic.html", topic=es.search_topic(q), p=1, a=None) + q = request.args.get("q", default=None, type=str) + + if q is None or not bool(len(q)): + return render_template("404.html"), 404 + + return render_template("search.html", search_result=es.search_topic(q), q=q) @ozgursozluk.app.route("/random") def random(): - return redirect(url_for("entry", id=randint(1, 500_000_000))) + return redirect(url_for("entry", id=randint(1, 300_000_000))) @ozgursozluk.app.route("/donate")