-
Notifications
You must be signed in to change notification settings - Fork 0
/
cozmo_irc2speech
111 lines (95 loc) · 2.32 KB
/
cozmo_irc2speech
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
import socket
import re
import cozmo
import time
CHUNKY = 128
#cozvox
shouldRun = True
isTalking = False
error = ""
durate = 0.5
pitch = 0.0
msg = ""
def quiter(_):
global shouldRun
shouldRun = False
def dur(args):
global durate
global error
try :
durate = float (args[0])
except(ValueError):
error = "INVALID INPUT"
def pit(args):
print(args)
global pitch
global error
try :
pitch = float (args[0])
except(ValueError):
error = "INVALID INPUT"
print(pitch)
cmds = {
"quit": (lambda _: quiter(_)),
"duration": (lambda d: dur(d)),
"pitch": (lambda p: pit(p))
}
def cozmo_program(robot: cozmo.robot.Robot):
global durate, pitch, msg, isTalking
robot.say_text(msg, use_cozmo_voice = False, duration_scalar=durate, voice_pitch = pitch).wait_for_completed()
isTalking = False
def joinchan(chan):
ircsock.send(bytes("JOIN "+ chan +"\n", "UTF-8"))
ircmsg = ""
while ircmsg.find("End of /NAMES list.") == -1:
ircmsg = ircsock.recv(2048).decode("UTF-8")
ircmsg = ircmsg.strip('\n\r')
if len(ircmsg) == 0:
break
print("INFO: " + ircmsg)
def ping():
ircsock.send(bytes("PONG :pingis\n", "UTF-8"))
ircsock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server = "chat.freenode.net"
channel = "##cozmo-test"
botnick = "HI0IM0COZMO"
exitcode = "bye " + botnick
ircsock.connect((server, 6667))
ircsock.send(bytes("USER " + botnick + " " + botnick + " " + botnick + " " + botnick + "\n", "UTF-8"))
ircsock.send(bytes("NICK " + botnick + "\n", "UTF-8"))
#
joinchan(channel)
pattern = re.compile('[^:]+$')
pcmd = re.compile("^\*(\w+)")
while (shouldRun):
ircmsg = ircsock.recv(2048).decode("UTF-8")
ircmsg = ircmsg.strip('\n\r')
if ircmsg.find("PING :") != -1:
ping()
print("1")
if len(ircmsg) == 0:
continue
print("2")
m = pattern.search(ircmsg)
script = m.group()
c = pcmd.search(script)
if c :
cmds[c.group()[1:]](script.split(" ")[1:])
if len(error)>0:
ircsock.send(bytes(error + "\n", "UTF-8"))
error = ""
else:
print("MSG: " + script)
chunks = int(len(script) / CHUNKY) + 1
for i in range(0, chunks):
print (range(0, chunks))
msg = script[i*CHUNKY:i*CHUNKY+CHUNKY]
print(i)
print(msg)
isTalking = True
cozmo.run_program(cozmo_program)
while (isTalking):
time.sleep(0.5)
print(3)
ircsock.send(bytes("QUIT\n", "UTF-8"))
ircsock.close()