From 941d6c4fe60a0da8335fe6cac9855705736b3ac3 Mon Sep 17 00:00:00 2001 From: Bulat Kurbanov Date: Mon, 18 Nov 2024 00:30:09 +0100 Subject: [PATCH] Check streams states from tasks --- src/modules/stream_notifications/tasks.py | 24 +++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/src/modules/stream_notifications/tasks.py b/src/modules/stream_notifications/tasks.py index c61104a..eed416a 100644 --- a/src/modules/stream_notifications/tasks.py +++ b/src/modules/stream_notifications/tasks.py @@ -1,7 +1,11 @@ +from datetime import datetime, timezone + from core.broker import broker +from repositories.streamers import StreamerConfigRepository from .state import State from .watcher import StateWatcher +from .twitch.authorize import authorize @broker.task("stream_notifications.twitch.on_stream_state_change") @@ -9,3 +13,23 @@ async def on_stream_state_change( streamer_id: int, new_state: State | None = None ): await StateWatcher.on_stream_state_change(streamer_id, new_state) + + +@broker.task( + "stream_notifications.check_streams_states", + schedule=[{"cron": "*/2 * * * *"}] +) +async def check_streams_states(): + streamers = await StreamerConfigRepository.all() + streamers_ids = [str(streamer.twitch.id) for streamer in streamers] + + twitch = await authorize() + + async for stream in twitch.get_streams(user_id=streamers_ids): + state = State( + title=stream.title, + category=stream.game_name, + last_live_at=datetime.now(timezone.utc) + ) + + await StateWatcher.on_stream_state_change(int(stream.user_id), state)