Compare commits

...

2 Commits

Author SHA1 Message Date
c47c481731
Try more encodings when parsing song file 2025-06-01 12:53:06 +02:00
4ccccf8667
Fix regex according to Regex101 2025-06-01 12:52:41 +02:00
2 changed files with 15 additions and 3 deletions

View File

@ -5,7 +5,7 @@ from pathlib import Path
from karaokatalog.instructions.MoveInstruction import MoveInstruction
from karaokatalog.Song import Song
FORBIDDEN_CHARACTERS = re.compile(r'[/<>:"\\|?*]')
FORBIDDEN_CHARACTERS = re.compile(r'[\/<>:"\\|?*]')
def _get_canonical_song_dir(song: Song, variant: int = 0) -> Path:

View File

@ -26,8 +26,20 @@ def _parse_tag_line(tag_line: str) -> tuple[str, str | None]:
)
def parse_song_txt(song_txt: Path) -> dict[str, Any]:
with song_txt.open(encoding="utf-8", errors="ignore") as f:
def _parse_song_txt_with_encoding(song_txt: Path, encoding: str) -> dict[str, Any]:
with song_txt.open(encoding=encoding) as f:
tags = dict(_parse_tag_line(line) for line in f if line.startswith("#"))
return tags
def parse_song_txt(song_txt: Path) -> dict[str, Any]:
encodings_to_try = ("utf-8", "cp1252")
for encoding_to_try in encodings_to_try:
try:
return _parse_song_txt_with_encoding(song_txt, encoding_to_try)
except UnicodeDecodeError:
pass
raise UnicodeError(f"Could not guess encoding for {song_txt}")