Skip to content

Instantly share code, notes, and snippets.

@nishimotz
Created November 17, 2009 06:01
Show Gist options
  • Save nishimotz/236687 to your computer and use it in GitHub Desktop.
Save nishimotz/236687 to your computer and use it in GitHub Desktop.
# gtalk.py
# encoding: utf-8
# socket client wrapper for Windows / Linux
# updated by nishimotz 2010-04-04
# created by nishimotz 2009-11-17
# http://ja.nishimotz.com/galateatalk
import socket
import threading
import time
import subprocess
import os
class GalateatalkThread(threading.Thread):
def __init__(self):
threading.Thread.__init__(self, name = 'galateatalk_thread')
def run(self):
os.chdir("c:\\work\\istc\\SSM\\gtalk\\")
self.p = subprocess.Popen(["gtalk.exe", "-C", "ssm-win.conf", "-p", "59999"],
stdout=subprocess.PIPE)
while True:
print "SO: " + self.p.stdout.readline()
self.p.poll()
if self.p.returncode != 0:
return
time.sleep(0.1)
class GalateatalkManager:
"""control galateatalk"""
def __init__(self):
self.s = socket.socket()
self.galtateatalk_thread = GalateatalkThread()
self.galtateatalk_thread.start()
def connect(self, addr = ('127.0.0.1', 59999)):
try:
self.s.connect(addr)
except:
return False
#a = ('Text.text', 'Text.pho', 'Text.dur',
# 'Speak.text', 'Speak.len', 'Speak.dur')
#for i in a:
# self.send('prop %s = NoAutoOutput' % i)
return True
def send(self, msg):
try:
self.s.setblocking(1)
self.s.send('%s./\r' % msg)
print "send: " + msg
except:
print "send error"
def wait_for(self, msg):
str = ''
while True:
try:
self.s.setblocking(0)
str += self.s.recv(100)
except:
pass
if str.find(msg) >= 0:
print "recv: " + str
return
def close(self):
self.s.setblocking(1)
self.send('set Run = EXIT')
self.galtateatalk_thread.join()
return self.s.close()
def talk(self, text):
# msg = text.decode('utf8').encode('euc-jp') # for linux
msg = text.decode('utf8').encode('SJIS') # for windows
self.send('set Text = %s' % msg)
self.wait_for('rep Speak.stat = READY')
self.send('set Speak = NOW')
self.wait_for('rep Speak.stat = IDLE')
if __name__ == '__main__':
s = GalateatalkManager()
s.connect()
s.talk('12345')
s.talk('私は今日学校に行きます。')
s.close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment