70 lines
2.5 KiB
Python
70 lines
2.5 KiB
Python
|
import twitchio.ext.commands
|
||
|
import collections
|
||
|
import zmq
|
||
|
|
||
|
from .filters import FFmpegChain, FFmpegFilter, filter_escape
|
||
|
|
||
|
|
||
|
def make_twitch_chat(name, nickname, token, channel, x=200, y=25, addr='tcp://localhost:5555', nick_width=160, nick_args={}, chat_width=300, chat_args={}):
|
||
|
nick_id = 'drawtext@twitch-{}-nicks'.format(name)
|
||
|
chat_id = 'drawtext@twitch-{}-messages'.format(name)
|
||
|
runner = TwitchChatUpdater(nickname=nickname, token=token, channel=channel, nick_target=nick_id, chat_target=chat_id)
|
||
|
return [runner.run], [FFmpegChain(
|
||
|
FFmpegFilter(nick_id,
|
||
|
text='Loading',
|
||
|
expansion='none',
|
||
|
x='{} + max(0, {} - text_w)'.format(x, nick_width),
|
||
|
y=y,
|
||
|
**nick_args
|
||
|
),
|
||
|
FFmpegFilter(chat_id,
|
||
|
text='chat...',
|
||
|
expansion='none',
|
||
|
x='{} + {}'.format(x, nick_width),
|
||
|
y=y,
|
||
|
**chat_args
|
||
|
),
|
||
|
FFmpegFilter('zmq')
|
||
|
)]
|
||
|
|
||
|
def zmq_escape(v):
|
||
|
return (filter_escape(v)
|
||
|
.replace('\\', '\\\\')
|
||
|
.replace('\n', '\\\n')
|
||
|
.replace(' ', '\\ ')
|
||
|
)
|
||
|
|
||
|
class TwitchChatUpdater(twitchio.ext.commands.Bot):
|
||
|
def __init__(self, nickname, token, channel, nick_target, chat_target, addr='tcp://localhost:5555', maxlen=15):
|
||
|
super().__init__(irc_token=token, nick=nickname, initial_channels=[channel], prefix='kwdakwdwajd')
|
||
|
self.zmq_messages = collections.deque(maxlen=maxlen)
|
||
|
self.zmq = zmq.Context()
|
||
|
self.zmq_sock = self.zmq.socket(zmq.REQ)
|
||
|
self.zmq_sock.connect(addr)
|
||
|
self.zmq_nick_target = nick_target
|
||
|
self.zmq_chat_target = chat_target
|
||
|
|
||
|
def zmq_send_command(self, target, command, **args):
|
||
|
cmd = '{} {} {}'.format(
|
||
|
target, command,
|
||
|
':'.join(
|
||
|
'{}={}'.format(k, zmq_escape(v)) for k, v in args.items()
|
||
|
)
|
||
|
).encode('utf-8')
|
||
|
self.zmq_sock.send(cmd)
|
||
|
resp = self.zmq_sock.recv()
|
||
|
|
||
|
def zmq_update(self):
|
||
|
nick_text = '\n'.join('<{}>'.format(m.author.name) for m in self.zmq_messages if m)
|
||
|
self.zmq_send_command(self.zmq_nick_target, 'reinit', text=nick_text)
|
||
|
chat_text = '\n'.join(m.content for m in self.zmq_messages if m)
|
||
|
self.zmq_send_command(self.zmq_chat_target, 'reinit', text=chat_text)
|
||
|
|
||
|
async def event_ready(self):
|
||
|
self.zmq_update()
|
||
|
|
||
|
async def event_message(self, message):
|
||
|
print(message.tags)
|
||
|
self.zmq_messages.append(message)
|
||
|
self.zmq_update()
|