Skip to content

Commit

Permalink
update file
Browse files Browse the repository at this point in the history
  • Loading branch information
MauroLuzzatto committed Jan 8, 2023
1 parent 0423eb9 commit bbafc88
Show file tree
Hide file tree
Showing 6 changed files with 342 additions and 275 deletions.
66 changes: 34 additions & 32 deletions .gitignore
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
26 changes: 13 additions & 13 deletions example.py
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)
68 changes: 33 additions & 35 deletions lyrics_translator/console.py
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)
23 changes: 13 additions & 10 deletions lyrics_translator/core/lyrics.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,14 @@


class Lyrics(object):
def __init__(self, song:str, artist:str, testing:bool=None) -> None:
def __init__(self, song: str, artist: str, testing: bool = None) -> None:
self.song = song
self.artist = artist
self.testing = testing

self.text: str = None
self.translation:str = None
self.language:str = None
self.translation: str = None
self.language: str = None

def download_lyrics(self, genius, get_full_info: Optional[bool] = False) -> None:
"""Download the lyrics of the song using the `genius` API.
Expand All @@ -37,7 +37,7 @@ def download_lyrics(self, genius, get_full_info: Optional[bool] = False) -> None

self.text = genius_song.lyrics

def translate(self, translator, language:str, short:bool=False) -> None:
def translate(self, translator, language: str) -> None:
"""Translate the lyrics into the target language.
Returns:
Expand All @@ -48,34 +48,37 @@ def translate(self, translator, language:str, short:bool=False) -> None:
if self.testing:
self.translation = "<test translation>"
else:
self.translation = translator.translate(self.text, short)
self.translation = translator.translate(self.text)

def get_translation(self, translator, language) -> str:
self.translate(translator, language)
return self.translation

def get_lyrics(self, genius) -> str:
self.download_lyrics(genius)
return self.lyrics
return self.text

def save(self, folder: Path = None, kind: str = "txt") -> None:
def save(self, folder: str = "lyrics", kind: str = "txt") -> None:
"""Save the translated lyrics to a file.
Args:
folder (Path): _description_
kind (str, optional): _description_. Defaults to "txt".
"""

if folder is None:
folder = Path().resolve()
if not folder:
folder_path = Path().resolve()
else:
folder_path = Path(folder).resolve()
folder_path.mkdir(parents=True, exist_ok=True)

saver = Saver(
song=self.song,
artist=self.artist,
translation=self.translation,
language=self.language,
)
saver.save(folder=folder, kind=kind)
saver.save(folder=folder_path, kind=kind)

def __str__(self) -> str:
"""string version of the Lyrics instance, returns the translated lyrics
Expand Down
141 changes: 72 additions & 69 deletions lyrics_translator/core/lyrics_translator.py
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})"
)
Loading

0 comments on commit bbafc88

Please sign in to comment.