Wrong use of ISBNDB API

This commit is contained in:
Yohann D'ANELLO 2020-10-03 15:47:22 +02:00
parent 263b4cff77
commit aa51a40cf6
2 changed files with 7 additions and 2 deletions

View File

@ -96,8 +96,13 @@ class MediaAdminForm(ModelForm):
req = urllib.request.Request(api_url)
req.add_header("Authorization", os.getenv("ISBNDB_KEY"))
with urllib.request.urlopen(req) as url:
data = json.loads(url.read().decode())
data: dict = json.loads(url.read().decode())["book"]
print(data)
data.setdefault("title", "")
data.setdefault("date_published", "1970-01-01")
data.setdefault("pages", 0)
data.setdefault("authors", [])
data.setdefault("image", "")
self.cleaned_data["title"] = data["title"]
self.cleaned_data["published_date"] = data["date_published"][:10]
self.cleaned_data["number_of_pages"] = data["pages"]

View File

@ -46,4 +46,4 @@ class Command(BaseCommand):
except Exception as e:
self.stderr.write(self.style.WARNING(
"An error occured while importing ISBN {isbn}: {error}"
.format(isbn=isbn, error=str(e))))
.format(isbn=isbn, error=str(e.__class__) + "(" + str(e) + ")")))