geminispace.info

gemini search engine
git clone https://git.clttr.info/geminispace.info.git
Log (Feed) | Files | Refs (Tags) | README | LICENSE

commit a8a1abd487ad5dd0587d680a136dda0a8b1fd7a7
parent 0a0d4669dea4557b24f35fdba4177203e782018a
Author: Natalie Pendragon <natpen@natpen.net>
Date:   Wed, 15 Jul 2020 08:20:03 -0400

[serve] Get backlinks from db instead of index

Diffstat:
Mserve/models.py | 23++++++++++++-----------
1 file changed, 12 insertions(+), 11 deletions(-)

diff --git a/serve/models.py b/serve/models.py @@ -56,7 +56,7 @@ class GUS(): "prompt" : result["prompt"] if "prompt" in result else "", "highlights" : self.gemini_highlighter.highlight_hit(result, "content", top=1) if "content" in result and result["content_type"] in ["text/plain", "text/gemini", "text/markdown"] else "", "link_text" : GUS._get_link_text(result), - "backlinks" : result["backlinks"].split(" ") if "backlinks" in result and result["backlinks"] != "" else [], + "backlinks" : list(set(result["backlinks"].split(" "))) if "backlinks" in result and result["backlinks"] != "" else [], } for result in results], ) @@ -65,17 +65,18 @@ class GUS(): resource = GeminiResource(url) if not resource.is_valid: return [] - backlinks = [] - result = self.searcher.document(url_id=resource.indexable_url.rstrip("/")) - if result and "backlinks" in result and result["backlinks"] != "": - backlinks.extend(result["backlinks"].split(" ")) - - result = self.searcher.document(url_id=resource.indexable_url.rstrip("/") + "/") - if result and "backlinks" in result and result["backlinks"] != "": - backlinks.extend(result["backlinks"].split(" ")) - - return backlinks + u = resource.indexable_url.rstrip("/") + from_page = Page.alias() + to_page = Page.alias() + backlinks = (Link + .select(from_page) + .join(from_page, on=(from_page.id == Link.from_page_id)) + .join(to_page, on=(to_page.id == Link.to_page_id)) + .where(to_page.url << [u, f"{u}/"]) + .dicts()) + backlink_urls = [b["url"] for b in backlinks] + return list(set(backlink_urls)) def _get_link_text(result):