Skip to content

Commit

Permalink
Specify if song wasn't found or API error occurred
Browse files Browse the repository at this point in the history
  • Loading branch information
ashermorgan committed Nov 16, 2024
1 parent 8ea1298 commit c9f1da0
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 8 deletions.
16 changes: 13 additions & 3 deletions songs2slides/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@ class SongData:
artist: str
lyrics: str

class SongNotFound(Exception):
"""Raised when the API cannot find the lyrics to a song"""
pass

def filter_lyrics(lyrics: str):
"""
Filter raw lyrics to remove text enclosed in brackets or parenthesis
Expand Down Expand Up @@ -75,7 +79,7 @@ def get_song_data(title: str, artist:str):
# Get API URL
url = os.getenv('API_URL')
if url is None:
raise Exception()
raise Exception('Bad API_URL')
url = url.replace('{title}', title, 1)
url = url.replace('{artist}', artist, 1)

Expand All @@ -84,14 +88,20 @@ def get_song_data(title: str, artist:str):
headers = { 'Authorization': auth } if auth else {}

# Query API
data = requests.get(url, headers=headers).json()
res = requests.get(url, headers=headers)
if res.status_code != requests.codes.ok:
if res.status_code == 404:
raise SongNotFound()
else:
res.raise_for_status()
data = res.json()

# Parse response
if 'lyrics' in data.keys():
return SongData(data['title'], data['artist'],
filter_lyrics(data['lyrics']))
else:
raise Exception()
raise Exception('API returned invalid lyric data')

def parse_song_lyrics(lyrics: str, lines_per_slide: int):
"""
Expand Down
9 changes: 7 additions & 2 deletions songs2slides/routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,19 +59,24 @@ def create_step_2():
songs = parse_form(request.form)

# Get lyrics
api_error = True # Whether an API error occured for all requests
for i in range(len(songs)):
try:
songs[i] = core.get_song_data(songs[i].title, songs[i].artist)
api_error = False
slides = core.parse_song_lyrics(songs[i].lyrics, 4)
songs[i].lyrics = '\n\n'.join(slides)
except:
except core.SongNotFound:
api_error = False
except Exception as e:
pass

# Count missing songs
missing = sum([1 for x in songs if x.lyrics == None])

# Return song data
return render_template('create-step-2.html', songs=songs, missing=missing)
return render_template('create-step-2.html', songs=songs, missing=missing,
api_error=api_error)

@bp.get('/create/step-3/')
def create_step_3_get():
Expand Down
1 change: 1 addition & 0 deletions songs2slides/templates/create-step-2.html
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ <h1>Step 2: Review Lyrics</h1>
</p>

<p id="missing-message" {% if missing == 0 %} hidden {% endif %}>
{% if api_error %} Our lyric API is currently down. {% endif %}
Lyrics must be entered manually for
<span id="missing-count">{{ missing }}</span> song(s).
</p>
Expand Down
7 changes: 4 additions & 3 deletions tests/test_routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@ def test_get_lyrics_basic(client, mocker):
core.parse_song_lyrics.assert_has_calls([
mocker.call('L1', 4), mocker.call('L2', 4)
])
routes.render_template.assert_called_with('create-step-2.html',
songs=songs, missing=0)
routes.render_template.assert_called_with('create-step-2.html', songs=songs,
missing=0, api_error=False)

def test_get_lyrics_one_error(client, mocker):
# Mock get_song_data, parse_song_lyrics, and render_template
Expand All @@ -62,7 +62,8 @@ def test_get_lyrics_one_error(client, mocker):
mocker.call('T1', 'A1'), mocker.call('T2', 'A2')
])
core.parse_song_lyrics.assert_has_calls([mocker.call('L2', 4)])
routes.render_template.assert_called_with('create-step-2.html', songs=songs, missing=1)
routes.render_template.assert_called_with('create-step-2.html', songs=songs,
missing=1, api_error=False)

def test_get_lyrics_missing_artist(client, mocker):
# Mock get_song_data
Expand Down

0 comments on commit c9f1da0

Please sign in to comment.