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] [project]
name = "structables" name = "structables"
version = "0.2.1" version = "0.2.2"
authors = [ authors = [
{ name="Private.coffee Team", email="support@private.coffee" }, { 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 traceback import print_exc
from urllib.parse import urlparse from urllib.parse import urlparse
from argparse import ArgumentParser from argparse import ArgumentParser
from typing import List
from werkzeug.exceptions import BadRequest, abort, InternalServerError, NotFound from werkzeug.exceptions import BadRequest, abort, InternalServerError, NotFound
from bs4 import BeautifulSoup from bs4 import BeautifulSoup
@ -59,6 +60,26 @@ invidious = None
unsafe = False 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( def projects_search(
query="*", query="*",
category="", category="",
@ -402,10 +423,19 @@ def project_list(head, sort="", per_page=20):
category = parts[1] category = parts[1]
channel = "" if parts[2] == "projects" else parts[2] channel = "" if parts[2] == "projects" else parts[2]
channel_names = unslugify(channel)
for channel_name in channel_names:
project_ibles, total = projects_search( project_ibles, total = projects_search(
category=category, channel=channel, per_page=per_page, page=page category=category,
channel=channel_name,
per_page=per_page,
page=page,
) )
if project_ibles:
break
elif "search" in path.split("/"): elif "search" in path.split("/"):
ibles = [] ibles = []
query = ( query = (
@ -1275,6 +1305,7 @@ def main():
app.run(port=args.port, host=args.listen_host, debug=debugmode) app.run(port=args.port, host=args.listen_host, debug=debugmode)
if __name__ == "__main__": if __name__ == "__main__":
main() main()