简体   繁体   English

在Python中,如何使用正则表达式仅过滤Twitch API的WHISPER命令中的用户和消息?

[英]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]+)

REGEX DEMO 正则演示


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.

相关问题 如何使用 Twitch api python 检查是否有人在 twitch 上直播 - How can I check if Someone is Live on twitch with the Twitch api python 如何在 python 中的 Telebot API 命令后回复消息 - How can I reply to a message after a command in Telebot API in python 如何在 OpenAI 的 Whisper ASR 中获取词级时间戳? - How can I get word-level timestamps in OpenAI's Whisper ASR? 如何根据自己的训练数据微调 OpenAI 的 Whisper ASR 中的 model? - How can I finetune a model from OpenAI's Whisper ASR on my own training data? 如何为 OpenAI 的 Whisper ASR 提供一些提示短语? - How can I give some hint phrases to OpenAI's Whisper ASR? 如何在python中使用Selenium Webdriver滚动网页以抽搐? - How can I scroll a web page using selenium webdriver in python, for twitch? 在元组上使用python regex过滤用户输入 - using python regex on tuples to filter user inputs Python 3和Twitch API? - Python 3 and Twitch API? 如何使用Python Twitch IRC Bot获取聊天消息参数? - How do I use a Python Twitch IRC Bot to take chat message parameters? 在python中,如何在api端点中的{之前添加$符号 - In python, How can i add a $ symbol just before { in the api endpoint
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM