Skip to content

Commit

Permalink
Handle network errors when translating
Browse files Browse the repository at this point in the history
  • Loading branch information
patkub committed Oct 27, 2024
1 parent 3a98a80 commit 62870e3
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 4 deletions.
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# App
python-dotenv
requests
lyricsgenius
side-by-side

Expand Down
14 changes: 10 additions & 4 deletions translate_lyrics/TranslateLyrics.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import requests, uuid

from requests.exceptions import RequestException

class TranslateLyrics:
def __init__(self, translator_key):
Expand Down Expand Up @@ -37,8 +37,14 @@ def translate_lyrics(self, lyrics):
body = [{
'text': lyrics
}]
request = requests.post(constructed_url, headers=headers, json=body)
response = request.json()
english_translation = response[0]["translations"][0]["text"]

english_translation = ""
try:
request = requests.post(constructed_url, headers=headers, json=body)
response = request.json()
english_translation = response[0]["translations"][0]["text"]
except (RequestException, KeyError):
# ignore network error, or invalid data
pass

return english_translation

0 comments on commit 62870e3

Please sign in to comment.