【发布时间】:2016-11-21 17:06:09
【问题描述】:
我完全没有使用 Python 的经验,也很少使用任何其他编码语言。然而,几天前我发现了一个关于如何在 twitch.tv 上制作“Twitch Plays”流的教程。该过程的最后一步是弄乱您从 pastebin.com 复制和粘贴的一些 Python 代码。我 99% 确定我已经正确完成了到目前为止的所有步骤,但是我在两个单独的文件中得到了两个错误代码。如果我发布的内容超出了需要,我很抱歉,我不知道什么是相关或必要的。
第一个文件“Main”给出错误信息:
将我们的详细信息发送到 twitch... 回溯(最近一次通话最后): 文件位置,第 11 行,在 t.twitch_connect(用户名, 密钥);
1 #Define the imports
2 import twitch
3 import keypresser
4 t = twitch.Twitch();
5 k = keypresser.Keypresser();
6
7 #Enter your twitch username and oauth-key below, and the app connects to twitch with the details.
8 #Your oauth-key can be generated at http://twitchapps.com/tmi/
9 username = "dbproxy";
10 key = "I put my OauthKey here in the real file";
11 t.twitch_connect(username, key);
12
13 #The main loop
14 while True:
15 #Check for new mesasages
16 new_messages = t.twitch_recieve_messages();
17
18 if not new_messages:
19 #No new messages...
20 continue
21 else:
22 for message in new_messages:
23 #Wuhu we got a message. Let's extract some details from it
24 msg = message['message'].lower()
25 username = message['username'].lower()
26 print(username + ": " + msg);
27
28 #This is where you change the keys that shall be pressed and listened to.
29 #The code below will simulate the key q if "q" is typed into twitch by someone
30 #.. the same thing with "w"
31 #Change this to make Twitch fit to your game!
32 if msg == "q": k.key_press("q");
33 if msg == "w": k.key_press("w");
第二个文件“Twitch”给出了这个错误信息:
文件位置,第 30 行,在 twitch_connect 中 s.send('USER %s\r\n' % user); TypeError: 'str' 不支持缓冲区接口
代码是:
1 import socket
2 import sys
3 import re
4
5 class Twitch:
6
7 user = "";
8 oauth = "";
9 s = None;
10
11 def twitch_login_status(self, data):
12 if not re.match(r'^:(testserver\.local|tmi\.twitch\.tv) NOTICE \* :Login unsuccessful\r\n$', data): return True
13 else: return False
14
15 def twitch_connect(self, user, key):
16 self.user = user;
17 self.oauth= key;
18 print("Connecting to twitch.tv");
19 s = socket.socket(socket.AF_INET, socket.SOCK_STREAM);
20 s.settimeout(0.6);
21 connect_host = "irc.twitch.tv";
22 connect_port = 6667;
23 try:
24 s.connect((connect_host, connect_port));
25 except:
26 print("Failed to connect to twitch");
27 sys.exit();
28 print("Connected to twitch");
29 print("Sending our details to twitch...");
30 s.send('USER %s\r\n' % user);
31 s.send('PASS %s\r\n' % key);
32 s.send('NICK %s\r\n' % user);
33
34 if not self.twitch_login_status(s.recv(1024)):
35 print("... and they didn't accept our details");
36 sys.exit();
37 else:
38 print("... they accepted our details");
39 print("Connected to twitch.tv!")
40 self.s = s;
41 s.send('JOIN #%s\r\n' % user)
42 s.recv(1024);
43
44 def check_has_message(self, data):
45 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)
46
47 def parse_message(self, data):
48 return {
49 'channel': re.findall(r'^:.+\![a-zA-Z0-9_]+@[a-zA-Z0-9_]+.+ PRIVMSG (.*?) :', data)[0],
50 'username': re.findall(r'^:([a-zA-Z0-9_]+)\!', data)[0],
51 'message': re.findall(r'PRIVMSG #[a-zA-Z0-9_]+ :(.+)', data)[0].decode('utf8')
52 }
53
54 def twitch_recieve_messages(self, amount=1024):
55 data = None
56 try: data = self.s.recv(1024);
57 except: return False;
58
59 if not data:
60 print("Lost connection to Twitch, attempting to reconnect...");
61 self.twitch_connect(self.user, self.oauth);
62 return None
63
64 #self.ping(data)
65
66 if self.check_has_message(data):
67 return [self.parse_message(line) for line in filter(None, data.split('\r\n'))];
我很抱歉这篇文章有多长,因为有些事情告诉我这是一个非常简单的问题。
如果有人不介意回答,我确实有最后一个问题。
我已经把代码弄得乱七八糟了,说实话,我很震惊没有更多的错误消息,但我相当确定在第一个文件“Main”中我是应该修改:
#Wuhu we got a message. Let's extract some details from it
msg = message['message'].lower()
username = message['username'].lower()
print(username + ": " + msg);
我认为我应该更改 ['message']、['username'] 和第三行引号之间的内容,因为这些地方的文本是橙色的,到目前为止我应该触摸的所有内容在“主”文件上是橙色的,但我不知道那部分是做什么的,或者我为什么要更改这些部分,我已经阅读了 10 次左右的教程,但它没有解释,这很明显知道这些代码的含义和作用的人吗?
非常感谢您的任何帮助,提前谢谢您。
【问题讨论】:
标签: python