From c60da5ec3acb31525fae13808a50be1700f367db Mon Sep 17 00:00:00 2001 From: Kumi Date: Sat, 25 May 2024 15:14:27 +0200 Subject: [PATCH] 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. --- pyproject.toml | 2 +- src/structables/main.py | 41 ++++++++++++++++++++++++++++++++++++----- 2 files changed, 37 insertions(+), 6 deletions(-) diff --git a/pyproject.toml b/pyproject.toml index cc2667e..b949c40 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -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" }, ] diff --git a/src/structables/main.py b/src/structables/main.py index 5c62f39..5262487 100644 --- a/src/structables/main.py +++ b/src/structables/main.py @@ -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() \ No newline at end of file +update_data()