Add twitch support #6

Merged
skeh merged 3 commits from provider/twitch into main 2021-04-08 07:13:28 +00:00
1 changed files with 21 additions and 2 deletions
Showing only changes of commit 83936e01b1 - Show all commits

View File

@ -1,4 +1,5 @@
from hashlib import md5
import re
import pygame
from pygame.locals import Rect
@ -8,6 +9,20 @@ from chats import AUTHOR_TYPES
from . import BG_COLOR, XTERM_COLORS
from .TextFragment import TextFragment
hex_color_regex = re.compile('^#[a-f0-9]{6}$')
def closest_xterm(target_color):
r, g, b = (int(target_color[i:i+2], 16) for i in range(1, len(target_color), 2))
distances = []
for xterm_color in XTERM_COLORS:
xr, xg, xb = xterm_color
distance = math.sqrt(abs(r - xr) ** 2 + abs(g - xg) ** 2 + abs(b - xb) ** 2)
if distance == 0:
return xterm_color
distances.append((distance, xterm_color))
return min(distances)[1]
class MessageView:
# TODO: Superchat and chat sponsor handling
@ -26,8 +41,12 @@ class MessageView:
author_id = message['author_id']
author_type = message['author_type']
author_hash = int(md5(author_id.encode('utf-8')).hexdigest(), 16)
author_color = (author_hash % 144) + 88
author_color = message.get('author_color')
if author_color is not None and hex_color_regex.match(author_color):
author_color = closest_xterm(author_color)
else:
author_hash = int(md5(author_id.encode('utf-8')).hexdigest(), 16)
author_color = (author_hash % 144) + 88
tag_fg = XTERM_COLORS[15]
tag_bg = BG_COLOR