Skip to content

Commit

Permalink
接入twitch;部分优化
Browse files Browse the repository at this point in the history
  • Loading branch information
Ikaros-521 committed Oct 21, 2023
1 parent 02b0d5c commit c8d862c
Show file tree
Hide file tree
Showing 12 changed files with 1,809 additions and 1,275 deletions.
4 changes: 4 additions & 0 deletions Scripts/2-2.安装依赖.bat
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,15 @@ Miniconda3\python.exe -m pip install -r requirements_dy.txt -i https://pypi.tuna
Miniconda3\python.exe -m pip install -r requirements_ks.txt -i https://pypi.tuna.tsinghua.edu.cn/simple
Miniconda3\python.exe -m pip install -r requirements_douyu.txt -i https://pypi.tuna.tsinghua.edu.cn/simple
Miniconda3\python.exe -m pip install -r requirements_talk.txt -i https://pypi.tuna.tsinghua.edu.cn/simple
Miniconda3\python.exe -m pip install -r requirements_youtube.txt -i https://pypi.tuna.tsinghua.edu.cn/simple
Miniconda3\python.exe -m pip install -r requirements_twitch.txt -i https://pypi.tuna.tsinghua.edu.cn/simple
Miniconda3\python.exe -m pip install -r requirements_bilibili.txt -i https://pypi.python.org/simple/
Miniconda3\python.exe -m pip install -r requirements_dy.txt -i https://pypi.python.org/simple/
Miniconda3\python.exe -m pip install -r requirements_ks.txt -i https://pypi.python.org/simple/
Miniconda3\python.exe -m pip install -r requirements_douyu.txt -i https://pypi.python.org/simple/
Miniconda3\python.exe -m pip install -r requirements_talk.txt -i https://pypi.python.org/simple/
Miniconda3\python.exe -m pip install -r requirements_youtube.txt -i https://pypi.python.org/simple/
Miniconda3\python.exe -m pip install -r requirements_twitch.txt -i https://pypi.python.org/simple/

echo 如果都成功了,那没事了,如果有失败的,请手动补装
cmd /k
1,073 changes: 541 additions & 532 deletions UI_main.py

Large diffs are not rendered by default.

10 changes: 8 additions & 2 deletions config.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"platform": "talk",
"room_display_id": "278333",
"platform": "twitch",
"room_display_id": "zod0416",
"chat_type": "reread",
"need_lang": "none",
"before_prompt": "请简要回复:",
Expand Down Expand Up @@ -38,6 +38,12 @@
"cookie": "",
"ac_time_value": ""
},
"twitch": {
"token": "oauth:xxxx",
"user": "love_ikaros",
"proxy_server": "127.0.0.1",
"proxy_port": "10809"
},
"read_user_name": {
"enable": false,
"voice_change": true,
Expand Down
6 changes: 6 additions & 0 deletions config.json.bak
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,12 @@
"cookie": "",
"ac_time_value": ""
},
"twitch": {
"token": "oauth:xxxx",
"user": "love_ikaros",
"proxy_server": "127.0.0.1",
"proxy_port": "10809"
},
"read_user_name": {
"enable": false,
"voice_change": true,
Expand Down
80 changes: 75 additions & 5 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -687,7 +687,7 @@ def init_config(self):
"""
# 修改下拉框内容
self.ui.comboBox_platform.clear()
self.ui.comboBox_platform.addItems(["聊天模式", "哔哩哔哩", "抖音", "快手", "斗鱼", "YouTube"])
self.ui.comboBox_platform.addItems(["聊天模式", "哔哩哔哩", "抖音", "快手", "斗鱼", "YouTube", "twitch"])
platform_index = 0
if self.platform == "talk":
platform_index = 0
Expand All @@ -701,6 +701,8 @@ def init_config(self):
platform_index = 4
elif self.platform == "youtube":
platform_index = 5
elif self.platform == "twitch":
platform_index = 6
self.ui.comboBox_platform.setCurrentIndex(platform_index)

# 修改输入框内容
Expand Down Expand Up @@ -1969,6 +1971,59 @@ def bilibili_gui_create():

bilibili_gui_create()

# twitch
def twitch_gui_create():
data_json = []
twitch_config = config.get("twitch")

tmp_json = {
"label_text": "token",
"label_tip": "访问 https://twitchapps.com/tmi/ 获取,格式为:oauth:xxx",
"data": twitch_config["token"],
"main_obj_name": "twitch",
"index": 1
}
data_json.append(tmp_json)

tmp_json = {
"label_text": "用户名",
"label_tip": "你的twitch账号用户名",
"data": twitch_config["user"],
"main_obj_name": "twitch",
"index": 2
}
data_json.append(tmp_json)

tmp_json = {
"label_text": "HTTP代理IP地址",
"label_tip": "代理软件,http协议监听的ip地址,一般为:127.0.0.1",
"data": twitch_config["proxy_server"],
"main_obj_name": "twitch",
"index": 3
}
data_json.append(tmp_json)

tmp_json = {
"label_text": "HTTP代理端口",
"label_tip": "代理软件,http协议监听的端口,一般为:1080",
"data": twitch_config["proxy_port"],
"main_obj_name": "twitch",
"index": 4
}
data_json.append(tmp_json)

widgets = self.create_widgets_from_json(data_json)

# 动态添加widget到对应的gridLayout
row = 0
# 分2列,左边就是label说明,右边就是输入框等
for i in range(0, len(widgets), 2):
self.ui.gridLayout_twitch.addWidget(widgets[i], row, 0)
self.ui.gridLayout_twitch.addWidget(widgets[i + 1], row, 1)
row += 1

twitch_gui_create()

# bard
def bard_gui_create():
data_json = []
Expand Down Expand Up @@ -2701,13 +2756,16 @@ def common_textEdit_handle(content):
config_data["platform"] = "douyu"
elif platform == "YouTube":
config_data["platform"] = "youtube"
elif platform == "twitch":
config_data["platform"] = "twitch"

# 获取单行文本输入框的内容
room_display_id = self.ui.lineEdit_room_display_id.text()
if False == self.is_alpha_numeric(room_display_id):
logging.error("直播间号只由字母或数字组成,请勿输入错误内容")
self.show_message_box("错误", "直播间号只由字母或数字组成,请勿输入错误内容", QMessageBox.Critical)
return False
# 直播间号配置放宽
# if False == self.is_alpha_numeric(room_display_id):
# logging.error("直播间号只由字母或数字组成,请勿输入错误内容")
# self.show_message_box("错误", "直播间号只由字母或数字组成,请勿输入错误内容", QMessageBox.Critical)
# return False
config_data["room_display_id"] = room_display_id

# 新增LLM时,这块也需要适配,保存回配置文件
Expand Down Expand Up @@ -3224,6 +3282,18 @@ def reorganize_copywriting_config_data(copywriting_config_data):
bilibili_data = self.update_data_from_gridLayout(self.ui.gridLayout_bilibili)
config_data["bilibili"] = reorganize_grid_data(bilibili_data, bilibili_keys_mapping)

twitch_keys_mapping = {
"token": 0,
"user": 1,
"proxy_server": 2,
"proxy_port": 3
}

# 重组twitch数据并写回json
twitch_data = self.update_data_from_gridLayout(self.ui.gridLayout_twitch)
config_data["twitch"] = reorganize_grid_data(twitch_data, twitch_keys_mapping)


bard_keys_mapping = {
"token": 0
}
Expand Down
3 changes: 2 additions & 1 deletion requirements_common.txt
Original file line number Diff line number Diff line change
Expand Up @@ -135,4 +135,5 @@ zhipuai==1.0.7
bardapi==0.1.33
revTongYi==0.0.1.0
pyautogui
nicegui==1.3.14
nicegui==1.3.14
pytchat==0.5.5
38 changes: 38 additions & 0 deletions requirements_twitch.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
openai
pygame
edge-tts==6.1.6
langid
aiohttp
asyncio
slack_sdk==3.21.3
profanity==1.1
elevenlabs
PyPDF2
protobuf
protobuf_inspector
requests
websocket-client
langchain==0.0.142
PyQt5
pyqt5-tools
tiktoken
pyahocorasick
webuiapi
pyvirtualcam
numpy
faiss-cpu
sentence_transformers
pydub
send2trash
pypinyin
sparkdesk-api
gradio_client
schedule
pyhandytools
curl_cffi
zhipuai
bardapi
revTongYi
pyautogui
nicegui
socket
37 changes: 37 additions & 0 deletions tests/test_tiktok/tiktok.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
from TikTokLive import TikTokLiveClient
from TikTokLive.types.events import CommentEvent, ConnectEvent, DisconnectEvent, JoinEvent

proxies = {
"http://": "http://127.0.0.1:10809",
"https://": "http://127.0.0.1:10809"
}

# Instantiate the client with the user's username
client: TikTokLiveClient = TikTokLiveClient(unique_id="@markus864", proxies=proxies)


# Define how you want to handle specific events via decorator
@client.on("connect")
async def on_connect(_: ConnectEvent):
print("Connected to Room ID:", client.room_id)

@client.on("disconnect")
async def on_disconnect(event: DisconnectEvent):
print("Disconnected")

@client.on("join")
async def on_join(event: JoinEvent):
print(f"@{event.user.unique_id} joined the stream!")

# Notice no decorator?
async def on_comment(event: CommentEvent):
print(f"{event.user.nickname} -> {event.comment}")


# Define handling an event via a "callback"
client.add_listener("comment", on_comment)

if __name__ == '__main__':
# Run the client and block the main thread
# await client.start() to run non-blocking
client.run()
56 changes: 56 additions & 0 deletions tests/test_twitch/twitch.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import socks, re
from emoji import demojize

server = 'irc.chat.twitch.tv'
port = 6667
nickname = '主人'
token = 'oauth:xxx' # 访问 https://twitchapps.com/tmi/ 获取
user = 'love_ikaros' # 你的Twitch用户名 Your Twitch username
channel = '#prettyyjj' # 要从中检索消息的频道,注意#必须携带在头部 The channel you want to retrieve messages from

# 代理服务器的地址和端口
proxy_server = "127.0.0.1"
proxy_port = 10809

# 配置代理服务器
socks.set_default_proxy(socks.HTTP, proxy_server, proxy_port)

# 创建socket对象
sock = socks.socksocket()

try:
sock.connect((server, port))
print("成功连接 Twitch IRC server")
except Exception as e:
print(f"连接 Twitch IRC server 失败: {e}")


sock.send(f"PASS {token}\n".encode('utf-8'))
sock.send(f"NICK {nickname}\n".encode('utf-8'))
sock.send(f"JOIN {channel}\n".encode('utf-8'))

regex = r":(\w+)!\w+@\w+\.tmi\.twitch\.tv PRIVMSG #\w+ :(.+)"

while True:
try:
resp = sock.recv(2048).decode('utf-8')

# 输出所有接收到的内容,包括PING/PONG
# print(resp)

if resp.startswith('PING'):
sock.send("PONG\n".encode('utf-8'))

elif not user in resp:
resp = demojize(resp)
match = re.match(regex, resp)

username = match.group(1)
message = match.group(2)


chat = '[' + username + ']: ' + message
print(chat)

except Exception as e:
print("Error receiving chat: {0}".format(e))
Loading

0 comments on commit c8d862c

Please sign in to comment.