Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
# 只能在windows上运行,需要安装 pip install pywin32
import win32com.client
import urllib.parse
import urllib.request
import json
import time
import re
# access token在http://you.owncast.site0/admin/access-tokens/ 页面新建,需要新建一个具有admin权限的token
access_token="ACCESS_TOKEN"
url = "http://you.owncast.site/api/integrations/chat"
speaker = win32com.client.Dispatch("SAPI.SpVoice")
user_agent = 'owncast-chat-reader'
headers = {'User-Agent': user_agent, "Authorization": "Bearer " + access_token}
def filter_emoji(input_text):
pattern = r'<[^<]*>'
return re.sub(pattern, "", input_text)
def get_chat_history():
req = urllib.request.Request(url, None, headers)
chat_history = list()
with urllib.request.urlopen(req) as response:
chat_history_raw = response.read().decode("utf-8")
parsed = json.loads(chat_history_raw)
for entry in parsed:
if entry["type"] == 'CHAT':
chat_history.append({
"name": entry['user']['displayName'],
"text": filter_emoji(entry['body']),
"timestamp": entry["timestamp"]})
return chat_history
if __name__ == "__main__":
msgs = get_chat_history()
timestamp_cursor = str()
if len(msgs) > 1:
timestamp_cursor = msgs[len(msgs)-1]["timestamp"]
print("last time: " + timestamp_cursor)
while True:
time.sleep(10)
msgs = get_chat_history()
for msg in msgs:
if (msg['timestamp'] > timestamp_cursor):
print("new msg: " + msg['text'])
speaker.Speak(msg['name'] + "说:" + msg['text'])
timestamp_cursor = msg['timestamp']
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment