From 30f78a9290cfc25796b7253fce6b7caaeeecc54e Mon Sep 17 00:00:00 2001 From: Colin Powell Date: Sat, 3 Feb 2024 00:41:47 -0500 Subject: [PATCH] Fix bug in koreader migrator and delete scrobbles before migrating --- .../commands/migrate_koreader_data_to_json.py | 14 +++-- .../commands/migrate_pages_to_json_data.py | 62 ------------------- 2 files changed, 9 insertions(+), 67 deletions(-) delete mode 100644 vrobbler/apps/books/management/commands/migrate_pages_to_json_data.py diff --git a/vrobbler/apps/books/management/commands/migrate_koreader_data_to_json.py b/vrobbler/apps/books/management/commands/migrate_koreader_data_to_json.py index 109671f..8847751 100644 --- a/vrobbler/apps/books/management/commands/migrate_koreader_data_to_json.py +++ b/vrobbler/apps/books/management/commands/migrate_koreader_data_to_json.py @@ -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, diff --git a/vrobbler/apps/books/management/commands/migrate_pages_to_json_data.py b/vrobbler/apps/books/management/commands/migrate_pages_to_json_data.py deleted file mode 100644 index 4f1a4d6..0000000 --- a/vrobbler/apps/books/management/commands/migrate_pages_to_json_data.py +++ /dev/null @@ -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")