Basic plugins support #5

Merged
skeh merged 4 commits from plugins into main 2021-04-08 01:55:48 +00:00
4 changed files with 44 additions and 30 deletions
Showing only changes of commit 9e8d05fc90 - Show all commits

View File

@ -8,9 +8,11 @@ import time
class GracefulShutdownException(Exception): class GracefulShutdownException(Exception):
pass pass
class LIFECYCLE_MESSAGES(Enum): class LIFECYCLE_MESSAGES(Enum):
SHUTDOWN = auto() SHUTDOWN = auto()
class ChatProcess(Process, ABC): class ChatProcess(Process, ABC):
def __init__(self, event_poll_frequency): def __init__(self, event_poll_frequency):
super().__init__() super().__init__()
@ -22,6 +24,10 @@ class ChatProcess(Process, ABC):
self._state = None self._state = None
self._next_state = None self._next_state = None
@abstractmethod
def keybinds(self):
return []
@abstractmethod @abstractmethod
def loop(self, next_state): def loop(self, next_state):
pass pass

View File

@ -1,4 +1,3 @@
import time
import random import random
from enum import Enum, auto from enum import Enum, auto
@ -23,6 +22,12 @@ class FakeChat(ChatProcess):
self._max_messages_per_chunk = 1 self._max_messages_per_chunk = 1
self._max_delay = 10 self._max_delay = 10
@property
def keybinds(self):
return {
ord('f'): {'type': CONTROL_MESSAGES.START_STOP}
}
def process_messages(self, message_type, args, next_state): def process_messages(self, message_type, args, next_state):
if message_type == CONTROL_MESSAGES.START_STOP: if message_type == CONTROL_MESSAGES.START_STOP:
running = not self.state == STATES.RUNNING running = not self.state == STATES.RUNNING

View File

@ -39,22 +39,10 @@ class YoutubeLiveProcess(ChatProcess):
self._live_chat_id = None self._live_chat_id = None
self._page_token = None self._page_token = None
@classmethod @property
def normalize_message(cls, message): def keybinds(self):
if message['authorDetails']['isChatOwner']:
author_type = AUTHOR_TYPES.OWNER
elif message['authorDetails']['isChatModerator']:
author_type = AUTHOR_TYPES.MODERATOR
elif message['authorDetails']['isChatSponsor']:
author_type = AUTHOR_TYPES.PATRON
else:
author_type = AUTHOR_TYPES.USER
return { return {
'text': message['snippet']['displayMessage'], ord('r'): {'type': CONTROL_MESSAGES.RESTART}
'author_name': message['authorDetails']['displayName'],
'author_id': message['authorDetails']['channelId'],
'author_type': author_type,
} }
def loop(self, next_state): def loop(self, next_state):
@ -156,6 +144,24 @@ class YoutubeLiveProcess(ChatProcess):
}) })
return None, None return None, None
@classmethod
def normalize_message(cls, message):
if message['authorDetails']['isChatOwner']:
author_type = AUTHOR_TYPES.OWNER
elif message['authorDetails']['isChatModerator']:
author_type = AUTHOR_TYPES.MODERATOR
elif message['authorDetails']['isChatSponsor']:
author_type = AUTHOR_TYPES.PATRON
else:
author_type = AUTHOR_TYPES.USER
return {
'text': message['snippet']['displayMessage'],
'author_name': message['authorDetails']['displayName'],
'author_id': message['authorDetails']['channelId'],
'author_type': author_type,
}
def request_oauth_consent(self): def request_oauth_consent(self):
params = { params = {
'client_id': self._client_secrets['client_id'], 'client_id': self._client_secrets['client_id'],

View File

@ -13,17 +13,17 @@ from chats import FakeChat, YoutubeLive
class App(Process): class App(Process):
def __init__(self, chat_processes): def __init__(self, chat_processes):
super().__init__() super().__init__()
self._running = False self._chat_processes = chat_processes
self._display_surf = None
self._title = 'Chat monitor'
self._chat_processes = {proc.__class__: proc for proc in chat_processes} self.size = self.width, self.height = 500, 1200
self._log_scroll = 0 self._title = 'Chat monitor'
self._scroll_speed = 20 self._scroll_speed = 20
self._max_fps = 60 self._max_fps = 60
self.chat_log = deque(maxlen=100) self.chat_log = deque(maxlen=100)
self.size = self.width, self.height = 500, 1200 self._log_scroll = 0
self._running = False
self._display_surf = None
@property @property
def deamon(self): def deamon(self):
@ -38,14 +38,11 @@ class App(Process):
def on_event(self, event): def on_event(self, event):
if event.type == pygame.QUIT: if event.type == pygame.QUIT:
self._running = False self._running = False
elif event.type == pygame.KEYDOWN and event.key == ord('r'): elif event.type == pygame.KEYDOWN:
self._chat_processes[YoutubeLive.YoutubeLiveProcess].control_pipe.send( for process in self._chat_processes:
{'type': YoutubeLive.CONTROL_MESSAGES.RESTART} bound_action = process.keybinds.get(event.key)
) if bound_action is not None:
elif event.type == pygame.KEYDOWN and event.key == ord('f'): process.control_pipe.send(bound_action)
self._chat_processes[FakeChat.FakeChat].control_pipe.send(
{'type': FakeChat.CONTROL_MESSAGES.START_STOP}
)
def tick(self, dt): def tick(self, dt):
for message in self.chat_log: for message in self.chat_log: