Listen to watch status events to call simkl

This commit is contained in:
Zoe Roux 2024-03-20 20:41:15 +01:00
parent c6f12ab2a8
commit e1f889f862
No known key found for this signature in database
2 changed files with 32 additions and 3 deletions

View File

@ -1,17 +1,24 @@
import json
import os
import pika
from pika import spec
from pika.adapters.blocking_connection import BlockingChannel
import pika.credentials
from autosync.services.simkl import Simkl
def callback(
# TODO: declare multiples services
service = Simkl()
def on_message(
ch: BlockingChannel,
method: spec.Basic.Deliver,
properties: spec.BasicProperties,
body: bytes,
):
print(f" [x] {method.routing_key}:{body}")
status = json.loads(body)
service.update(status.user, status.resource, status)
def main():
@ -31,5 +38,7 @@ def main():
queue_name = result.method.queue
channel.queue_bind(exchange="events.watched", queue=queue_name, routing_key="#")
channel.basic_consume(queue=queue_name, on_message_callback=callback, auto_ack=True)
channel.basic_consume(
queue=queue_name, on_message_callback=on_message, auto_ack=True
)
channel.start_consuming()

View File

@ -0,0 +1,20 @@
from dataclasses import dataclass
from autosync.models.episode import Episode
from autosync.models.movie import Movie
from autosync.models.show import Show
from autosync.models.user import User
from autosync.models.watch_status import WatchStatus
@dataclass
class WatchStatusMessage(WatchStatus):
user: User
resource: Movie | Show | Episode
@dataclass
class Message:
action: str
type: str
value: WatchStatusMessage