[tasks] First try at adding an emacs webhook
This commit is contained in:
@ -500,6 +500,95 @@ def todoist_scrobble_task(
|
||||
return scrobble
|
||||
|
||||
|
||||
def emacs_scrobble_task(
|
||||
task_data: dict,
|
||||
user_id: int,
|
||||
started: bool = False,
|
||||
stopped: bool = False,
|
||||
) -> Scrobble | None:
|
||||
|
||||
prefix = ""
|
||||
suffix = ""
|
||||
source_id = task_data.get("source_id")
|
||||
for label in task_data.get("labels"):
|
||||
if label in TODOIST_TITLE_PREFIX_LABELS:
|
||||
prefix = label
|
||||
if label in TODOIST_TITLE_SUFFIX_LABELS:
|
||||
suffix = label
|
||||
|
||||
if not prefix and suffix:
|
||||
logger.warning(
|
||||
"Missing a prefix and suffix tag for task",
|
||||
extra={"emacs_scrobble_task": task_data},
|
||||
)
|
||||
|
||||
title = " ".join([prefix.capitalize(), suffix.capitalize()])
|
||||
|
||||
task = Task.find_or_create(title)
|
||||
|
||||
timestamp = pendulum.parse(task_data.get("updated_at", timezone.now()))
|
||||
in_progress_scrobble = Scrobble.objects.filter(
|
||||
user_id=user_id,
|
||||
in_progress=True,
|
||||
log__source_id=source_id,
|
||||
log__source=task_data.get("source"),
|
||||
task=task,
|
||||
).last()
|
||||
print(in_progress_scrobble)
|
||||
|
||||
if not in_progress_scrobble and stopped:
|
||||
logger.info(
|
||||
"[emacs_scrobble_task] cannot stop already stopped task",
|
||||
extra={
|
||||
"emacs_id": source_id,
|
||||
},
|
||||
)
|
||||
return
|
||||
|
||||
if in_progress_scrobble and started:
|
||||
logger.info(
|
||||
"[todoist_scrobble_task] cannot start already started task",
|
||||
extra={
|
||||
"emacs_id": source_id,
|
||||
},
|
||||
)
|
||||
return in_progress_scrobble
|
||||
|
||||
# Finish an in-progress scrobble
|
||||
if in_progress_scrobble and stopped:
|
||||
logger.info(
|
||||
"[emacs_scrobble_task] finishing",
|
||||
extra={
|
||||
"emacs_id": emacs_task["emacs_id"],
|
||||
},
|
||||
)
|
||||
in_progress_scrobble.stop(timestamp=timestamp, force_finish=True)
|
||||
return in_progress_scrobble
|
||||
|
||||
if in_progress_scrobble:
|
||||
return in_progress_scrobble
|
||||
|
||||
scrobble_dict = {
|
||||
"user_id": user_id,
|
||||
"timestamp": timestamp,
|
||||
"playback_position_seconds": 0,
|
||||
"source": "Org-mode",
|
||||
"log": task_data,
|
||||
}
|
||||
|
||||
logger.info(
|
||||
"[emacs_scrobble_task] creating",
|
||||
extra={
|
||||
"task_id": task.id,
|
||||
"user_id": user_id,
|
||||
"scrobble_dict": scrobble_dict,
|
||||
"media_type": Scrobble.MediaType.TASK,
|
||||
},
|
||||
)
|
||||
scrobble = Scrobble.create_or_update(task, user_id, scrobble_dict)
|
||||
return scrobble
|
||||
|
||||
|
||||
def manual_scrobble_task(url: str, user_id: int, action: Optional[str] = None):
|
||||
source_id = re.findall(r"\d+", url)[0]
|
||||
|
||||
|
||||
Reference in New Issue
Block a user