Wrong use of ISBNDB API
This commit is contained in:
parent
263b4cff77
commit
aa51a40cf6
|
@ -96,8 +96,13 @@ class MediaAdminForm(ModelForm):
|
||||||
req = urllib.request.Request(api_url)
|
req = urllib.request.Request(api_url)
|
||||||
req.add_header("Authorization", os.getenv("ISBNDB_KEY"))
|
req.add_header("Authorization", os.getenv("ISBNDB_KEY"))
|
||||||
with urllib.request.urlopen(req) as url:
|
with urllib.request.urlopen(req) as url:
|
||||||
data = json.loads(url.read().decode())
|
data: dict = json.loads(url.read().decode())["book"]
|
||||||
print(data)
|
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["title"] = data["title"]
|
||||||
self.cleaned_data["published_date"] = data["date_published"][:10]
|
self.cleaned_data["published_date"] = data["date_published"][:10]
|
||||||
self.cleaned_data["number_of_pages"] = data["pages"]
|
self.cleaned_data["number_of_pages"] = data["pages"]
|
||||||
|
|
|
@ -46,4 +46,4 @@ class Command(BaseCommand):
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
self.stderr.write(self.style.WARNING(
|
self.stderr.write(self.style.WARNING(
|
||||||
"An error occured while importing ISBN {isbn}: {error}"
|
"An error occured while importing ISBN {isbn}: {error}"
|
||||||
.format(isbn=isbn, error=str(e))))
|
.format(isbn=isbn, error=str(e.__class__) + "(" + str(e) + ")")))
|
||||||
|
|
Loading…
Reference in New Issue