Skip to content

Commit

Permalink
Refactor as a script (#4)
Browse files Browse the repository at this point in the history
* Refactor with src directory

* Setup pyproject.toml as a runnable script

* prefix all env vars with CS_

* lint with black

* update README install instructions

* Fix ms translator key env vars
  • Loading branch information
patkub authored Nov 2, 2024
1 parent 709fdf3 commit 4cfcf1a
Show file tree
Hide file tree
Showing 21 changed files with 102 additions and 41 deletions.
69 changes: 43 additions & 26 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,49 +14,35 @@

Enjoy listening to music and learning a new language!

### Setup
Requires Python 3.11+
### Install

Create an `.env` file with your API keys:
```
GENIUS_ACCESS_TOKEN=...
MS_TRANSLATOR_KEY=...
MS_TRANSLATOR_REGION=...
```
- [Genius API](https://docs.genius.com)
- [Azure Translator Resource](https://learn.microsoft.com/en-us/azure/ai-services/translator/create-translator-resource)
Requires [pipx](https://pipx.pypa.io/stable/)

Setup a virtual environment:
Install the `console-songs` script with pipx.
```
python3 -m venv .
source bin/activate
pipx install .
```

Install dependencies:
Setup environment variables
```
pip3 install -r requirements.txt
```

You should now be able to run `python3 songs.py --help` and see usage.

When making code changes, remember to format code with black:
```
black .
export "CS_GENIUS_ACCESS_TOKEN=..."
export "CS_MS_TRANSLATOR_KEY=..."
export "CS_MS_TRANSLATOR_REGION=..."
```

### Usage

Provide the song and optionally the artist's name
```
python3 songs.py song [artist]
console-songs song [artist]
```
<details>

<summary>Full Usage</summary>

```
(console-songs) patka@Patricks-MacBook-Air console-songs % python3 songs.py --help
usage: songs.py [-h] [-r | --refresh | --no-refresh] [--genius-patch | --no-genius-patch] song [song ...]
(console-songs) patka@Patricks-MacBook-Air console-songs % console-songs --help
usage: console-songs [-h] [-r | --refresh | --no-refresh] [--genius-patch | --no-genius-patch] song [song ...]
positional arguments:
song
Expand All @@ -78,7 +64,7 @@ python3 songs.py "Ma ucide ea" "Mihail"

### Sample Output
```
(console-songs) patka@Patricks-MacBook-Air console-songs % python3 songs.py "Ma ucide ea" "Mihail"
(console-songs) patka@Patricks-MacBook-Air console-songs % console-songs "Ma ucide ea" "Mihail"
Searching for "Ma ucide ea" by Mihail...
Done.
Expand Down Expand Up @@ -178,6 +164,37 @@ Da, da... Yes, yes...
</details>


### Development Setup
Requires Python 3.11+

Create an `.env` file with your API keys:
```
CS_GENIUS_ACCESS_TOKEN=...
CS_MS_TRANSLATOR_KEY=...
CS_MS_TRANSLATOR_REGION=...
```
- [Genius API](https://docs.genius.com)
- [Azure Translator Resource](https://learn.microsoft.com/en-us/azure/ai-services/translator/create-translator-resource)

Setup a virtual environment:
```
python3 -m venv .
source bin/activate
```

Install dependencies:
```
pip3 install -r requirements.txt
```

You should now be able to run `python3 songs.py --help` and see usage.

When making code changes, remember to format code with black:
```
black .
```


### Unit Tests

Run unit tests with [pytest](https://docs.pytest.org/en/stable/) and report coverage.
Expand Down
36 changes: 36 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
[project]
name = "console-songs"
version = "2024.11.02"
dependencies = [
"python-dotenv",
"requests",
"lyricsgenius",
"side-by-side",
"beautifulsoup4"
]
requires-python = ">=3.8"
authors = [
{name = "Patrick Kubiak", email = "epicpatka@gmail.com"}
]
maintainers = [
{name = "Patrick Kubiak", email = "epicpatka@gmail.com"}
]
description = "Given a song and artist, fetches lyrics from Genius, translates them to English using Azure AI Translator, and displays original and translated lyrics side-by-side in the console."
readme = "README.md"
license = {file = "LICENSE"}
keywords = ["console", "songs", "genius-lyrics-api", "azure-translator"]
classifiers = [
"Development Status :: 5 - Production/Stable",
"Programming Language :: Python",
"Environment :: Console"
]

[project.optional-dependencies]
cli = [
"pytest",
"pytest-cov",
"black"
]

[project.scripts]
console-songs = "songs:main"
4 changes: 4 additions & 0 deletions src/__main__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
if __name__ == "__main__":
from songs import main

main()
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
16 changes: 10 additions & 6 deletions songs.py → src/songs.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ def process_song(song, artist, access_keys, refresh, genius_patch):
# Fetch song lyrics from Genius
#
patched_genius = PatchedGenius if genius_patch else None
lyrics_fetcher = FetchLyrics(access_keys["GENIUS_ACCESS_TOKEN"], patched_genius)
lyrics_fetcher = FetchLyrics(access_keys["CS_GENIUS_ACCESS_TOKEN"], patched_genius)
song_info = lyrics_fetcher.fetch_lyrics(song, artist)
if song_info is None:
# song not found, end
Expand Down Expand Up @@ -72,7 +72,7 @@ def process_song(song, artist, access_keys, refresh, genius_patch):
# Translate lyrics to English using Microsoft Azure AI Translator
#
lyrics_translator = TranslateLyrics(
access_keys["MS_TRANSLATOR_KEY"], access_keys["MS_TRANSLATOR_REGION"]
access_keys["CS_MS_TRANSLATOR_KEY"], access_keys["CS_MS_TRANSLATOR_REGION"]
)
english_translation = lyrics_translator.translate_lyrics(song_lyrics)

Expand All @@ -85,7 +85,7 @@ def process_song(song, artist, access_keys, refresh, genius_patch):
ConsoleDisplayLyrics.display_lyrics(song_info, song_lyrics, english_translation)


if __name__ == "__main__": # pragma: no cover
def main(): # pragma: no cover
#
# Parse arguments
#
Expand Down Expand Up @@ -113,13 +113,17 @@ def process_song(song, artist, access_keys, refresh, genius_patch):
artist = args.song[1] if len(args.song) > 1 else None

access_keys = {
"GENIUS_ACCESS_TOKEN": os.getenv("GENIUS_ACCESS_TOKEN"),
"MS_TRANSLATOR_KEY": os.getenv("MS_TRANSLATOR_KEY"),
"MS_TRANSLATOR_REGION": os.getenv("MS_TRANSLATOR_REGION"),
"CS_GENIUS_ACCESS_TOKEN": os.getenv("CS_GENIUS_ACCESS_TOKEN"),
"CS_MS_TRANSLATOR_KEY": os.getenv("CS_MS_TRANSLATOR_KEY"),
"CS_MS_TRANSLATOR_REGION": os.getenv("CS_MS_TRANSLATOR_REGION"),
}

#
# 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)


if __name__ == "__main__": # pragma: no cover
main()
18 changes: 9 additions & 9 deletions test_songs.py → src/test_songs.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,9 @@ def test_process_song(
song = "test_song_name"
artist = "test_artist_name"
access_keys = {
"GENIUS_ACCESS_TOKEN": "fake_token_1",
"MS_TRANSLATOR_KEY": "fake_token_2",
"MS_TRANSLATOR_REGION": "fake_token_3",
"CS_GENIUS_ACCESS_TOKEN": "fake_token_1",
"CS_MS_TRANSLATOR_KEY": "fake_token_2",
"CS_MS_TRANSLATOR_REGION": "fake_token_3",
}
refresh = False
experimental = False
Expand Down Expand Up @@ -68,9 +68,9 @@ def test_process_song_exists(
song = "test_song_name"
artist = "test_artist_name"
access_keys = {
"GENIUS_ACCESS_TOKEN": "fake_token_1",
"MS_TRANSLATOR_KEY": "fake_token_2",
"MS_TRANSLATOR_REGION": "fake_token_3",
"CS_GENIUS_ACCESS_TOKEN": "fake_token_1",
"CS_MS_TRANSLATOR_KEY": "fake_token_2",
"CS_MS_TRANSLATOR_REGION": "fake_token_3",
}
refresh = False
experimental = False
Expand Down Expand Up @@ -107,9 +107,9 @@ def test_process_song_null(
song = "test_song_name"
artist = "test_artist_name"
access_keys = {
"GENIUS_ACCESS_TOKEN": "fake_token_1",
"MS_TRANSLATOR_KEY": "fake_token_2",
"MS_TRANSLATOR_REGION": "fake_token_3",
"CS_GENIUS_ACCESS_TOKEN": "fake_token_1",
"CS_MS_TRANSLATOR_KEY": "fake_token_2",
"CS_MS_TRANSLATOR_REGION": "fake_token_3",
}
refresh = False
experimental = False
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.

0 comments on commit 4cfcf1a

Please sign in to comment.