feat: Enhance project channel search flexibility

Introduced `unslugify` function to expand search capabilities by generating possible original titles from slugs, allowing for a broader search when matching project channels. This function is utilized in the `project_list` method to attempt queries with different variants of a channel's name, enhancing the likelihood of matching user-desired content. The update optimizes search functionality and user experience by accommodating varied naming conventions.

Version incremented to 0.2.2 in project metadata to reflect new feature addition and improvements.
This commit is contained in:
Kumi 2024-05-25 15:14:27 +02:00
parent c593904a19
commit c60da5ec3a
Signed by: kumi
GPG key ID: ECBCC9082395383F
2 changed files with 37 additions and 6 deletions

View file

@ -4,7 +4,7 @@ build-backend = "hatchling.build"
[project]
name = "structables"
version = "0.2.1"
version = "0.2.2"
authors = [
{ name="Private.coffee Team", email="support@private.coffee" },
]

View file

@ -14,6 +14,7 @@ from urllib.error import HTTPError
from traceback import print_exc
from urllib.parse import urlparse
from argparse import ArgumentParser
from typing import List
from werkzeug.exceptions import BadRequest, abort, InternalServerError, NotFound
from bs4 import BeautifulSoup
@ -59,6 +60,26 @@ invidious = None
unsafe = False
def unslugify(slug: str) -> List[str]:
"""Return a list of possible original titles for a slug.
Args:
slug (str): The slug to unslugify.
Returns:
List[str]: A list of possible original titles for the slug.
"""
results = []
results.append(slug.replace("-", " ").title())
if "and" in slug:
results.append(results[0].replace("And", "&").title())
return results
def projects_search(
query="*",
category="",
@ -402,9 +423,18 @@ def project_list(head, sort="", per_page=20):
category = parts[1]
channel = "" if parts[2] == "projects" else parts[2]
project_ibles, total = projects_search(
category=category, channel=channel, per_page=per_page, page=page
)
channel_names = unslugify(channel)
for channel_name in channel_names:
project_ibles, total = projects_search(
category=category,
channel=channel_name,
per_page=per_page,
page=page,
)
if project_ibles:
break
elif "search" in path.split("/"):
ibles = []
@ -492,7 +522,7 @@ def route_sitemap(path=""):
for li in main.select("ul.sitemap-listing li"):
channel = li.a.text
channel_link = li.a["href"]
if channel_link.startswith("https://"):
channel_link = f'/{"/".join(channel_link.split("/")[3:])}'
@ -1275,8 +1305,9 @@ def main():
app.run(port=args.port, host=args.listen_host, debug=debugmode)
if __name__ == "__main__":
main()
# Initialize data when the server starts
update_data()
update_data()