-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
0423eb9
commit bbafc88
Showing
6 changed files
with
342 additions
and
275 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,33 +1,35 @@ | ||
__pycache__ | ||
.mypy_cache | ||
*.env | ||
_archive | ||
.vscode | ||
app | ||
app/ | ||
Dockerfile | ||
lyrics/* | ||
commands.txt | ||
prepare_game.py | ||
requirements.txt | ||
resources | ||
Makefile | ||
lyrics_translator/spotify_api.py | ||
TODO.md | ||
poetry.lock | ||
main.py | ||
|
||
.mypy_cache/ | ||
/.coverage | ||
/.coverage.* | ||
/.nox/ | ||
/.python-version | ||
/.pytype/ | ||
/dist/ | ||
/docs/_build/ | ||
/src/*.egg-info/ | ||
__pycache__/ | ||
|
||
|
||
static | ||
__pycache__ | ||
.mypy_cache | ||
*.env | ||
_archive | ||
.vscode | ||
app | ||
app/ | ||
Dockerfile | ||
lyrics/* | ||
commands.txt | ||
prepare_game.py | ||
requirements.txt | ||
resources | ||
Makefile | ||
lyrics_translator/spotify_api.py | ||
lyrics_translator/model_Test.py | ||
|
||
TODO.md | ||
poetry.lock | ||
main.py | ||
|
||
.mypy_cache/ | ||
/.coverage | ||
/.coverage.* | ||
/.nox/ | ||
/.python-version | ||
/.pytype/ | ||
/dist/ | ||
/docs/_build/ | ||
/src/*.egg-info/ | ||
__pycache__/ | ||
|
||
|
||
static | ||
site |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,13 @@ | ||
from lyrics_translator import LyricsTranslator | ||
|
||
if __name__ == "__main__": | ||
|
||
song = "Surfin' U.S.A." | ||
artist = "The Beach Boys" | ||
language = "de" | ||
|
||
translator = LyricsTranslator(language) | ||
lyrics = translator.get_song_translation(song, artist) | ||
print(lyrics) | ||
|
||
lyrics.save() | ||
from lyrics_translator import LyricsTranslator | ||
|
||
if __name__ == "__main__": | ||
|
||
song = "Surfin' U.S.A." | ||
artist = "The Beach Boys" | ||
|
||
for language in ["de", "sv"]: | ||
|
||
translator = LyricsTranslator(language) | ||
lyrics = translator.get_song_translation(song, artist) | ||
lyrics.save() | ||
print(lyrics) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,35 +1,33 @@ | ||
import textwrap | ||
|
||
import click | ||
from dotenv import dotenv_values | ||
|
||
from lyrics_translator import LyricsTranslator | ||
|
||
from . import __version__ | ||
|
||
|
||
@click.command() | ||
@click.option( | ||
"--song", default="Surfin' U.S.A.", help="Name of the song to be translated." | ||
) | ||
@click.option("--artist", default="The Beach Boys", help="Name of the artist.") | ||
@click.option( | ||
"--language", default="de", help="Language to song lyrics to translate to." | ||
) | ||
@click.option( | ||
"--testing", | ||
default=False, | ||
help=('Mode of developement, if testing is set to "True" only dummy data is used'), | ||
) | ||
@click.version_option(version=__version__) | ||
def main(song, artist, language, testing): | ||
|
||
config = dotenv_values(".env") | ||
|
||
lyrics = LyricsTranslator(song, artist, config, language, testing=testing) | ||
lyrics.get_song_translation() | ||
|
||
title = f"'{song}' by '{artist}' translated into '{language}'" | ||
|
||
click.secho(title, fg="green") | ||
click.echo(lyrics) | ||
import textwrap | ||
|
||
import click | ||
|
||
from lyrics_translator import LyricsTranslator | ||
|
||
from . import __version__ | ||
|
||
|
||
@click.command() | ||
@click.option( | ||
"--song", default="Surfin' U.S.A.", help="Name of the song to be translated." | ||
) | ||
@click.option("--artist", default="The Beach Boys", help="Name of the artist.") | ||
@click.option( | ||
"--language", default="de", help="Language to song lyrics to translate to." | ||
) | ||
@click.option( | ||
"--testing", | ||
default=False, | ||
help=('Mode of developement, if testing is set to "True" only dummy data is used'), | ||
) | ||
@click.version_option(version=__version__) | ||
def main(song, artist, language, testing): | ||
|
||
translator = LyricsTranslator(language) | ||
lyrics = translator.get_song_translation(song, artist, testing) | ||
lyrics.get_song_translation() | ||
|
||
title = f"'{song}' by '{artist}' translated into '{language}'" | ||
|
||
click.secho(title, fg="green") | ||
click.echo(lyrics) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,69 +1,72 @@ | ||
import lyricsgenius | ||
from dotenv import dotenv_values | ||
|
||
from lyrics_translator.core.lyrics import Lyrics | ||
from lyrics_translator.core.translator import Translator | ||
|
||
MANDATORY_ENV_VARS = ["GENIUS_ACCESS_TOKEN"] | ||
|
||
|
||
class LyricsTranslator(object): | ||
def __init__(self, language: str = "de", origin_language="en", config: dict = None) -> None: | ||
"""LyricsTranslator main class, which uses the Lyrics class to fetch | ||
lyrics and translates them into the target language. | ||
Args: | ||
language (str): target language that the lyrics should be transalted into. Defaults to "de". | ||
origin_language (str, optional): set optional current language of the lyrics for more advanced traslations. Defaults to "en". | ||
config (dict): config file to pass in environment variables | ||
Raises: | ||
EnvironmentError: _description_ | ||
""" | ||
self.language = language | ||
self.origin_language = origin_language | ||
|
||
if config is None: | ||
self.config = dotenv_values(".env") | ||
else: | ||
self.config = config | ||
|
||
for env_var in MANDATORY_ENV_VARS: | ||
if env_var not in self.config: | ||
message = ( | ||
f"Failed because the envrionment variable '{env_var}' is not set." | ||
f" Add '{env_var}' to the '.env' file and try it again!" | ||
) | ||
raise EnvironmentError(message) | ||
|
||
self.translator = Translator( | ||
language=self.language, origin_language=self.origin_language | ||
) | ||
self.translator.get_translator_pipeline() | ||
|
||
self.genius = lyricsgenius.Genius( | ||
self.config["GENIUS_ACCESS_TOKEN"], timeout=10, retries=3 | ||
) | ||
|
||
def get_song_translation( | ||
self, song, artist, testing: bool = False, short: bool = False | ||
) -> None: | ||
"""Download the song lyrics from the API and translate them using the Lyrics class.""" | ||
|
||
lyrics = Lyrics(song=song, artist=artist, testing=testing) | ||
lyrics.download_lyrics(genius=self.genius) | ||
lyrics.translate( | ||
translator=self.translator, language=self.language, short=short | ||
) | ||
return lyrics | ||
|
||
def __repr__(self) -> str: | ||
"""LyricsTranslator representation | ||
Returns: | ||
str: returns the string to create the same instance | ||
""" | ||
return ( | ||
f"LyricsTranslator(" | ||
f" language={self.language}, origin_language={self.origin_language})" | ||
) | ||
import lyricsgenius | ||
from dotenv import dotenv_values | ||
|
||
from lyrics_translator.core.lyrics import Lyrics | ||
from lyrics_translator.core.translator import Translator | ||
|
||
MANDATORY_ENV_VARS = ["GENIUS_ACCESS_TOKEN"] | ||
|
||
|
||
class LyricsTranslator(object): | ||
def __init__( | ||
self, language: str = "de", origin_language="en", config: dict = None | ||
) -> None: | ||
"""LyricsTranslator main class, which uses the Lyrics class to fetch | ||
lyrics and translates them into the target language. | ||
Args: | ||
language (str): target language that the lyrics should be transalted into. Defaults to "de". | ||
origin_language (str, optional): set optional current language of the lyrics for more advanced traslations. Defaults to "en". | ||
config (dict): config file to pass in environment variables | ||
Raises: | ||
EnvironmentError: _description_ | ||
""" | ||
self.language = language | ||
self.origin_language = origin_language | ||
|
||
if config is None: | ||
self.config = dotenv_values(".env") | ||
else: | ||
self.config = config | ||
|
||
for env_var in MANDATORY_ENV_VARS: | ||
if env_var not in self.config: | ||
message = ( | ||
f"Failed because the envrionment variable '{env_var}' is not set." | ||
f" Add '{env_var}' to the '.env' file and try it again!" | ||
) | ||
raise EnvironmentError(message) | ||
|
||
self.translator = Translator( | ||
language=self.language, origin_language=self.origin_language | ||
) | ||
self.translator.get_translator_pipeline() | ||
|
||
self.genius = lyricsgenius.Genius( | ||
self.config["GENIUS_ACCESS_TOKEN"], timeout=10, retries=3 | ||
) | ||
|
||
def get_song_lyrics(self, song, artist, testing: bool = False): | ||
"""retrun only the lyrics of the song""" | ||
lyrics = Lyrics(song=song, artist=artist, testing=testing) | ||
text = lyrics.get_lyrics(genius=self.genius) | ||
return lyrics | ||
|
||
def get_song_translation(self, song, artist, testing: bool = False) -> None: | ||
"""Download the song lyrics from the API and translate them using the Lyrics class.""" | ||
lyrics = Lyrics(song=song, artist=artist, testing=testing) | ||
lyrics.download_lyrics(genius=self.genius) | ||
lyrics.translate(translator=self.translator, language=self.language) | ||
return lyrics | ||
|
||
def __repr__(self) -> str: | ||
"""LyricsTranslator representation | ||
Returns: | ||
str: returns the string to create the same instance | ||
""" | ||
return ( | ||
f"LyricsTranslator(" | ||
f" language={self.language}, origin_language={self.origin_language})" | ||
) |
Oops, something went wrong.