2023-07-27 18:44:24 +00:00
|
|
|
from typing import Tuple
|
|
|
|
|
2023-04-18 20:39:19 +00:00
|
|
|
from transliterate.exceptions import LanguageDetectionError
|
|
|
|
from transliterate import translit
|
2023-05-10 14:39:44 +00:00
|
|
|
from pathvalidate import sanitize_filename
|
|
|
|
|
2023-04-18 20:39:19 +00:00
|
|
|
|
2023-07-27 18:44:24 +00:00
|
|
|
COMMON_TITLE_APPENDIX_LIST: Tuple[str, ...] = (
|
|
|
|
"(official video)",
|
|
|
|
)
|
|
|
|
|
|
|
|
|
2023-01-31 12:18:52 +00:00
|
|
|
def unify(string: str) -> str:
|
|
|
|
"""
|
2023-04-18 20:39:19 +00:00
|
|
|
returns a unified str, to make comparisons easy.
|
|
|
|
a unified string has the following attributes:
|
2023-01-31 12:18:52 +00:00
|
|
|
- is lowercase
|
|
|
|
"""
|
2023-04-18 20:39:19 +00:00
|
|
|
|
|
|
|
try:
|
|
|
|
string = translit(string, reversed=True)
|
|
|
|
except LanguageDetectionError:
|
|
|
|
pass
|
|
|
|
|
2023-01-31 12:18:52 +00:00
|
|
|
return string.lower()
|
2023-04-03 09:17:55 +00:00
|
|
|
|
2023-04-18 20:39:19 +00:00
|
|
|
|
2023-04-03 09:17:55 +00:00
|
|
|
def fit_to_file_system(string: str) -> str:
|
|
|
|
string = string.strip()
|
2023-04-18 20:39:19 +00:00
|
|
|
|
2023-04-03 09:17:55 +00:00
|
|
|
while string[0] == ".":
|
|
|
|
if len(string) == 0:
|
|
|
|
return string
|
2023-04-18 20:39:19 +00:00
|
|
|
|
2023-04-03 09:17:55 +00:00
|
|
|
string = string[1:]
|
2023-04-18 20:39:19 +00:00
|
|
|
|
2023-08-01 10:07:09 +00:00
|
|
|
string = string.replace("/", "_").replace("\\", "_")
|
2023-04-18 20:39:19 +00:00
|
|
|
|
2023-05-10 14:39:44 +00:00
|
|
|
string = sanitize_filename(string)
|
|
|
|
|
2023-04-03 09:17:55 +00:00
|
|
|
return string
|
2023-07-27 18:44:24 +00:00
|
|
|
|
|
|
|
|
|
|
|
def clean_song_title(raw_song_title: str, artist_name: str) -> str:
|
|
|
|
"""
|
|
|
|
This function cleans common naming "conventions" for non clean song titles, like the title of youtube videos
|
|
|
|
|
|
|
|
cleans:
|
|
|
|
|
|
|
|
- `artist - song` -> `song`
|
|
|
|
- `song (Official Video)` -> `song`
|
|
|
|
- ` song` -> `song`
|
|
|
|
- `song (prod. some producer)`
|
|
|
|
"""
|
|
|
|
raw_song_title = raw_song_title.strip()
|
|
|
|
artist_name = artist_name.strip()
|
|
|
|
|
|
|
|
# Clean official Video appendix
|
|
|
|
for dirty_appendix in COMMON_TITLE_APPENDIX_LIST:
|
|
|
|
if raw_song_title.lower().endswith(dirty_appendix):
|
|
|
|
raw_song_title = raw_song_title[:-len(dirty_appendix)].strip()
|
|
|
|
|
|
|
|
# Remove artist from the start of the title
|
|
|
|
if raw_song_title.lower().startswith(artist_name.lower()):
|
|
|
|
raw_song_title = raw_song_title[len(artist_name):].strip()
|
|
|
|
|
|
|
|
if raw_song_title.startswith("-"):
|
|
|
|
raw_song_title = raw_song_title[1:].strip()
|
|
|
|
|
|
|
|
return raw_song_title.strip()
|