diff --git a/database/models.py b/database/models.py index 5590b51..ed24f51 100644 --- a/database/models.py +++ b/database/models.py @@ -21,7 +21,8 @@ class GameBundle(db.Model): class LiveAlert(db.Model): id = db.Column(db.Integer, primary_key=True) - enable = db.Column(db.Boolean, default=False) + enable = db.Column(db.Boolean, default=True) + online = db.Column(db.Boolean, default=False) login = db.Column(db.String(128)) notify_channel = db.Column(db.Integer) message = db.Column(db.String(2000)) diff --git a/database/schema.sql b/database/schema.sql index 0989286..2777bba 100644 --- a/database/schema.sql +++ b/database/schema.sql @@ -25,6 +25,7 @@ CREATE TABLE IF NOT EXISTS `humeur` ( CREATE TABLE IF NOT EXISTS live_alert ( id INTEGER PRIMARY KEY AUTOINCREMENT, `enable` BOOLEAN NOT NULL DEFAULT TRUE, + `online` BOOLEAN NOT NULL DEFAULT FALSE, `login` VARCHAR(128) UNIQUE NOT NULL, `notify_channel` INTEGER NOT NULL, `message` VARCHAR(2000) NOT NULL diff --git a/twitchbot/__init__.py b/twitchbot/__init__.py index 061d8b8..529bf0d 100644 --- a/twitchbot/__init__.py +++ b/twitchbot/__init__.py @@ -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()) diff --git a/twitchbot/live_alert.py b/twitchbot/live_alert.py new file mode 100644 index 0000000..1e41db2 --- /dev/null +++ b/twitchbot/live_alert.py @@ -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 + diff --git a/webapp/static/css/style.css b/webapp/static/css/style.css index 05fd81a..25a3d21 100644 --- a/webapp/static/css/style.css +++ b/webapp/static/css/style.css @@ -7,9 +7,11 @@ table td { text-align: left; vertical-align: top; overflow: hidden; - text-overflow: ellipsis; white-space: normal; - max-width: 250px; +} + +table.live-alert tr td:last-child { + white-space: nowrap; } a.icon { diff --git a/webapp/templates/live-alert.html b/webapp/templates/live-alert.html index d3f8753..ea02988 100644 --- a/webapp/templates/live-alert.html +++ b/webapp/templates/live-alert.html @@ -5,13 +5,12 @@ {% if not alert %}
| Chaine | Canal | Message | -Actif | # | {{alert.message}} | {{ '✅' if alert.enable else '❌' }} - | -
✐
🗑
@@ -53,7 +50,15 @@
La chaine est le login de la chaine, par exemple chainesteve pour https://www.twitch.tv/chainesteve.
- Pour le message vous avez acces à ces variables : TODO + Pour le message vous avez acces à ces variables : +
|
|---|