mirror of
https://github.com/kurbezz/discord-bot.git
synced 2025-12-08 09:30:44 +01:00
Check streams states from tasks
This commit is contained in:
@@ -1,7 +1,11 @@
|
|||||||
|
from datetime import datetime, timezone
|
||||||
|
|
||||||
from core.broker import broker
|
from core.broker import broker
|
||||||
|
from repositories.streamers import StreamerConfigRepository
|
||||||
|
|
||||||
from .state import State
|
from .state import State
|
||||||
from .watcher import StateWatcher
|
from .watcher import StateWatcher
|
||||||
|
from .twitch.authorize import authorize
|
||||||
|
|
||||||
|
|
||||||
@broker.task("stream_notifications.twitch.on_stream_state_change")
|
@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
|
streamer_id: int, new_state: State | None = None
|
||||||
):
|
):
|
||||||
await StateWatcher.on_stream_state_change(streamer_id, new_state)
|
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)
|
||||||
|
|||||||
Reference in New Issue
Block a user