Fix author imports for books
This commit is contained in:
@ -44,6 +44,7 @@ def lookup_author_from_openlibrary(olid: str) -> dict:
|
|||||||
bio = results.get("bio")
|
bio = results.get("bio")
|
||||||
return {
|
return {
|
||||||
"name": results.get("name"),
|
"name": results.get("name"),
|
||||||
|
"openlibrary_id": olid,
|
||||||
"wikipedia_url": results.get("wikipedia"),
|
"wikipedia_url": results.get("wikipedia"),
|
||||||
"wikidata_id": remote_ids.get("wikidata"),
|
"wikidata_id": remote_ids.get("wikidata"),
|
||||||
"isni": remote_ids.get("isni"),
|
"isni": remote_ids.get("isni"),
|
||||||
@ -76,14 +77,13 @@ def lookup_book_from_openlibrary(title: str, author: str = None) -> dict:
|
|||||||
f"Lookup for {title} found top result with mismatched author"
|
f"Lookup for {title} found top result with mismatched author"
|
||||||
)
|
)
|
||||||
ol_id = top.get("cover_edition_key")
|
ol_id = top.get("cover_edition_key")
|
||||||
author_ol_id = get_first("author_key", top)
|
ol_author_id = get_first("author_key", top)
|
||||||
first_sentence = ""
|
first_sentence = ""
|
||||||
if top.get("first_sentence"):
|
if top.get("first_sentence"):
|
||||||
try:
|
try:
|
||||||
first_sentence = top.get("first_sentence")[0].get("value")
|
first_sentence = top.get("first_sentence")[0].get("value")
|
||||||
except AttributeError:
|
except AttributeError:
|
||||||
first_sentence = top.get("first_sentence")[0]
|
first_sentence = top.get("first_sentence")[0]
|
||||||
print(top)
|
|
||||||
return {
|
return {
|
||||||
"title": top.get("title"),
|
"title": top.get("title"),
|
||||||
"isbn": top.get("isbn")[0],
|
"isbn": top.get("isbn")[0],
|
||||||
@ -93,5 +93,5 @@ def lookup_book_from_openlibrary(title: str, author: str = None) -> dict:
|
|||||||
"first_sentence": first_sentence,
|
"first_sentence": first_sentence,
|
||||||
"pages": top.get("number_of_pages_median", None),
|
"pages": top.get("number_of_pages_median", None),
|
||||||
"cover_url": COVER_URL.format(id=ol_id),
|
"cover_url": COVER_URL.format(id=ol_id),
|
||||||
"ol_author_id": author_ol_id,
|
"ol_author_id": ol_author_id,
|
||||||
}
|
}
|
||||||
|
|||||||
@ -1,7 +1,10 @@
|
|||||||
from django.core.files.base import ContentFile
|
from django.core.files.base import ContentFile
|
||||||
import requests
|
import requests
|
||||||
from typing import Optional
|
from typing import Optional
|
||||||
from books.openlibrary import lookup_book_from_openlibrary
|
from books.openlibrary import (
|
||||||
|
lookup_author_from_openlibrary,
|
||||||
|
lookup_book_from_openlibrary,
|
||||||
|
)
|
||||||
from books.models import Author, Book
|
from books.models import Author, Book
|
||||||
|
|
||||||
|
|
||||||
@ -17,27 +20,26 @@ def update_or_create_book(
|
|||||||
if book_created or force_update:
|
if book_created or force_update:
|
||||||
cover_url = book_dict.pop("cover_url")
|
cover_url = book_dict.pop("cover_url")
|
||||||
ol_author_id = book_dict.pop("ol_author_id")
|
ol_author_id = book_dict.pop("ol_author_id")
|
||||||
ol_author_name = book_dict.pop("ol_author_name")
|
|
||||||
author_image_url = book_dict.pop("author_image_url")
|
|
||||||
|
|
||||||
Book.objects.filter(pk=book.id).update(**book_dict)
|
Book.objects.filter(pk=book.id).update(**book_dict)
|
||||||
book.refresh_from_db()
|
book.refresh_from_db()
|
||||||
|
|
||||||
# Process authors
|
# Process authors
|
||||||
if ol_author_name and ol_author_id:
|
author_dict = lookup_author_from_openlibrary(ol_author_id)
|
||||||
author, author_created = Author.objects.get_or_create(
|
author = Author.objects.filter(openlibrary_id=ol_author_id).first()
|
||||||
name=ol_author_name
|
if not author:
|
||||||
)
|
author_image_url = author_dict.pop("author_headshot_url", None)
|
||||||
if author_created or force_update:
|
|
||||||
author.openlibrary_id = ol_author_id
|
|
||||||
author.save()
|
|
||||||
|
|
||||||
|
author = Author.objects.create(**author_dict)
|
||||||
|
|
||||||
|
if author_image_url:
|
||||||
r = requests.get(author_image_url)
|
r = requests.get(author_image_url)
|
||||||
if r.status_code == 200:
|
if r.status_code == 200:
|
||||||
fname = f"{author.name}_{author.uuid}.jpg"
|
fname = f"{author.name}_{author.uuid}.jpg"
|
||||||
author.headshot.save(
|
author.headshot.save(
|
||||||
fname, ContentFile(r.content), save=True
|
fname, ContentFile(r.content), save=True
|
||||||
)
|
)
|
||||||
|
book.authors.add(author)
|
||||||
|
|
||||||
# Process cover URL
|
# Process cover URL
|
||||||
r = requests.get(cover_url)
|
r = requests.get(cover_url)
|
||||||
|
|||||||
Reference in New Issue
Block a user