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 bc78084

Browse files
committedJan 19, 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 e47fe7d commit bc78084

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
@@ -733,6 +733,7 @@ def scrape(cls, html: str) -> str | None:
733733
class Translator(RequestHandler):
734734
TRANSLATE_URL = "https://api.cognitive.microsofttranslator.com/translate"
735735
LINE_PARTS_RE = re.compile(r"^(\[\d\d:\d\d.\d\d\]|) *(.*)$")
736+
SEPARATOR = " | "
736737
remove_translations = partial(re.compile(r" / [^\n]+").sub, "")
737738

738739
_log: beets.logging.Logger
@@ -762,14 +763,16 @@ def get_translations(self, texts: Iterable[str]) -> list[tuple[str, str]]:
762763
map the translations back to the original texts.
763764
"""
764765
unique_texts = list(dict.fromkeys(texts))
766+
text = self.SEPARATOR.join(unique_texts)
765767
data: list[TranslatorAPI.Response] = self.post_json(
766768
self.TRANSLATE_URL,
767769
headers={"Ocp-Apim-Subscription-Key": self.api_key},
768-
json=[{"text": "|".join(unique_texts)}],
770+
json=[{"text": text}],
769771
params={"api-version": "3.0", "to": self.to_language},
770772
)
771773

772-
translations = data[0]["translations"][0]["text"].split("|")
774+
translated_text = data[0]["translations"][0]["text"]
775+
translations = translated_text.split(self.SEPARATOR)
773776
trans_by_text = dict(zip(unique_texts, translations))
774777
return list(zip(texts, (trans_by_text.get(t, "") for t in texts)))
775778

‎test/plugins/test_lyrics.py

+9-9
Original file line numberDiff line numberDiff line change
@@ -531,23 +531,23 @@ def callback(request, _):
531531
if b"Refrain" in request.body:
532532
translations = (
533533
""
534-
"|[Refrain : Doja Cat]"
535-
"|Difficile pour moi de te laisser partir (Te laisser partir, te laisser partir)" # noqa: E501
536-
"|Mon corps ne me laissait pas le cacher (Cachez-le)"
537-
"|Quoi qu’il arrive, je ne plierais pas (Ne plierait pas, ne plierais pas)" # noqa: E501
538-
"|Chevauchant à travers le tonnerre, la foudre"
534+
" | [Refrain : Doja Cat]"
535+
" | Difficile pour moi de te laisser partir (Te laisser partir, te laisser partir)" # noqa: E501
536+
" | Mon corps ne me laissait pas le cacher (Cachez-le)"
537+
" | Quoi qu’il arrive, je ne plierais pas (Ne plierait pas, ne plierais pas)" # noqa: E501
538+
" | Chevauchant à travers le tonnerre, la foudre"
539539
)
540540
elif b"00:00.00" in request.body:
541541
translations = (
542542
""
543-
"|[00:00.00] Quelques paroles synchronisées"
544-
"|[00:01.00] Quelques paroles plus synchronisées"
543+
" | [00:00.00] Quelques paroles synchronisées"
544+
" | [00:01.00] Quelques paroles plus synchronisées"
545545
)
546546
else:
547547
translations = (
548548
""
549-
"|Quelques paroles synchronisées"
550-
"|Quelques paroles plus synchronisées"
549+
" | Quelques paroles synchronisées"
550+
" | Quelques paroles plus synchronisées"
551551
)
552552

553553
return [

0 commit comments

Comments
 (0)
Please sign in to comment.