Skip to content

Commit

Permalink
Option to translate to any language
Browse files Browse the repository at this point in the history
TODO: Update database to store multiple language translations for a single song
  • Loading branch information
patkub committed Nov 5, 2024
1 parent 092a527 commit b14aa49
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 5 deletions.
1 change: 1 addition & 0 deletions src/database_songs/SongDatabaseHandler.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
16 changes: 13 additions & 3 deletions src/songs.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,15 @@
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
@param artist: the name of the artist
@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
Expand Down Expand Up @@ -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)
Expand All @@ -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",
Expand Down Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions src/translate_lyrics/TranslateLyrics.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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 = {
Expand Down

0 comments on commit b14aa49

Please sign in to comment.