[scrobbles] Fix scrobbling duplicate tasks

This commit is contained in:
2024-10-15 21:29:50 -04:00
parent 4a0bac5b87
commit dbaf189628
6 changed files with 33 additions and 21 deletions

View File

@ -31,6 +31,10 @@ class JSONDataclass(JSONWizard):
return json.dumps(self.asdict) return json.dumps(self.asdict)
@dataclass
class ScrobbleLogData(JSONDataclass):
description: Optional[str] = None
class LongPlayLogData(JSONDataclass): class LongPlayLogData(JSONDataclass):
serial_scrobble_id: Optional[int] serial_scrobble_id: Optional[int]
long_play_complete: bool = False long_play_complete: bool = False

View File

@ -87,8 +87,8 @@ class ScrobblableMixin(TimeStampedModel):
@property @property
def logdata_cls(self) -> None: def logdata_cls(self) -> None:
logger.warning("logdata_cls() not implemented yet") from scrobbles.dataclasses import ScrobbleLogData
return None return ScrobbleLogData
@property @property
def subtitle(self) -> str: def subtitle(self) -> str:

View File

@ -612,16 +612,14 @@ class Scrobble(TimeStampedModel):
@property @property
def logdata(self) -> Optional[logdata.JSONDataclass]: def logdata(self) -> Optional[logdata.JSONDataclass]:
if not self.media_obj.logdata_cls: if self.media_obj:
logger.warn( logdata_cls = self.media_obj.logdata_cls
f"Media type has no log data class, you should add one!", else:
extra={"media_type": self.media_type, "scrobble_id": self.id}, logdata_cls = logdata.ScrobbleLogData
)
return None
log_dict = self.log log_dict = self.log
if isinstance(self.log, str): if isinstance(self.log, str):
# There's nothing stopping django from saving a string ina JSONField :( # There's nothing stopping django from saving a string in a JSONField :(
logger.warning( logger.warning(
"[scrobbles] Received string in JSON data in log", "[scrobbles] Received string in JSON data in log",
extra={"log": self.log}, extra={"log": self.log},
@ -631,7 +629,7 @@ class Scrobble(TimeStampedModel):
if not log_dict: if not log_dict:
log_dict = {} log_dict = {}
return self.media_obj.logdata_cls.from_dict(log_dict) return logdata_cls.from_dict(log_dict)
def redirect_url(self, user_id) -> str: def redirect_url(self, user_id) -> str:
user = User.objects.filter(id=user_id).first() user = User.objects.filter(id=user_id).first()

View File

@ -347,7 +347,7 @@ def todoist_scrobble_task(todoist_task: dict, user_id: int) -> Scrobble:
if not prefix and suffix: if not prefix and suffix:
logger.warning( logger.warning(
"Missing a prefix and suffix tag for task", "Missing a prefix and suffix tag for task",
extra={"todoist_task": todoist_task}, extra={"todoist_scrobble_task": todoist_task},
) )
title = " ".join([prefix.capitalize(), suffix.capitalize()]) title = " ".join([prefix.capitalize(), suffix.capitalize()])
@ -359,13 +359,26 @@ def todoist_scrobble_task(todoist_task: dict, user_id: int) -> Scrobble:
log__todoist_id=todoist_task.get("todoist_id"), log__todoist_id=todoist_task.get("todoist_id"),
task=task, task=task,
).last() ).last()
is_no_longer_in_progress = ( in_progress_in_todoist = (
"inprogress" not in todoist_task["todoist_label_list"] "inprogress" in todoist_task["todoist_label_list"]
) )
if in_progress_scrobble if not in_progress_scrobble and not in_progress_in_todoist:
if is_no_longer_in_progress: logger.info(
scrobble = todoist_scrobble_task_finish(todoist_task, user_id) "[todoist_scrobble_task] no task in progress, and no inprogress label found",
return in_progress_scrobble extra={
"todoist_type": todoist_task["todoist_type"],
"todoist_event": todoist_task["todoist_event"],
"todoist_id": todoist_task["todoist_id"],
},
)
return
if in_progress_scrobble and not in_progress_in_todoist:
scrobble = todoist_scrobble_task_finish(todoist_task, user_id)
# TODO this logic probably belongs in create_or_update
if in_progress_scrobble:
return scrobble
# TODO Should use updated_at from TOdoist, but parsing isn't working # TODO Should use updated_at from TOdoist, but parsing isn't working
scrobble_dict = { scrobble_dict = {

View File

@ -3,7 +3,7 @@ TODOIST_TITLE_SUFFIX_LABELS = [
"bug", "bug",
"project", "project",
"errand", "errand",
"chores", "chore",
"admin", "admin",
"meeting", "meeting",
"habit", "habit",

View File

@ -57,9 +57,6 @@ def todoist_webhook(request):
.user_id .user_id
) )
# TODO huge hack, find a way to populate user id from Todoist # TODO huge hack, find a way to populate user id from Todoist
if not user_id:
user_id = 1
scrobble = todoist_scrobble_task(todoist_task, user_id) scrobble = todoist_scrobble_task(todoist_task, user_id)
if not scrobble: if not scrobble: