From 664148e702459fa101ec9cd7fc8be53ae6521be8 Mon Sep 17 00:00:00 2001 From: Colin Powell Date: Tue, 26 May 2026 09:26:34 -0400 Subject: [PATCH] [importers] Try speeding up koreader imports --- vrobbler/apps/scrobbles/importers/webdav.py | 44 +++++++++++++++---- .../management/commands/import_from_webdav.py | 13 ++---- 2 files changed, 39 insertions(+), 18 deletions(-) diff --git a/vrobbler/apps/scrobbles/importers/webdav.py b/vrobbler/apps/scrobbles/importers/webdav.py index 6fe6122..0460c0b 100644 --- a/vrobbler/apps/scrobbles/importers/webdav.py +++ b/vrobbler/apps/scrobbles/importers/webdav.py @@ -57,7 +57,13 @@ def import_from_webdav_for_all_users(restart=False, update_retroarch_hash=False) scale_count += scan_webdav_for_scale(client, user_id) logger.info( - "WebDAV import complete", + "Started %d KOReader, %d Trail GPX, %d Retroarch, %d BGStats, %d eBird, %d Scale WebDAV imports", + ko_count, + gpx_count, + retro_count, + bgstats_count, + ebird_count, + scale_count, extra={ "koreader": ko_count, "trail_gpx": gpx_count, @@ -76,17 +82,39 @@ def scan_webdav_for_koreader(webdav_client, user_id, restart=False): Uses the remote file's ETag (from a single PROPFIND) to detect changes without downloading the file, making the common no-change case fast. """ - koreader_path = DEFAULT_KOREADER_PATH + "statistics.sqlite3" - try: - remote_info = webdav_client.info(koreader_path) - except: + # Don't queue if one is still being processed + if KoReaderImport.objects.filter( + user_id=user_id, processed_finished__isnull=True + ).exists(): logger.info( - "No koreader stats file found on webdav", + "KoReader import already pending for user", extra={"user_id": user_id}, ) return 0 - remote_etag = remote_info.get("etag") or "" + try: + files = webdav_client.list(DEFAULT_KOREADER_PATH, get_info=True) + except: + logger.info( + "No var/koreader/ directory on webdav", + extra={"user_id": user_id}, + ) + return 0 + + sqlite_info = None + for f in files: + if (f.get("name") or "") == "statistics.sqlite3": + sqlite_info = f + break + + if not sqlite_info: + logger.info( + "No statistics.sqlite3 found in var/koreader/", + extra={"user_id": user_id}, + ) + return 0 + + remote_etag = sqlite_info.get("etag") or "" last_import = ( KoReaderImport.objects.filter(user_id=user_id, processed_finished__isnull=False) @@ -117,7 +145,7 @@ def scan_webdav_for_koreader(webdav_client, user_id, restart=False): new_hash = get_file_md5_hash(file_path) old_hash = last_import.file_md5_hash() if last_import else None - if last_import and remote_etag and new_hash == old_hash: + if last_import and not remote_etag and new_hash == old_hash: logger.info( "koreader stats file has not changed (content hash match)", extra={ diff --git a/vrobbler/apps/scrobbles/management/commands/import_from_webdav.py b/vrobbler/apps/scrobbles/management/commands/import_from_webdav.py index f5dbc46..3d00339 100644 --- a/vrobbler/apps/scrobbles/management/commands/import_from_webdav.py +++ b/vrobbler/apps/scrobbles/management/commands/import_from_webdav.py @@ -21,14 +21,7 @@ class Command(BaseCommand): if options["restart"]: restart = True update_hash = options.get("update_retroarch_hash", False) - ko_count, gpx_count, retro_count, bgstats_count, ebird_count, scale_count = ( - webdav.import_from_webdav_for_all_users( - restart=restart, - update_retroarch_hash=update_hash, - ) - ) - print( - f"Started {ko_count} KOReader, {gpx_count} Trail GPX, " - f"{retro_count} Retroarch, {bgstats_count} BGStats, " - f"{ebird_count} eBird, {scale_count} Scale WebDAV imports" + webdav.import_from_webdav_for_all_users( + restart=restart, + update_retroarch_hash=update_hash, )