[scrobbles] Add metadata accessor
This commit is contained in:
@ -27,20 +27,20 @@ class JSONMetadata(object):
|
||||
@dataclass
|
||||
class BoardGameScore(JSONMetadata):
|
||||
user_id: int
|
||||
score: Optional[int]
|
||||
win: Optional[bool]
|
||||
score: Optional[int] = None
|
||||
win: Optional[bool] = None
|
||||
|
||||
|
||||
@dataclass
|
||||
class BoardGameMetadata(JSONMetadata):
|
||||
players: Optional[list[BoardGameScore]]
|
||||
players: Optional[list[BoardGameScore]] = None
|
||||
|
||||
|
||||
@dataclass
|
||||
class LifeEventMetadata(JSONMetadata):
|
||||
location: Optional[str]
|
||||
geo_location_id: Optional[int]
|
||||
participant_user_ids: Optional[list[int]]
|
||||
location: Optional[str] = None
|
||||
geo_location_id: Optional[int] = None
|
||||
participant_user_ids: Optional[list[int]] = None
|
||||
|
||||
@property
|
||||
def __dict__(self):
|
||||
|
||||
@ -39,8 +39,11 @@ from videogames import retroarch
|
||||
from videogames.models import VideoGame
|
||||
from videos.models import Series, Video
|
||||
from scrobbles.dataclasses import (
|
||||
BoardGameMetadata,
|
||||
LifeEventMetadata,
|
||||
ScrobbleMetadataDecoder,
|
||||
ScrobbleMetadataEncoder,
|
||||
VideoMetadata,
|
||||
)
|
||||
from webpages.models import WebPage
|
||||
from lifeevents.models import LifeEvent
|
||||
@ -602,6 +605,25 @@ class Scrobble(TimeStampedModel):
|
||||
},
|
||||
)
|
||||
|
||||
@property
|
||||
def metadata(self):
|
||||
metadata_cls = None
|
||||
if self.media_type == self.MediaType.LIFE_EVENT:
|
||||
metadata_cls = LifeEventMetadata
|
||||
if self.media_type == self.MediaType.BOARD_GAME:
|
||||
metadata_cls = BoardGameMetadata
|
||||
if self.media_type == self.MediaType.VIDEO:
|
||||
metadata_cls = VideoMetadata
|
||||
|
||||
if not metadata_cls:
|
||||
logger.warn(
|
||||
f"Media type has no metadata class",
|
||||
extra={"media_type": self.media_type, "scrobble_id": self.id},
|
||||
)
|
||||
return None
|
||||
|
||||
return metadata_cls(**self.log)
|
||||
|
||||
@property
|
||||
def tzinfo(self):
|
||||
return pytz.timezone(self.timezone)
|
||||
|
||||
Reference in New Issue
Block a user