Reorganize books
This commit is contained in:
@ -6,14 +6,14 @@ from scrobbles.admin import ScrobbleInline
|
|||||||
|
|
||||||
|
|
||||||
@admin.register(Author)
|
@admin.register(Author)
|
||||||
class AlbumAdmin(admin.ModelAdmin):
|
class AuthorAdmin(admin.ModelAdmin):
|
||||||
date_hierarchy = "created"
|
date_hierarchy = "created"
|
||||||
list_display = ("name", "openlibrary_id")
|
list_display = ("name", "openlibrary_id")
|
||||||
ordering = ("name",)
|
ordering = ("name",)
|
||||||
|
|
||||||
|
|
||||||
@admin.register(Book)
|
@admin.register(Book)
|
||||||
class ArtistAdmin(admin.ModelAdmin):
|
class BookAdmin(admin.ModelAdmin):
|
||||||
date_hierarchy = "created"
|
date_hierarchy = "created"
|
||||||
list_display = (
|
list_display = (
|
||||||
"title",
|
"title",
|
||||||
@ -23,3 +23,6 @@ class ArtistAdmin(admin.ModelAdmin):
|
|||||||
"openlibrary_id",
|
"openlibrary_id",
|
||||||
)
|
)
|
||||||
ordering = ("title",)
|
ordering = ("title",)
|
||||||
|
inlines = [
|
||||||
|
ScrobbleInline,
|
||||||
|
]
|
||||||
|
|||||||
@ -109,6 +109,8 @@ def process_koreader_sqlite_file(sqlite_file_path, user_id):
|
|||||||
if existing:
|
if existing:
|
||||||
logger.debug(f"Skipping existing scrobble {new_scrobble}")
|
logger.debug(f"Skipping existing scrobble {new_scrobble}")
|
||||||
continue
|
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")
|
logger.debug(f"Queued scrobble {new_scrobble} for creation")
|
||||||
new_scrobbles.append(new_scrobble)
|
new_scrobbles.append(new_scrobble)
|
||||||
|
|||||||
@ -1,14 +1,12 @@
|
|||||||
import logging
|
import logging
|
||||||
from typing import Dict
|
|
||||||
|
|
||||||
|
from books.openlibrary import lookup_book_from_openlibrary
|
||||||
from django.conf import settings
|
from django.conf import settings
|
||||||
from django.contrib.auth import get_user_model
|
from django.contrib.auth import get_user_model
|
||||||
from django.db import models
|
from django.db import models
|
||||||
from django.urls import reverse
|
from django.urls import reverse
|
||||||
from django_extensions.db.models import TimeStampedModel
|
from django_extensions.db.models import TimeStampedModel
|
||||||
from scrobbles.mixins import ScrobblableMixin
|
from scrobbles.mixins import ScrobblableMixin
|
||||||
|
|
||||||
from books.utils import lookup_book_from_openlibrary
|
|
||||||
from scrobbles.utils import get_scrobbles_for_media
|
from scrobbles.utils import get_scrobbles_for_media
|
||||||
|
|
||||||
logger = logging.getLogger(__name__)
|
logger = logging.getLogger(__name__)
|
||||||
@ -68,6 +66,8 @@ class Book(ScrobblableMixin):
|
|||||||
return 0
|
return 0
|
||||||
return int(self.pages * (self.COMPLETION_PERCENT / 100))
|
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()
|
last_scrobble = get_scrobbles_for_media(self, user).last()
|
||||||
return int((last_scrobble.book_pages_read / self.pages) * 100)
|
return int((last_scrobble.book_pages_read / self.pages) * 100)
|
||||||
|
|||||||
@ -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"),
|
|
||||||
}
|
|
||||||
|
|||||||
Reference in New Issue
Block a user