[英]In Python, how can I filter just the user and message in Twitch API's WHISPER command using regex?
I have this working well for PRIVMSG in main chat, however Twitch's WHISPER command is driving me a bit nuts - it includes a ton of extra information. 我在主聊天中对于PRIVMSG来说工作得很好,但是Twitch的WHISPER命令使我有点发疯-它包含大量的额外信息。
As an example, for PRIVMSG I have this working: 例如,对于PRIVMSG,我可以进行以下工作:
CHAT_MSG=re.compile(r"^:\w+!\w+@\w+\.tmi\.twitch\.tv PRIVMSG #\w+ :")
However, WHISPER returns this: 但是,WHISPER返回以下内容:
badges: @badges=;color=;display-name=;emotes=;message-id=34;thread-id=5575526_123681740;turbo=0;user-id=5575526;user-type= :teonnyn!teonnyn@teonnyn.tmi.twitch.tv WHISPER blastweb :Hello Bot
While PRIVMSG returns this: 虽然PRIVMSG返回此:
teonnyn!teonnyn@teonnyn.tmi.twitch.tv PRIVMSG #blastweb :Hello Bot
PRIVMSG - the public connection, uses this to parse the chat from public: PRIVMSG-公共连接,使用它来解析来自公共的聊天:
username = re.search(r"\w+", channelResponse).group(0)
message = CHAT_MSG.sub("", channelResponse)
print(username + ": " + message)
The same in WHISPER just returns the full "badges+" block of API information. WHISPER中的相同内容仅返回API信息的完整“徽章+”块。 What would the best way be to parse out all the extra information and get just the username and message for WHISPER? 解析所有额外信息并仅获取WHISPER的用户名和消息的最佳方法是什么?
I'm ultimately trying to reach just: teonnyn: Hello Bot
我最终只是想达到以下目标: teonnyn: Hello Bot
Following regex returns two matches - username and the message: 以下正则表达式返回两个匹配项-用户名和消息:
user-type=\\s+:(\\w+)!.*:([\\S\\s]+)
Here is working IDEONE DEMO : 这是正在工作的IDEONE DEMO :
>>> import re
>>> s = "badges: @badges=;color=;display-name=;emotes=;message-id=34;thread-id=5575526_123681740;turbo=0;user-id=5575526;user-type= :teonnyn!teonnyn@teonnyn.tmi.twitch.tv WHISPER blastweb :Hello Bot"
>>> re.findall(r'user-type=\s+:(\w+)!.*:([\S\s]+)', s)
[('teonnyn', 'Hello Bot')]
Your string is delimited, try to use that as your advantage: 您的字符串是定界的,请尝试使用它作为您的优势:
>>> bits = s.split(':')
>>> bits[2],bits[3]
('teonnyn!teonnyn@teonnyn.tmi.twitch.tv WHISPER blastweb ', 'Hello Bot')
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.