From b14aa493ba24133213d365a45d653987c1142b0b Mon Sep 17 00:00:00 2001 From: Patrick Kubiak Date: Tue, 5 Nov 2024 01:32:41 -0500 Subject: [PATCH] Option to translate to any language TODO: Update database to store multiple language translations for a single song --- src/database_songs/SongDatabaseHandler.py | 1 + src/songs.py | 16 +++++++++++++--- src/translate_lyrics/TranslateLyrics.py | 4 ++-- 3 files changed, 16 insertions(+), 5 deletions(-) diff --git a/src/database_songs/SongDatabaseHandler.py b/src/database_songs/SongDatabaseHandler.py index 4e75f66..7065f1e 100644 --- a/src/database_songs/SongDatabaseHandler.py +++ b/src/database_songs/SongDatabaseHandler.py @@ -60,6 +60,7 @@ def save_song(self, song_info, lyrics, translation): @param translation: English translation @return: True on success, False otherwise """ + # TODO: Update database to store multiple language translations for a single song success = True try: if self.con: diff --git a/src/songs.py b/src/songs.py index 6ee7821..7c06ef0 100644 --- a/src/songs.py +++ b/src/songs.py @@ -26,7 +26,7 @@ load_dotenv() -def process_song(song, artist, access_keys, refresh, genius_patch): +def process_song(song, artist, access_keys, refresh, genius_patch, to_lang): """ Fetch song lyrics, translate to English, and display original and English side-by-side lyrics. @param song: the name of the song @@ -34,6 +34,7 @@ def process_song(song, artist, access_keys, refresh, genius_patch): @param access_keys: dictionary of API access keys @param refresh: skip database and refresh song @param genius_patch: use patched version of Genius api + @param to_lang: language to translate song to """ # # Fetch song lyrics from Genius @@ -74,7 +75,7 @@ def process_song(song, artist, access_keys, refresh, genius_patch): lyrics_translator = TranslateLyrics( access_keys["CS_MS_TRANSLATOR_KEY"], access_keys["CS_MS_TRANSLATOR_REGION"] ) - english_translation = lyrics_translator.translate_lyrics(song_lyrics) + english_translation = lyrics_translator.translate_lyrics(song_lyrics, to_lang) # Save this song in the database song_db_handler.save_song(song_info, song_lyrics, english_translation) @@ -94,6 +95,13 @@ def main(): # pragma: no cover formatter_class=argparse.ArgumentDefaultsHelpFormatter ) parser.add_argument("song", nargs="+") + parser.add_argument( + "--to-lang", + type=str, + default="en", + required=False, + help="Language to translate to", + ) parser.add_argument( "-r", "--refresh", @@ -122,7 +130,9 @@ def main(): # pragma: no cover # Fetch song lyrics, translate to English, and display original and English side-by-side lyrics. # - process_song(song, artist, access_keys, args.refresh, args.genius_patch) + process_song( + song, artist, access_keys, args.refresh, args.genius_patch, args.to_lang + ) if __name__ == "__main__": # pragma: no cover diff --git a/src/translate_lyrics/TranslateLyrics.py b/src/translate_lyrics/TranslateLyrics.py index a0249c5..37a61d2 100644 --- a/src/translate_lyrics/TranslateLyrics.py +++ b/src/translate_lyrics/TranslateLyrics.py @@ -12,7 +12,7 @@ def __init__(self, translator_key, region): self.subscription_key = translator_key self.region = region - def translate_lyrics(self, lyrics): + def translate_lyrics(self, lyrics, to_lang): """ Translates lyrics to English using Microsoft Azure AI Translator @@ -26,7 +26,7 @@ def translate_lyrics(self, lyrics): # from romanian to english # params = '&from=ro&to=en' # or detect original language, and translate to english - params = "&to=en" + params = "&to=" + to_lang constructed_url = endpoint + path + params headers = {