Path: blob/master/ invest-robot-contest_TinkoffBotTwitch-main/venv/lib/python3.8/site-packages/twitchio/parse.py
7774 views
# -*- coding: utf-8 -*-12"""3The MIT License (MIT)45Copyright (c) 2017-2021 TwitchIO67Permission is hereby granted, free of charge, to any person obtaining a8copy of this software and associated documentation files (the "Software"),9to deal in the Software without restriction, including without limitation10the rights to use, copy, modify, merge, publish, distribute, sublicense,11and/or sell copies of the Software, and to permit persons to whom the12Software is furnished to do so, subject to the following conditions:1314The above copyright notice and this permission notice shall be included in15all copies or substantial portions of the Software.1617THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS18OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,19FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE20AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER21LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING22FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER23DEALINGS IN THE SOFTWARE.24"""2526import re27import typing2829if typing.TYPE_CHECKING:30from .websocket import WSConnection3132ACTIONS = (33"JOIN",34"PART",35"PING",36"PRIVMSG",37"PRIVMSG(ECHO)",38"USERSTATE",39"MODE",40"RECONNECT",41"WHISPER",42"USERNOTICE",43)44ACTIONS2 = ("USERSTATE", "ROOMSTATE", "PRIVMSG", "USERNOTICE", "WHISPER")45USER_SUB = re.compile(r":(?P<user>.*)!")46TMI = "tmi.twitch.tv"474849def parser(data: str, nick: str):50groups = data.split()51action = groups[1] if groups[1] == "JOIN" else groups[-2]52channel = None53message = None54user = None55badges = None5657if action == "PING":58return dict(action="PING")5960elif groups[2] in {"PRIVMSG", "PRIVMSG(ECHO)"}:61action = groups[2]62channel = groups[3].lstrip("#")63message = " ".join(groups[4:]).lstrip(":")64user = re.search(USER_SUB, groups[1]).group("user")6566elif groups[2] == "WHISPER":67action = groups[2]68message = " ".join(groups[4:]).lstrip(":")69user = re.search(USER_SUB, groups[1]).group("user")7071elif groups[2] == "USERNOTICE":72action = groups[2]73channel = groups[3].lstrip("#")74message = " ".join(groups[4:]).lstrip(":")7576elif action in ACTIONS:77channel = groups[-1].lstrip("#")7879elif groups[3] in {"PRIVMSG", "PRIVMSG(ECHO)"}:80action = groups[3]81channel = groups[4].lstrip("#")82message = " ".join(groups[5:]).lstrip(":")83user = re.search(USER_SUB, groups[2]).group("user")8485if action in ACTIONS2:86prebadge = groups[0].split(";")87badges = {}8889for badge in prebadge:90badge = badge.split("=")9192try:93badges[badge[0]] = badge[1]94except IndexError:95pass9697if action not in ACTIONS and action not in ACTIONS2:98action = None99100if not user:101try:102user = re.search(USER_SUB, groups[0]).group("user")103except (AttributeError, ValueError):104pass105106try:107code = int(groups[1])108except ValueError:109code = 0110111batches = []112if code == 353:113if not channel:114channel = groups[4].lstrip("#")115116for b in groups[5:-1]:117b = b.lstrip(":")118119if "\r\n:" in b:120batches.append(b.split("\r\n:")[0])121break122else:123batches.append(b)124125return dict(126data=data,127nick=nick,128groups=groups,129action=action,130channel=channel,131user=user,132badges=badges,133code=code,134message=message,135batches=batches,136)137138139def parse(data: str, ws: "WSConnection"):140messages = data.split("\r\n")141output = []142143for msg in messages:144if not msg:145continue146147if msg == "PING :tmi.twitch.tv":148output.append(dict(action="PING"))149continue150151msg = msg.replace(":tmi.twitch.tv ", "")152groups = msg.split()153length = len(groups)154155156