如何解决尝试建立Twitch Plays X时遇到“无法连接到抽搐”的问题
我一直在遵循本指南,了解如何构建自己的“ Twitch Plays X”,这是一种控制流媒体PC的众包聊天。
当我通过LiClipse Workspace运行它时,它一直给我错误“无法连接到抽搐”。我安装了pywin32,pip和python(兼容版本)。我有main.py,twitch.py和keypresser.py。出于明显的原因,我已经隐藏了我的oauth代码。
main.py
#Define the imports
import twitch
import keypresser
t = twitch.Twitch();
k = keypresser.Keypresser();
#Enter your twitch username and oauth-key below,and the app connects to twitch with the details.
#Your oauth-key can be generated at http://twitchapps.com/tmi/
username = "twitchplaysTrading";
key = "oauth:";
t.twitch_connect(username,key);
#The main loop
while True:
#Check for new mesasages
new_messages = t.twitch_recieve_messages();
if not new_messages:
#No new messages...
continue
else:
for message in new_messages:
#Wuhu we got a message. Let's extract some details from it
msg = message['message'].lower()
username = message['username'].lower()
print(username + ": " + msg);
#This is where you change the keys that shall be pressed and listened to.
#The code below will simulate the key q if "q" is typed into twitch by someone
#.. the same thing with "w"
#Change this to make Twitch fit to your game!
if msg == "!buy aapl": k.key_press("a");
if msg == "!buy amzn": k.key_press("z");
if msg == "!buy spy": k.key_press("s");
if msg == "!sell aapl": k.key_press("b");
if msg == "!sell amzn": k.key_press("x");
if msg == "!sell spy": k.key_press("d");
if msg == "!close all": k.key_press("c");
twitch.py
import socket
import sys
import re
class Twitch:
user = "";
oauth = "";
s = None;
def twitch_login_status(self,data):
if not re.match(r'^:(testserver\.local|tmi\.twitch\.tv) NOTICE \* :Login unsuccessful\r\n$',data): return True
else: return False
def twitch_connect(self,user,key):
self.user = user;
self.oauth= key;
print("Connecting to twitch.tv");
s = socket.socket(socket.AF_INET,socket.soCK_STREAM);
s.settimeout(0.6);
connect_host = "irc.twitch.tv";
connect_port = 6667;
try:
s.connect((connect_host,connect_port));
except:
print("Failed to connect to twitch");
sys.exit();
print("Connected to twitch");
print("Sending our details to twitch...");
s.send('USER %s\r\n' % user);
s.send('PASS %s\r\n' % key);
s.send('NICK %s\r\n' % user);
if not self.twitch_login_status(s.recv(1024)):
print("... and they didn't accept our details");
sys.exit();
else:
print("... they accepted our details");
print("Connected to twitch.tv!")
self.s = s;
s.send('JOIN #%s\r\n' % user)
s.recv(1024);
def check_has_message(self,data):
return re.match(r'^:[a-zA-Z0-9_]+\![a-zA-Z0-9_]+@[a-zA-Z0-9_]+(\.tmi\.twitch\.tv|\.testserver\.local) PRIVMSG #[a-zA-Z0-9_]+ :.+$',data)
def parse_message(self,data):
return {
'channel': re.findall(r'^:.+\![a-zA-Z0-9_]+@[a-zA-Z0-9_]+.+ PRIVMSG (.*?) :',data)[0],'username': re.findall(r'^:([a-zA-Z0-9_]+)\!','message': re.findall(r'PRIVMSG #[a-zA-Z0-9_]+ :(.+)',data)[0].decode('utf8')
}
def twitch_recieve_messages(self,amount=1024):
data = None
try: data = self.s.recv(1024);
except: return False;
if not data:
print("Lost connection to Twitch,attempting to reconnect...");
self.twitch_connect(self.user,self.oauth);
return None
#self.ping(data)
if self.check_has_message(data):
return [self.parse_message(line) for line in filter(None,data.split('\r\n'))];
keypresser.py
import socket
import sys
import re
class Twitch:
user = "";
oauth = "";
s = None;
def twitch_login_status(self,data.split('\r\n'))];
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。