# -*- coding: utf8 -*-
import os
import json
import urllib2
import urllib
class Chat(object) :
    key = "3a2d2cf6f0501cdcccf302d8d62339f9"
    apiurl="http://www.tuling123.com/openapi/api?"

    def init(self):
        os.system("clera")
        print "--------------欢迎是用图灵机器人---------------"
        os.system("espeak -vzh '欢迎使用图灵机器人'")
        print "----------------------------------------------- "
    def get(self):
        print "指令"
        info = raw_input()
        if (info == 'q' or info == 'exit' or info == "quit"):
           print "goodbye"
           return
        self.send(info)
    def send(self,info):
        url = self.apiurl + 'key='+self.key+'&'+'info='+info
        re = urllib2.urlopen(url).read()
        re_dict = json.loads(re)
        text=re_dict['text'] #uencod编码格式
        s=text.encode('utf-8') #换成utf-8编码
        print '回答',s    
        url = "http://tts.baidu.com/text2audio?lan=zh&ie=UTF-8&text="+s
        wp = urllib.urlopen(url) 
        content = wp.read()
        fp = open("/home/pi/xiangmu/tulin/tulin.mp3","wb")
        fp.write(content)
        os.system("mplayer /home/pi/xiangmu/tulin/tulin.mp3")
        #os.system("./bofang.sh")
        fp.close() 

        self.get()
if __name__ == "__main__":
    chat = Chat()
    chat.init()
    chat.get() 
   
