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):
pass
class LIFECYCLE_MESSAGES(Enum):
SHUTDOWN = auto()
class ChatProcess(Process, ABC):
def __init__(self, event_poll_frequency):
super().__init__()
@ -22,6 +24,10 @@ class ChatProcess(Process, ABC):
self._state = None
self._next_state = None
@abstractmethod
def keybinds(self):
return []
@abstractmethod
def loop(self, next_state):
pass

View File

@ -1,4 +1,3 @@
import time
import random
from enum import Enum, auto
@ -23,6 +22,12 @@ class FakeChat(ChatProcess):
self._max_messages_per_chunk = 1
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):
if message_type == CONTROL_MESSAGES.START_STOP:
running = not self.state == STATES.RUNNING

View File

@ -39,22 +39,10 @@ class YoutubeLiveProcess(ChatProcess):
self._live_chat_id = None
self._page_token = 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
@property
def keybinds(self):
return {
'text': message['snippet']['displayMessage'],
'author_name': message['authorDetails']['displayName'],
'author_id': message['authorDetails']['channelId'],
'author_type': author_type,
ord('r'): {'type': CONTROL_MESSAGES.RESTART}
}
def loop(self, next_state):
@ -156,6 +144,24 @@ class YoutubeLiveProcess(ChatProcess):
})
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):
params = {
'client_id': self._client_secrets['client_id'],

View File

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