mirror of
https://github.com/skylanix/MamieHenriette.git
synced 2026-02-06 06:40:35 +01:00
Alerte de live
This commit is contained in:
@@ -7,7 +7,7 @@ from twitchAPI.type import AuthScope, ChatEvent
|
||||
from twitchAPI.chat import Chat, ChatEvent, ChatMessage, EventData
|
||||
|
||||
from database.helpers import ConfigurationHelper
|
||||
|
||||
from twitchbot.live_alert import checkOnlineStreamer
|
||||
from webapp import webapp
|
||||
|
||||
USER_SCOPE = [AuthScope.CHAT_READ, AuthScope.CHAT_EDIT]
|
||||
@@ -53,13 +53,12 @@ class TwitchBot() :
|
||||
async def _checkOnlineStreamers(self):
|
||||
# pas bon faudrait faire un truc mieux
|
||||
while True :
|
||||
async for stream in self.twitch.get_streams(user_login=['chainesteve']):
|
||||
print(stream.user_id)
|
||||
try:
|
||||
await checkOnlineStreamer(self.twitch)
|
||||
except Exception as e:
|
||||
logging.error(f'Erreur lors lors du check des streamers online : {e}')
|
||||
# toutes les 5 minutes
|
||||
await asyncio.sleep(5*60)
|
||||
|
||||
|
||||
|
||||
|
||||
def begin(self):
|
||||
asyncio.run(self._connect())
|
||||
|
||||
36
twitchbot/live_alert.py
Normal file
36
twitchbot/live_alert.py
Normal file
@@ -0,0 +1,36 @@
|
||||
from twitchAPI.twitch import Twitch
|
||||
from twitchAPI.object.api import Stream
|
||||
|
||||
from database import db
|
||||
from database.models import LiveAlert
|
||||
from discordbot import bot
|
||||
from webapp import webapp
|
||||
|
||||
|
||||
async def checkOnlineStreamer(twitch: Twitch) :
|
||||
with webapp.app_context() :
|
||||
alerts : list[LiveAlert] = LiveAlert.query.filter_by(enable = True).all()
|
||||
streams = await _retreiveStreams(twitch, alerts)
|
||||
for alert in alerts :
|
||||
stream = next((s for s in streams if s.user_login == alert.login), None)
|
||||
if stream :
|
||||
if not alert.online :
|
||||
await _notifyAlert(alert, stream)
|
||||
alert.online = True
|
||||
else :
|
||||
alert.online = False
|
||||
db.session.commit()
|
||||
|
||||
async def _notifyAlert(alert : LiveAlert, stream : Stream):
|
||||
message : str = alert.message.format(stream)
|
||||
bot.loop.create_task(_sendMessage(alert.notify_channel, message))
|
||||
|
||||
async def _sendMessage(channel : int, message : str) :
|
||||
await bot.get_channel(channel).send(message)
|
||||
|
||||
async def _retreiveStreams(twitch: Twitch, alerts : list[LiveAlert]) -> list[Stream] :
|
||||
streams : list[Stream] = []
|
||||
async for stream in twitch.get_streams(user_login = [alert.login for alert in alerts]):
|
||||
streams.append(stream)
|
||||
return streams
|
||||
|
||||
Reference in New Issue
Block a user