[importers] Add bgstats import class
All checks were successful
build & deploy / test (push) Successful in 1m57s
build & deploy / build-and-deploy (push) Successful in 29s

This commit is contained in:
2026-05-23 17:03:17 -04:00
parent dce31ed840
commit a4030e89ec
8 changed files with 201 additions and 24 deletions

View File

@ -7,7 +7,6 @@ import json
from books.koreader import fetch_file_from_webdav
from profiles.models import UserProfile
from scrobbles.models import KoReaderImport, TrailGPXImport
from scrobbles.scrobblers import email_scrobble_board_game
from scrobbles.tasks import process_koreader_import, process_trail_gpx_import
from scrobbles.utils import get_file_md5_hash
from webdav.client import get_webdav_client
@ -290,14 +289,15 @@ def scan_webdav_for_retroarch(webdav_client, user_id):
def scan_webdav_for_bgstats(webdav_client, user_id):
"""Download .bgsplay files from WebDAV, parse JSON, and scrobble board games."""
"""Download .bgsplay files from WebDAV and queue imports for new files."""
from scrobbles.models import BGStatsImport
from scrobbles.tasks import process_bgstats_import
bgstats_path = DEFAULT_BGSTATS_PATH
try:
webdav_client.info(bgstats_path)
except:
logger.info(
"No var/bgstats/ directory on webdav", extra={"user_id": user_id}
)
logger.info("No var/bgstats/ directory on webdav", extra={"user_id": user_id})
return 0
try:
@ -309,38 +309,37 @@ def scan_webdav_for_bgstats(webdav_client, user_id):
)
return 0
processed = 0
seen_this_cycle = set()
new_imports = 0
already_imported = set(
BGStatsImport.objects.filter(user_id=user_id).values_list(
"original_filename", flat=True
)
)
for fname in files:
fname = os.path.basename(fname)
if not fname.lower().endswith(".bgsplay"):
continue
if fname in seen_this_cycle:
if fname in already_imported:
logger.debug(f"Skipping already-imported {fname}")
continue
seen_this_cycle.add(fname)
tmp = tempfile.NamedTemporaryFile(delete=False, suffix=fname)
try:
webdav_client.download_sync(
remote_path=f"{bgstats_path}/{fname}", local_path=tmp.name
)
with open(tmp.name, "r", encoding="utf-8") as f:
parsed_json = json.load(f)
scrobbles = email_scrobble_board_game(parsed_json, user_id)
if scrobbles:
logger.info(
"BG Stats import from %s for user %d created %d scrobble(s)",
fname,
user_id,
len(scrobbles),
)
processed += 1
except Exception as e:
logger.error(
"Failed to import BG Stats file %s: %s", fname, e
imp = BGStatsImport.objects.create(
user_id=user_id,
original_filename=fname,
)
with open(tmp.name, "rb") as f:
imp.bgsplay_file.save(fname, f, save=True)
process_bgstats_import.delay(imp.id)
new_imports += 1
except Exception as e:
logger.error(f"Failed to import BG Stats file {fname}: {e}")
finally:
os.unlink(tmp.name)
return processed
return new_imports