feat: use clean_html yt-dlp function over custom remove_html function

This commit is contained in:
Benex254
2024-08-11 20:46:54 +03:00
parent 622427b748
commit c80a8235e1
3 changed files with 9 additions and 77 deletions

View File

@@ -1,79 +1,18 @@
import logging
import os
import re
from functools import lru_cache
from typing import TYPE_CHECKING
from thefuzz import fuzz
from fastanime.libs.anilist.types import AnilistBaseMediaDataSchema
from .data import anime_normalizer
if TYPE_CHECKING:
from ..libs.anilist.types import AnilistBaseMediaDataSchema
logger = logging.getLogger(__name__)
@lru_cache()
def remove_html_tags(text: str):
clean = re.compile("<.*?>")
return re.sub(clean, "", text)
@lru_cache()
def sanitize_filename(filename: str):
"""
Sanitize a string to be safe for use as a file name.
:param filename: The original filename string.
:return: A sanitized filename string.
"""
# List of characters not allowed in filenames on various operating systems
invalid_chars = r'[<>:"/\\|?*\0]'
reserved_names = {
"CON",
"PRN",
"AUX",
"NUL",
"COM1",
"COM2",
"COM3",
"COM4",
"COM5",
"COM6",
"COM7",
"COM8",
"COM9",
"LPT1",
"LPT2",
"LPT3",
"LPT4",
"LPT5",
"LPT6",
"LPT7",
"LPT8",
"LPT9",
}
# Replace invalid characters with an underscore
sanitized = re.sub(invalid_chars, " ", filename)
# Remove leading and trailing whitespace
sanitized = sanitized.strip()
# Check for reserved filenames
name, ext = os.path.splitext(sanitized)
if name.upper() in reserved_names:
name += "_file"
sanitized = name + ext
# Ensure the filename is not empty
if not sanitized:
sanitized = "default_filename"
return sanitized
def anime_title_percentage_match(
possible_user_requested_anime_title: str, anime: AnilistBaseMediaDataSchema
possible_user_requested_anime_title: str, anime: "AnilistBaseMediaDataSchema"
) -> float:
"""Returns the percentage match between the possible title and user title
@@ -97,10 +36,3 @@ def anime_title_percentage_match(
)
logger.info(f"{locals()}")
return percentage_ratio
if __name__ == "__main__":
# Example usage
unsafe_filename = "CON:example?file*name.txt"
safe_filename = sanitize_filename(unsafe_filename)
print(safe_filename) # Output: 'CON_example_file_name.txt'

View File

@@ -917,9 +917,9 @@ def media_actions_menu(
"""
from rich.console import Console
from rich.prompt import Confirm
from yt_dlp.utils import clean_html
from ...Utility import anilist_data_helper
from ...Utility.utils import remove_html_tags
from ..utils.print_img import print_img
clear()
@@ -970,7 +970,7 @@ def media_actions_menu(
)
console.print(
"[bold underline cyan]Description\n[/]",
remove_html_tags(str(selected_anime_anilist["description"])),
clean_html(str(selected_anime_anilist["description"])),
)
if Confirm.ask("Enter to continue...", default=True):
media_actions_menu(config, fastanime_runtime_state)

View File

@@ -7,11 +7,11 @@ import textwrap
from threading import Thread
import requests
from yt_dlp.utils import clean_html
from ...constants import APP_CACHE_DIR
from ...libs.anilist.types import AnilistBaseMediaDataSchema
from ...Utility import anilist_data_helper
from ...Utility.utils import remove_html_tags
from ..utils.utils import get_true_fg
logger = logging.getLogger(__name__)
@@ -201,7 +201,7 @@ def write_search_results(
template = textwrap.dedent(template)
template = f"""
{template}
{textwrap.fill(remove_html_tags(
{textwrap.fill(clean_html(
str(anime['description'])), width=45)}
"""
future_to_task[executor.submit(save_info_from_str, template, title)] = title