Skip to content

Commit

Permalink
NASA APOD: Catch simplejson exception.
Browse files Browse the repository at this point in the history
  • Loading branch information
Gadgetoid committed May 17, 2024
1 parent 16b2712 commit c3b6fdd
Showing 1 changed file with 6 additions and 3 deletions.
9 changes: 6 additions & 3 deletions plugins/nasa_apod/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import qrcode
import math
import json
import simplejson
import sys

DEFAULT_WIDTH = 600
Expand Down Expand Up @@ -89,13 +90,15 @@ def text_in_rect(canvas, text, font, color, bg_color, rect, align='left', valign
apod_date = str(datetime.datetime.now().date())
suffix = "daily"

response = requests.get(f"https://api.nasa.gov/planetary/apod?api_key=DEMO_KEY&thumbs=true&date={apod_date}")
apod_url = f"https://api.nasa.gov/planetary/apod?api_key=DEMO_KEY&thumbs=true&date={apod_date}"
print(f"Fetching: {apod_url}")
response = requests.get(apod_url)

try:
metadata = response.json()

except json.decoder.JSONDecodeError:
print(response.text)
except (json.decoder.JSONDecodeError, simplejson.errors.JSONDecodeError):
print(f"JSON Decode error. Response: {response.text}")
sys.exit(1)

print(apod_date, metadata)
Expand Down

0 comments on commit c3b6fdd

Please sign in to comment.