Move search stuff to own file
This commit is contained in:
@ -24,9 +24,7 @@ Exceptions:
|
|||||||
from typing import Iterable
|
from typing import Iterable
|
||||||
|
|
||||||
from discord_webhook import DiscordWebhook
|
from discord_webhook import DiscordWebhook
|
||||||
from reader import (
|
from reader import Entry
|
||||||
Entry,
|
|
||||||
)
|
|
||||||
from requests import Response
|
from requests import Response
|
||||||
|
|
||||||
from discord_rss_bot.settings import reader
|
from discord_rss_bot.settings import reader
|
||||||
|
@ -37,11 +37,12 @@ from fastapi import FastAPI, Form, HTTPException, Request
|
|||||||
from fastapi.responses import FileResponse, HTMLResponse
|
from fastapi.responses import FileResponse, HTMLResponse
|
||||||
from fastapi.staticfiles import StaticFiles
|
from fastapi.staticfiles import StaticFiles
|
||||||
from fastapi.templating import Jinja2Templates
|
from fastapi.templating import Jinja2Templates
|
||||||
from reader import Entry, EntryCounts, EntrySearchResult, Feed, FeedCounts, HighlightedString
|
from reader import Entry, EntryCounts, Feed, FeedCounts
|
||||||
from starlette.templating import _TemplateResponse
|
from starlette.templating import _TemplateResponse
|
||||||
from tomlkit.toml_document import TOMLDocument
|
from tomlkit.toml_document import TOMLDocument
|
||||||
|
|
||||||
from discord_rss_bot.feeds import send_to_discord
|
from discord_rss_bot.feeds import send_to_discord
|
||||||
|
from discord_rss_bot.search import create_html_for_search_results
|
||||||
from discord_rss_bot.settings import read_settings_file, reader
|
from discord_rss_bot.settings import read_settings_file, reader
|
||||||
|
|
||||||
app: FastAPI = FastAPI()
|
app: FastAPI = FastAPI()
|
||||||
@ -251,42 +252,6 @@ async def search(request: Request, query: str) -> _TemplateResponse:
|
|||||||
search_results = reader.search_entries(query)
|
search_results = reader.search_entries(query)
|
||||||
search_amount = reader.search_entry_counts(query)
|
search_amount = reader.search_entry_counts(query)
|
||||||
|
|
||||||
def add_span_with_slice(highlighted_string: HighlightedString):
|
|
||||||
"""Add a span with the highlighted string."""
|
|
||||||
|
|
||||||
for txt_slice in highlighted_string.highlights:
|
|
||||||
first = f"{highlighted_string.value[: txt_slice.start]}"
|
|
||||||
second = f"<span class='bg-warning'>{highlighted_string.value[txt_slice.start: txt_slice.stop]}</span>"
|
|
||||||
third = f"{highlighted_string.value[txt_slice.stop:]}"
|
|
||||||
return f"{first}{second}{third}"
|
|
||||||
|
|
||||||
def create_html_for_search_results(search_results: Iterable[EntrySearchResult]) -> str:
|
|
||||||
"""Create HTML for the search results.
|
|
||||||
|
|
||||||
Args:
|
|
||||||
search_results: The search results.
|
|
||||||
|
|
||||||
Returns:
|
|
||||||
str: The HTML.
|
|
||||||
"""
|
|
||||||
html = ""
|
|
||||||
for result in search_results:
|
|
||||||
if ".summary" in result.content:
|
|
||||||
result_summary = add_span_with_slice(result.content[".summary"])
|
|
||||||
feed = reader.get_feed(result.feed_url)
|
|
||||||
feed_url = encode_url(feed.url)
|
|
||||||
|
|
||||||
html += f"""
|
|
||||||
<a class="text-muted text-decoration-none" href="/feed?feed_url={feed_url}">
|
|
||||||
<h2>{result.metadata[".title"]}</h2>
|
|
||||||
</a>
|
|
||||||
<blockquote>
|
|
||||||
{result_summary}
|
|
||||||
</blockquote>
|
|
||||||
<hr>
|
|
||||||
"""
|
|
||||||
return html
|
|
||||||
|
|
||||||
search_html = create_html_for_search_results(search_results)
|
search_html = create_html_for_search_results(search_results)
|
||||||
return templates.TemplateResponse(
|
return templates.TemplateResponse(
|
||||||
"search.html", {"request": request, "search_html": search_html, "query": query, "search_amount": search_amount}
|
"search.html", {"request": request, "search_html": search_html, "query": query, "search_amount": search_amount}
|
||||||
|
49
discord_rss_bot/search.py
Normal file
49
discord_rss_bot/search.py
Normal file
@ -0,0 +1,49 @@
|
|||||||
|
import urllib.parse
|
||||||
|
from typing import Iterable
|
||||||
|
|
||||||
|
from reader import EntrySearchResult, HighlightedString
|
||||||
|
|
||||||
|
from discord_rss_bot.settings import reader
|
||||||
|
|
||||||
|
|
||||||
|
def create_html_for_search_results(search_results: Iterable[EntrySearchResult]) -> str:
|
||||||
|
"""Create HTML for the search results.
|
||||||
|
|
||||||
|
Args:
|
||||||
|
search_results: The search results.
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
str: The HTML.
|
||||||
|
"""
|
||||||
|
html = ""
|
||||||
|
for result in search_results:
|
||||||
|
if ".summary" in result.content:
|
||||||
|
result_summary = add_span_with_slice(result.content[".summary"])
|
||||||
|
feed = reader.get_feed(result.feed_url)
|
||||||
|
feed_url = urllib.parse.quote(feed.url)
|
||||||
|
|
||||||
|
html += f"""
|
||||||
|
<a class="text-muted text-decoration-none" href="/feed?feed_url={feed_url}">
|
||||||
|
<h2>{result.metadata[".title"]}</h2>
|
||||||
|
</a>
|
||||||
|
{result_summary}
|
||||||
|
<hr>
|
||||||
|
"""
|
||||||
|
return html
|
||||||
|
|
||||||
|
|
||||||
|
def add_span_with_slice(highlighted_string: HighlightedString) -> str:
|
||||||
|
"""Add span tags to the string to highlight the search results.
|
||||||
|
|
||||||
|
Args:
|
||||||
|
highlighted_string: The highlighted string.
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
str: The string with added <span> tags.
|
||||||
|
"""
|
||||||
|
|
||||||
|
for txt_slice in highlighted_string.highlights:
|
||||||
|
before_span = f"{highlighted_string.value[: txt_slice.start]}"
|
||||||
|
span_part = f"<span class='bg-warning'>{highlighted_string.value[txt_slice.start: txt_slice.stop]}</span>"
|
||||||
|
after_span = f"{highlighted_string.value[txt_slice.stop:]}"
|
||||||
|
return f"{before_span}{span_part}{after_span}"
|
Reference in New Issue
Block a user