Fix bug in koreader migrator and delete scrobbles before migrating
This commit is contained in:
@ -17,7 +17,11 @@ class Command(BaseCommand):
|
||||
def handle(self, *args, **options):
|
||||
scrobbles_to_create = []
|
||||
|
||||
for book in Book.objects.all():
|
||||
for book in Book.objects.filter(koreader_id__isnull=False):
|
||||
for scrobble in book.scrobble_set.all():
|
||||
scrobble.scrobbledpage_set.all().delete()
|
||||
book.scrobble_set.all().delete()
|
||||
|
||||
koreader_data = book.koreader_data_by_hash or {}
|
||||
if book.koreader_md5:
|
||||
koreader_data[book.koreader_md5] = {
|
||||
@ -50,7 +54,7 @@ class Command(BaseCommand):
|
||||
if prev_page:
|
||||
seconds_from_last_page = (
|
||||
page.end_time.timestamp()
|
||||
- prev_page.start_ts.timestamp()
|
||||
- prev_page.start_time.timestamp()
|
||||
)
|
||||
playback_position_seconds = (
|
||||
playback_position_seconds + page.duration_seconds
|
||||
@ -124,9 +128,9 @@ class Command(BaseCommand):
|
||||
timestamp=timestamp,
|
||||
stop_timestamp=stop_timestamp,
|
||||
playback_position_seconds=playback_position_seconds,
|
||||
book_koreader_hash=book.koreader_data_by_hash.keys()[
|
||||
0
|
||||
],
|
||||
book_koreader_hash=list(
|
||||
book.koreader_data_by_hash.keys()
|
||||
)[0],
|
||||
book_page_data=scrobble_page_data,
|
||||
book_pages_read=page.number,
|
||||
in_progress=False,
|
||||
|
||||
@ -1,62 +0,0 @@
|
||||
from books.models import Book
|
||||
from django.core.management.base import BaseCommand
|
||||
|
||||
|
||||
class Command(BaseCommand):
|
||||
def add_arguments(self, parser):
|
||||
parser.add_argument(
|
||||
"--commit",
|
||||
action="store_true",
|
||||
help="Actually populate data",
|
||||
)
|
||||
|
||||
def handle(self, *args, **options):
|
||||
dry_run = True
|
||||
if options["commit"]:
|
||||
dry_run = False
|
||||
|
||||
pages_to_create = []
|
||||
associated_scrobble = None
|
||||
last_scrobble = None
|
||||
for book in Book.objects.all():
|
||||
for page in book.page_set.all().order_by("number"):
|
||||
notes = ""
|
||||
last_scrobble = associated_scrobble
|
||||
if (
|
||||
not associated_scrobble
|
||||
or page.number > associated_scrobble.book_pages_read
|
||||
):
|
||||
associated_scrobble = page.user.scrobble_set.filter(
|
||||
book=page.book,
|
||||
timestamp__gte=page.start_time,
|
||||
timestamp__lte=page.end_time,
|
||||
).first()
|
||||
|
||||
if (
|
||||
last_scrobble
|
||||
and not associated_scrobble
|
||||
and page.number > last_scrobble.book_pages_read
|
||||
):
|
||||
associated_scrobble = last_scrobble
|
||||
notes = f"Extrapolated reading from scrobble {associated_scrobble.id}"
|
||||
|
||||
pages_to_create.append(
|
||||
ScrobbledPage(
|
||||
scrobble=associated_scrobble,
|
||||
number=page.number,
|
||||
start_time=page.start_time,
|
||||
end_time=page.end_time,
|
||||
duration_seconds=page.duration_seconds,
|
||||
notes=notes,
|
||||
)
|
||||
)
|
||||
|
||||
pages_to_move_len = len(pages_to_create)
|
||||
if dry_run:
|
||||
print(
|
||||
f"Found {pages_to_move_len} to migrate. Use --commit to move them"
|
||||
)
|
||||
return
|
||||
|
||||
ScrobbledPage.objects.bulk_create(pages_to_create)
|
||||
print(f"Migrated {pages_to_move_len} generic pages to scrobbled pages")
|
||||
Reference in New Issue
Block a user