Skip to content

Commit b713d72

Browse files
committed
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 43032f7 commit b713d72

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
@@ -554,23 +554,23 @@ def callback(request, _):
554554
if b"Refrain" in request.body:
555555
translations = (
556556
""
557-
"|[Refrain : Doja Cat]"
558-
"|Difficile pour moi de te laisser partir (Te laisser partir, te laisser partir)" # noqa: E501
559-
"|Mon corps ne me laissait pas le cacher (Cachez-le)"
560-
"|Quoi qu’il arrive, je ne plierais pas (Ne plierait pas, ne plierais pas)" # noqa: E501
561-
"|Chevauchant à travers le tonnerre, la foudre"
557+
" | [Refrain : Doja Cat]"
558+
" | Difficile pour moi de te laisser partir (Te laisser partir, te laisser partir)" # noqa: E501
559+
" | Mon corps ne me laissait pas le cacher (Cachez-le)"
560+
" | Quoi qu’il arrive, je ne plierais pas (Ne plierait pas, ne plierais pas)" # noqa: E501
561+
" | Chevauchant à travers le tonnerre, la foudre"
562562
)
563563
elif b"00:00.00" in request.body:
564564
translations = (
565565
""
566-
"|[00:00.00] Quelques paroles synchronisées"
567-
"|[00:01.00] Quelques paroles plus synchronisées"
566+
" | [00:00.00] Quelques paroles synchronisées"
567+
" | [00:01.00] Quelques paroles plus synchronisées"
568568
)
569569
else:
570570
translations = (
571571
""
572-
"|Quelques paroles synchronisées"
573-
"|Quelques paroles plus synchronisées"
572+
" | Quelques paroles synchronisées"
573+
" | Quelques paroles plus synchronisées"
574574
)
575575

576576
return [

0 commit comments

Comments
 (0)