[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

@ -455,6 +455,53 @@ class RetroarchImport(BaseFileImportMixin):
self.mark_finished()
class BGStatsImport(BaseFileImportMixin):
class Meta:
verbose_name = "BG Stats Import"
@property
def import_type(self) -> str:
return "BG Stats"
def get_absolute_url(self):
return reverse("scrobbles:bgstats-import-detail", kwargs={"slug": self.uuid})
def get_path(instance, filename):
extension = filename.split(".")[-1]
uuid = instance.uuid
return f"bgstats-uploads/{uuid}.{extension}"
@property
def upload_file_path(self):
if getattr(settings, "USE_S3_STORAGE"):
path = self.bgsplay_file.url
else:
path = self.bgsplay_file.path
return path
bgsplay_file = models.FileField(upload_to=get_path, **BNULL)
original_filename = models.CharField(max_length=255, **BNULL)
def process(self, force=False):
"""Import scrobbles from a single BG Stats bgsplay file"""
if self.processed_finished and not force:
logger.info(f"{self} already processed on {self.processed_finished}")
return
self.mark_started()
import json
from scrobbles.scrobblers import email_scrobble_board_game
with open(self.upload_file_path, "r", encoding="utf-8") as f:
parsed_json = json.load(f)
scrobbles = email_scrobble_board_game(parsed_json, self.user_id)
self.record_log(scrobbles)
self.mark_finished()
class ScrobbleQuerySet(models.QuerySet):
def with_related(self):
return self.select_related("user").prefetch_related(