Skip to content

Commit

Permalink
Add wikipedia information for the snippet at the right
Browse files Browse the repository at this point in the history
  • Loading branch information
amogusussy committed Jun 19, 2024
1 parent 5569692 commit c3b517c
Show file tree
Hide file tree
Showing 4 changed files with 127 additions and 9 deletions.
29 changes: 27 additions & 2 deletions src/textResults.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from src import helpers
from urllib.parse import unquote, quote
from urllib.parse import unquote, quote, urlparse, parse_qs
from _config import *
from flask import request, render_template, jsonify, Response
import time
Expand All @@ -8,6 +8,14 @@
from math import isclose # For float comparisons



def local_href(url):
url_parsed = parse_qs(urlparse(url).query)
if "q" not in url_parsed:
return ""
return f"/search?q={url_parsed['q'][0]}&p=0&t=text"


def textResults(query) -> Response:
# get user language settings
settings = helpers.Settings()
Expand Down Expand Up @@ -105,6 +113,23 @@ def textResults(query) -> Response:
for div in soup.find_all("div", {'class': 'nnFGuf'}):
div.decompose()

wiki_known_for = soup.find("div", {'class': 'loJjTe'})
if wiki_known_for is not None:
wiki_known_for = wiki_known_for.get_text()

wiki_info = {}
wiki_info_divs = soup.find_all("div", {"class": "rVusze"})
for info in wiki_info_divs:
spans = info.findChildren("span" , recursive=False)
for a in spans[1].find_all("a"):
# Delete all non-href attributes
a.attrs = {"href": a['href']}
if "sca_esv=" in a.get("href", ""):
# Remove any trackers for google domains
a['href'] = local_href(a.get("href", ""))

wiki_info[spans[0].get_text()] = spans[1].prettify()

# retrieve featured snippet
try:
featured_snip = soup.find("span", {"class": "hgKElc"})
Expand Down Expand Up @@ -225,5 +250,5 @@ def textResults(query) -> Response:
type=type, search_type=search_type, repo_url=REPO, donate_url=DONATE, commit=helpers.latest_commit(),
exported_math_expression=exported_math_expression, API_ENABLED=API_ENABLED,
TORRENTSEARCH_ENABLED=TORRENTSEARCH_ENABLED, lang_data=lang_data,
settings=settings,
settings=settings, wiki_known_for=wiki_known_for, wiki_info=wiki_info
)
32 changes: 30 additions & 2 deletions static/css/style.css
Original file line number Diff line number Diff line change
Expand Up @@ -1263,6 +1263,16 @@ p {
font-size: 22px;
}

.wiki_known_for {
margin: 0px;
font-weight: normal;
}

.snip hr {
margin: 5px 0px;
border-bottom: 1px solid var(--fg);
}

.snip img {
float: right;
max-width: 30%;
Expand All @@ -1271,8 +1281,7 @@ p {
margin-left: 10px;
}

.snip a {
display: block;
.snip .wiki_link {
margin-top: 10px;
text-decoration: none;
color: var(--link);
Expand All @@ -1286,6 +1295,25 @@ p {
text-decoration: underline;
}

.snip .about {
font-size: 18px;
margin-top: 10px;
margin-bottom: 5px;
}

.factpoint {
color: var(--fg);
font-weight: bold;
}

.fact a {
color: var(--link);
text-decoration: none;
}
.fact a:visited {
color: var(--link-visited);
}

.snipp {
padding: 10px;
border-bottom: 1px solid var(--border);
Expand Down
53 changes: 53 additions & 0 deletions temp.html

Large diffs are not rendered by default.

22 changes: 17 additions & 5 deletions templates/results.html
Original file line number Diff line number Diff line change
Expand Up @@ -108,18 +108,30 @@
<p id="info_font" class="snip">{{ calc }}</p>
{% endif %}
{% endif %}
{% if kno_rdesc == "" %}
{% else %}
{% if kno_rdesc != "" %}
<div class="snip">
<h1 class="snip_title">{{ rkno_title }}</h1>
{% if kno_wiki == "" %}
{% else %}
{% if wiki_known_for != "" %}
<h4 class="wiki_known_for">{{ wiki_known_for }}</h4>
{% endif %}
<hr>
{% if kno_wiki != "" %}
<img src="{{ kno_wiki }}"/>
{% endif %}
{% if settings.javascript == "enabled" %}
<img class="kno_wiki" src=""/>
{% endif %}
{{ kno_rdesc }}<a target="_blank" href="{{ rdesc_link }}">{{ rdesc_link }}</a>
{{ kno_rdesc }}
<a target="_blank" class="wiki_link" href="{{ rdesc_link }}">Wikipedia</a>
<h4 class="about">About</h4>
<div class="wiki_info">
{% for info_point in wiki_info %}
<div class="info">
<span class="factpoint">{{ info_point }}</span>
<span class="fact">{{ wiki_info[info_point]|safe }}</span>
</div>
{% endfor %}
</div>
</div>
{% endif %}
{% if snip == "" %}
Expand Down

0 comments on commit c3b517c

Please sign in to comment.