Reorganize books

This commit is contained in:
2023-03-05 02:30:01 -05:00
parent 25c00d7f1b
commit 0a6774c284
4 changed files with 11 additions and 53 deletions

View File

@ -6,14 +6,14 @@ from scrobbles.admin import ScrobbleInline
@admin.register(Author)
class AlbumAdmin(admin.ModelAdmin):
class AuthorAdmin(admin.ModelAdmin):
date_hierarchy = "created"
list_display = ("name", "openlibrary_id")
ordering = ("name",)
@admin.register(Book)
class ArtistAdmin(admin.ModelAdmin):
class BookAdmin(admin.ModelAdmin):
date_hierarchy = "created"
list_display = (
"title",
@ -23,3 +23,6 @@ class ArtistAdmin(admin.ModelAdmin):
"openlibrary_id",
)
ordering = ("title",)
inlines = [
ScrobbleInline,
]

View File

@ -109,6 +109,8 @@ def process_koreader_sqlite_file(sqlite_file_path, user_id):
if existing:
logger.debug(f"Skipping existing scrobble {new_scrobble}")
continue
if book.progress_for_user(user_id) >= Book.COMPLETION_PERCENT:
new_scrobble.long_play_complete = True
logger.debug(f"Queued scrobble {new_scrobble} for creation")
new_scrobbles.append(new_scrobble)

View File

@ -1,14 +1,12 @@
import logging
from typing import Dict
from books.openlibrary import lookup_book_from_openlibrary
from django.conf import settings
from django.contrib.auth import get_user_model
from django.db import models
from django.urls import reverse
from django_extensions.db.models import TimeStampedModel
from scrobbles.mixins import ScrobblableMixin
from books.utils import lookup_book_from_openlibrary
from scrobbles.utils import get_scrobbles_for_media
logger = logging.getLogger(__name__)
@ -68,6 +66,8 @@ class Book(ScrobblableMixin):
return 0
return int(self.pages * (self.COMPLETION_PERCENT / 100))
def progress_for_user(self, user: User) -> int:
def progress_for_user(self, user_id: int) -> int:
"""Used to keep track of whether the book is complete or not"""
user = User.objects.get(id=user_id)
last_scrobble = get_scrobbles_for_media(self, user).last()
return int((last_scrobble.book_pages_read / self.pages) * 100)

View File

@ -1,47 +0,0 @@
import json
from typing import Optional
import requests
import logging
logger = logging.getLogger(__name__)
SEARCH_URL = "https://openlibrary.org/search.json?title={title}"
ISBN_URL = "https://openlibrary.org/isbn/{isbn}.json"
def get_first(key: str, result: dict) -> str:
obj = ""
if obj_list := result.get(key):
obj = obj_list[0]
return obj
def lookup_book_from_openlibrary(title: str, author: str = None) -> dict:
search_url = SEARCH_URL.format(title=title)
response = requests.get(search_url)
if response.status_code != 200:
logger.warn(f"Bad response from OL: {response.status_code}")
return {}
results = json.loads(response.content)
if len(results.get("docs")) == 0:
logger.warn(f"No results found from OL for {title}")
return {}
top = results.get("docs")[0]
if author and author not in top["author_name"]:
logger.warn(
f"Lookup for {title} found top result with mismatched author"
)
return {
"title": top.get("title"),
"isbn": top.get("isbn")[0],
"openlibrary_id": top.get("cover_edition_key"),
"author_name": get_first("author_name", top),
"author_openlibrary_id": get_first("author_key", top),
"goodreads_id": get_first("id_goodreads", top),
"first_publish_year": top.get("first_publish_year"),
}