diff --git a/media/forms.py b/media/forms.py index 5dfc41b..6445566 100644 --- a/media/forms.py +++ b/media/forms.py @@ -6,6 +6,7 @@ import json import os import re import unicodedata +from urllib.error import HTTPError import urllib.request from django.db.models import QuerySet @@ -95,8 +96,11 @@ class MediaAdminForm(ModelForm): api_url = "https://api2.isbndb.com/book/" + str(isbn) + "?Authorization=" + os.getenv("ISBNDB_KEY") req = urllib.request.Request(api_url) req.add_header("Authorization", os.getenv("ISBNDB_KEY")) - with urllib.request.urlopen(req) as url: - data: dict = json.loads(url.read().decode())["book"] + try: + with urllib.request.urlopen(req) as url: + data: dict = json.loads(url.read().decode())["book"] + except HTTPError: + return False print(data) data.setdefault("title", "") data.setdefault("date_published", "1970-01-01")