Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit ea10354

Browse files
committedJan 20, 2025··
translations: use a more distinctive separator
I found that the translator would sometimes replace the pipe character with another symbol (maybe it got confused thinking the character is part of the text?). Added spaces around the pipe to make it more clear that it's definitely the separator.
1 parent 4ff9b7b commit ea10354

File tree

2 files changed

+14
-11
lines changed

2 files changed

+14
-11
lines changed
 

‎beetsplug/lyrics.py

+5-2
Original file line numberDiff line numberDiff line change
@@ -744,6 +744,7 @@ def scrape(cls, html: str) -> str | None:
744744
class Translator(RequestHandler):
745745
TRANSLATE_URL = "https://api.cognitive.microsofttranslator.com/translate"
746746
LINE_PARTS_RE = re.compile(r"^(\[\d\d:\d\d.\d\d\]|) *(.*)$")
747+
SEPARATOR = " | "
747748
remove_translations = partial(re.compile(r" / [^\n]+").sub, "")
748749

749750
_log: Logger
@@ -773,14 +774,16 @@ def get_translations(self, texts: Iterable[str]) -> list[tuple[str, str]]:
773774
map the translations back to the original texts.
774775
"""
775776
unique_texts = list(dict.fromkeys(texts))
777+
text = self.SEPARATOR.join(unique_texts)
776778
data: list[TranslatorAPI.Response] = self.post_json(
777779
self.TRANSLATE_URL,
778780
headers={"Ocp-Apim-Subscription-Key": self.api_key},
779-
json=[{"text": "|".join(unique_texts)}],
781+
json=[{"text": text}],
780782
params={"api-version": "3.0", "to": self.to_language},
781783
)
782784

783-
translations = data[0]["translations"][0]["text"].split("|")
785+
translated_text = data[0]["translations"][0]["text"]
786+
translations = translated_text.split(self.SEPARATOR)
784787
trans_by_text = dict(zip(unique_texts, translations))
785788
return list(zip(texts, (trans_by_text.get(t, "") for t in texts)))
786789

‎test/plugins/test_lyrics.py

+9-9
Original file line numberDiff line numberDiff line change
@@ -548,23 +548,23 @@ def callback(request, _):
548548
if b"Refrain" in request.body:
549549
translations = (
550550
""
551-
"|[Refrain : Doja Cat]"
552-
"|Difficile pour moi de te laisser partir (Te laisser partir, te laisser partir)" # noqa: E501
553-
"|Mon corps ne me laissait pas le cacher (Cachez-le)"
554-
"|Quoi qu’il arrive, je ne plierais pas (Ne plierait pas, ne plierais pas)" # noqa: E501
555-
"|Chevauchant à travers le tonnerre, la foudre"
551+
" | [Refrain : Doja Cat]"
552+
" | Difficile pour moi de te laisser partir (Te laisser partir, te laisser partir)" # noqa: E501
553+
" | Mon corps ne me laissait pas le cacher (Cachez-le)"
554+
" | Quoi qu’il arrive, je ne plierais pas (Ne plierait pas, ne plierais pas)" # noqa: E501
555+
" | Chevauchant à travers le tonnerre, la foudre"
556556
)
557557
elif b"00:00.00" in request.body:
558558
translations = (
559559
""
560-
"|[00:00.00] Quelques paroles synchronisées"
561-
"|[00:01.00] Quelques paroles plus synchronisées"
560+
" | [00:00.00] Quelques paroles synchronisées"
561+
" | [00:01.00] Quelques paroles plus synchronisées"
562562
)
563563
else:
564564
translations = (
565565
""
566-
"|Quelques paroles synchronisées"
567-
"|Quelques paroles plus synchronisées"
566+
" | Quelques paroles synchronisées"
567+
" | Quelques paroles plus synchronisées"
568568
)
569569

570570
return [

0 commit comments

Comments
 (0)
Please sign in to comment.