-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathChatMessage.py
88 lines (68 loc) · 3.45 KB
/
ChatMessage.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
import textwrap
# Message sent by viewer
class ChatMessage():
def __init__(self, obj, baseDraw, fonts, chatWidth, videoHeight):
self.time = int(obj["content_offset_seconds"] * 1000)
self.nick = obj["commenter"]["display_name"] if obj["commenter"]["display_name"] is not None else obj["commenter"]["name"]
try:
self.color = self.adjustColor(obj["message"]["user_color"])
except KeyError:
self.color = self.generateColor()
self.rawMessage = obj["message"]["body"]
self.message = self.wrapMessage(baseDraw, fonts, chatWidth)
self.dimensions = self.getDimensions(baseDraw, fonts)
self.timeToNext = 0
# All messages start below the bottom of the video
self.currentY = videoHeight
# Adjust nick color so it's not too dark
def adjustColor(self, color):
# Grab each color value
r = int(color[1:3], 16)
g = int(color[3:5], 16)
b = int(color[5:7], 16)
# Return directly if it's bright enough
if r > 20 or g > 20 or b > 20:
return color
# Otherwise, just set the red channel to 20 to make sure it's visible
return "#{:0>2X}{:0>2X}{:0>2X}".format(20, g, b)
# Generate color based on nick
def generateColor(self):
r, g, b = 0, 0, 0
for letter in self.nick:
r = ord(letter) % 255
g = ord(letter) * 100 % 255
b = ord(letter) * 200 % 255
color = self.adjustColor("#{:0>2X}{:0>2X}{:0>2X}".format(r, g, b))
return color
# Wrap message so it fits on screen
def wrapMessage(self, baseDraw, fonts, chatWidth):
maximumTextLength = 0
wrappedMessage = None
previousWrappedMessage = None
# Try to have as many characters as possible on a line until it exceeds the chat's length
while True:
maximumTextLength += 5
previousWrappedMessage = wrappedMessage
wrappedMessage = textwrap.fill(self.rawMessage, width = maximumTextLength)
widthMessage, heightMessage = baseDraw.textsize(wrappedMessage, fonts["verdana"])
# We exceeded the allowed length
if widthMessage >= (chatWidth - 20):
return previousWrappedMessage
# We already have everything on a single line
if "\n" not in wrappedMessage:
return wrappedMessage
# Get nick and message heights and widths
def getDimensions(self, baseDraw, fonts):
widthNick, heightNick = baseDraw.textsize(self.nick, fonts["verdanaBold"])
widthMessage, heightMessage = baseDraw.textsize(self.message, fonts["verdana"])
dimensions = {"nick": {}, "message": {}}
dimensions["nick"] = {"width": widthNick, "height": heightNick}
dimensions["message"] = {"width": widthMessage, "height": heightMessage}
dimensions["total"] = {"height": heightNick + heightMessage}
return dimensions
def __str__(self):
return "[{time}/{timeToNext}] {nick}({color}) [{nickWidth}x{nickHeight}]:\n[{messageWidth}x{messageHeight}]\n\"{message}\"".format(
time = self.time, timeToNext = self.timeToNext, nick = self.nick, color = self.color,
nickWidth = self.dimensions["nick"]["width"], nickHeight = self.dimensions["nick"]["height"],
messageWidth = self.dimensions["message"]["width"], messageHeight = self.dimensions["message"]["height"],
message = self.message)