Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
wiseplat
GitHub Repository: wiseplat/python-code
Path: blob/master/ invest-robot-contest_TinkoffBotTwitch-main/venv/lib/python3.8/site-packages/twitchio/parse.py
7774 views
1
# -*- coding: utf-8 -*-
2
3
"""
4
The MIT License (MIT)
5
6
Copyright (c) 2017-2021 TwitchIO
7
8
Permission is hereby granted, free of charge, to any person obtaining a
9
copy of this software and associated documentation files (the "Software"),
10
to deal in the Software without restriction, including without limitation
11
the rights to use, copy, modify, merge, publish, distribute, sublicense,
12
and/or sell copies of the Software, and to permit persons to whom the
13
Software is furnished to do so, subject to the following conditions:
14
15
The above copyright notice and this permission notice shall be included in
16
all copies or substantial portions of the Software.
17
18
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19
OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
23
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
24
DEALINGS IN THE SOFTWARE.
25
"""
26
27
import re
28
import typing
29
30
if typing.TYPE_CHECKING:
31
from .websocket import WSConnection
32
33
ACTIONS = (
34
"JOIN",
35
"PART",
36
"PING",
37
"PRIVMSG",
38
"PRIVMSG(ECHO)",
39
"USERSTATE",
40
"MODE",
41
"RECONNECT",
42
"WHISPER",
43
"USERNOTICE",
44
)
45
ACTIONS2 = ("USERSTATE", "ROOMSTATE", "PRIVMSG", "USERNOTICE", "WHISPER")
46
USER_SUB = re.compile(r":(?P<user>.*)!")
47
TMI = "tmi.twitch.tv"
48
49
50
def parser(data: str, nick: str):
51
groups = data.split()
52
action = groups[1] if groups[1] == "JOIN" else groups[-2]
53
channel = None
54
message = None
55
user = None
56
badges = None
57
58
if action == "PING":
59
return dict(action="PING")
60
61
elif groups[2] in {"PRIVMSG", "PRIVMSG(ECHO)"}:
62
action = groups[2]
63
channel = groups[3].lstrip("#")
64
message = " ".join(groups[4:]).lstrip(":")
65
user = re.search(USER_SUB, groups[1]).group("user")
66
67
elif groups[2] == "WHISPER":
68
action = groups[2]
69
message = " ".join(groups[4:]).lstrip(":")
70
user = re.search(USER_SUB, groups[1]).group("user")
71
72
elif groups[2] == "USERNOTICE":
73
action = groups[2]
74
channel = groups[3].lstrip("#")
75
message = " ".join(groups[4:]).lstrip(":")
76
77
elif action in ACTIONS:
78
channel = groups[-1].lstrip("#")
79
80
elif groups[3] in {"PRIVMSG", "PRIVMSG(ECHO)"}:
81
action = groups[3]
82
channel = groups[4].lstrip("#")
83
message = " ".join(groups[5:]).lstrip(":")
84
user = re.search(USER_SUB, groups[2]).group("user")
85
86
if action in ACTIONS2:
87
prebadge = groups[0].split(";")
88
badges = {}
89
90
for badge in prebadge:
91
badge = badge.split("=")
92
93
try:
94
badges[badge[0]] = badge[1]
95
except IndexError:
96
pass
97
98
if action not in ACTIONS and action not in ACTIONS2:
99
action = None
100
101
if not user:
102
try:
103
user = re.search(USER_SUB, groups[0]).group("user")
104
except (AttributeError, ValueError):
105
pass
106
107
try:
108
code = int(groups[1])
109
except ValueError:
110
code = 0
111
112
batches = []
113
if code == 353:
114
if not channel:
115
channel = groups[4].lstrip("#")
116
117
for b in groups[5:-1]:
118
b = b.lstrip(":")
119
120
if "\r\n:" in b:
121
batches.append(b.split("\r\n:")[0])
122
break
123
else:
124
batches.append(b)
125
126
return dict(
127
data=data,
128
nick=nick,
129
groups=groups,
130
action=action,
131
channel=channel,
132
user=user,
133
badges=badges,
134
code=code,
135
message=message,
136
batches=batches,
137
)
138
139
140
def parse(data: str, ws: "WSConnection"):
141
messages = data.split("\r\n")
142
output = []
143
144
for msg in messages:
145
if not msg:
146
continue
147
148
if msg == "PING :tmi.twitch.tv":
149
output.append(dict(action="PING"))
150
continue
151
152
msg = msg.replace(":tmi.twitch.tv ", "")
153
groups = msg.split()
154
length = len(groups)
155
156