create ILLEGAL_CHAR_REPLACEMENT var

This commit is contained in:
Rafael Moraes 2024-08-03 01:45:53 -03:00
parent e3f96d8684
commit c890068eb7

View File

@ -27,8 +27,9 @@ from .models import DownloadQueue, UrlInfo
class Downloader: class Downloader:
ILLEGAL_CHARACTERS_REGEX = r'[\\/:*?"<>|;]' ILLEGAL_CHARS_RE = r'[\\/:*?"<>|;]'
VALID_URL_REGEX = r"/([a-z]{2})/(artist|album|playlist|song|music-video|post)/([^/]*)(?:/([^/?]*))?(?:\?i=)?([0-9a-z]*)?" ILLEGAL_CHAR_REPLACEMENT = "_"
VALID_URL_RE = r"/([a-z]{2})/(artist|album|playlist|song|music-video|post)/([^/]*)(?:/([^/?]*))?(?:\?i=)?([0-9a-z]*)?"
def __init__( def __init__(
self, self,
@ -120,7 +121,7 @@ class Downloader:
def get_url_info(self, url: str) -> UrlInfo: def get_url_info(self, url: str) -> UrlInfo:
url_info = UrlInfo() url_info = UrlInfo()
url_regex_result = re.search( url_regex_result = re.search(
self.VALID_URL_REGEX, self.VALID_URL_RE,
url, url,
) )
url_info.storefront = url_regex_result.group(1) url_info.storefront = url_regex_result.group(1)
@ -284,7 +285,10 @@ class Downloader:
playlist_file_path.parent.mkdir(parents=True, exist_ok=True) playlist_file_path.parent.mkdir(parents=True, exist_ok=True)
with playlist_file_path.open("a", encoding="utf8") as playlist_file: with playlist_file_path.open("a", encoding="utf8") as playlist_file:
playlist_file.write( playlist_file.write(
final_path.relative_to(playlist_file_path.parent, walk_up=True).as_posix() + "\n" final_path.relative_to(
playlist_file_path.parent, walk_up=True
).as_posix()
+ "\n"
) )
@staticmethod @staticmethod
@ -360,11 +364,15 @@ class Downloader:
) )
def get_sanitized_string(self, dirty_string: str, is_folder: bool) -> str: def get_sanitized_string(self, dirty_string: str, is_folder: bool) -> str:
dirty_string = re.sub(self.ILLEGAL_CHARACTERS_REGEX, "_", dirty_string) dirty_string = re.sub(
self.ILLEGAL_CHARS_RE,
self.ILLEGAL_CHAR_REPLACEMENT,
dirty_string,
)
if is_folder: if is_folder:
dirty_string = dirty_string[: self.truncate] dirty_string = dirty_string[: self.truncate]
if dirty_string.endswith("."): if dirty_string.endswith("."):
dirty_string = dirty_string[:-1] + "_" dirty_string = dirty_string[:-1] + self.ILLEGAL_CHAR_REPLACEMENT
else: else:
if self.truncate is not None: if self.truncate is not None:
dirty_string = dirty_string[: self.truncate - 4] dirty_string = dirty_string[: self.truncate - 4]