Add SearchResult

pull/12/head
beucismis 11 months ago
parent 0e681fb624
commit 834004b408
  1. 41
      ozgursozluk/api.py
  2. 6
      ozgursozluk/models.py
  3. 27
      ozgursozluk/templates/search.html
  4. 17
      ozgursozluk/views.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:

@ -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

@ -0,0 +1,27 @@
{% extends "base.html" %}
{% block title %}search - özgürsözlük{% endblock %}
{% block meta %}
<meta name="title" content="search - özgürsözlük">
<meta name="description" content="{{ q }}">
{% endblock %}
{% block main %}
<nav>{% include "navigation.html" %}</nav>
<div class="topic">
<div class="info">
<div><p>search result: {{ q }}</p></div>
<div></div>
</div>
{% for topic in search_result %}
<a href="{{ url_for('topic', path=topic.path) }}" class="entry">
<div style="display: flex; justify-content: space-between;">
<div>{{ topic.title }}</div>
<div>
{% if topic.entry_count %}
<small style="opacity: 0.5;">{{ topic.entry_count }}</small>
{% endif %}
</div>
</div>
</a>
{% endfor %}
</div>
{% endblock %}

@ -20,7 +20,7 @@ def global_template_variables():
last_commit=last_commit(),
contributors=contributors(),
version=ozgursozluk.__version__,
source=ozgursozluk.__source__,
source_code=ozgursozluk.__source_code__,
description=ozgursozluk.__description__,
)
@ -36,7 +36,7 @@ def index():
return redirect(url_for("search", q=q))
if request.method == "POST":
return redirect(url_for("search", q=request.form["q"]))
return redirect(url_for("search", q=request.form["q"] or None))
gundem = es.get_gundem(p)
@ -74,16 +74,21 @@ def debe():
return render_template("debe.html", debe=es.get_debe())
@ozgursozluk.app.route("/search/<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")

Loading…
Cancel
Save