def main(token): baidu_tts().tts("Welcome using the FlightTracer") baidu_tts().tts("Made by Lan_zhijiang") baidu_tts().tts("Loading cities... please wait...") cities_info_list = get_cities_info_list() baidu_tts().tts("Load finished") dep = "北京" arr = "深圳" date = "20190811" baidu_tts().tts("Staring confirm info", token) baidu_tts().tts("Information confirm completed", token) baidu_tts().tts("Searching for the flight...please wait...", token) flight_list = request_csairlines(dep, arr, date, cities_info_list, token) flight_num_list = list(flight_list.keys()) for i in flight_num_list: dep_time = flight_list[i]["depTime"][0:2] + ":" + flight_list[i][ "depTime"][2:4] arr_time = flight_list[i]["arrTime"][0:2] + ":" + flight_list[i][ "arrTime"][2:4] baidu_tts().tts( i + ",这趟航班的经济舱价格是" + str(flight_list[i]["cabin"][-1]["adultPrice"]) + ",出发时间是" + str(dep_time) + ",到达时间是" + str(arr_time), token)
def cortol(self, cortolthings, cortolmode, tok): #智能家居中的灯、开关控制于此(支持一句话插件) bt = baidu_tts() bs = baidu_stt(1, 2, 3, 4) r = recorder() h = hass() url = 'http://hassio.local' port = '8123' passwd = 'y20050801' headers = {'x-ha-access': passwd, 'content-type': 'application/json'} domains = h.service() e_id = h.e_id() cortolthings = unicode(cortolthings, "utf-8", "ignore") try: if cortolmode == 'turn_on': if e_id[cortolthings] != None: if 'switch' in e_id[cortolthings]: color_name = 'a' service = '/api/services/switch/turn_on' elif 'light' in e_id[cortolthings]: ask = '请问要设置什么颜色,可以忽略' bt.tts(ask, tok) speaker.speak() speaker.ding() r.record() speaker.dong() color_name_f = bs.stt('./voice.wav', tok) if color_name_f != None: color_name = h.chosecolor(color_name_f) else: color_name = 'a' service = '/api/services/light/turn_on' elif 'automation' in e_id[cortolthings]: color_name = 'a' service = '/api/services/automation/turn_on' elif cortolmode == 'turn_off': if e_id[cortolthings] != None: if 'switch' in e_id[cortolthings]: color_name = 'a' service = '/api/services/switch/turn_off' elif 'light' in e_id[cortolthings]: color_name = 'a' service = '/api/services/light/turn_off' elif 'automation' in e_id[cortolthings]: color_name = 'a' service = '/api/services/automation/turn_off' except KeyError: sorry = '对不起,控制设备不存在,请注意!控制设备的名称得跟在homeassistant上设置的friendly,name一样' bt.tts(sorry, tok) speaker.speak() except TypeError: sorry = '对不起,控制设备不存在,请注意!控制设备的名称得跟在homeassistant上设置的friendly,name一样' bt.tts(sorry, tok) speaker.speak() except ValueError: sorry = '对不起,控制设备不存在,请注意!控制设备的名称得跟在homeassistant上设置的friendly,name一样' bt.tts(sorry, tok) speaker.speak() else: pass try: cortole_id = e_id[cortolthings] if color_name == 'a': dataf = {"entity_id": cortole_id.encode('utf-8')} data = json.dumps(dataf) else: dataf = { "color_name": color_name, "entity_id": cortole_id.encode('utf-8') } data = json.dumps(dataf) except KeyError: sorry = '对不起,控制设备不存在,请注意!控制设备的名称得跟在homeassistant上设置的friendly,name一样' bt.tts(sorry, tok) speaker.speak() except TypeError: sorry = '对不起,控制设备不存在,请注意!控制设备的名称得跟在homeassistant上设置的friendly,name一样' bt.tts(sorry, tok) speaker.speak() except ValueError: sorry = '对不起,控制设备不存在,请注意!控制设备的名称得跟在homeassistant上设置的friendly,name一样' bt.tts(sorry, tok) speaker.speak() else: print data cortolback = requests.post(url + ':' + port + service, headers=headers, data=data) if cortolback.status_code == 200 or cortolback.status_code == 201: sayback = '执行成功' bt.tts(sayback, tok) speaker.speak() else: sayback = '执行错误' bt.tts(sayback, tok) speaker.speak()
def main(tok): r = recorder() bt = baidu_tts() url = 'https://snowboy.kitt.ai/api/v1/train/' apikey = '78ee816d9bfc8fb0e01341d4408e3f23fbbc9b03' askf = '第一次训练录音开始,请在滴一声之后说出,blueberry' asks = '第二次训练录音开始,请在滴一声之后说出,blueberry' askt = '第三次训练录音开始,请在滴一声之后说出,blueberry' bt.tts(askf, tok) speaker.speak() speaker.ding() r.train_f_record() speaker.dong() bt.tts(asks, tok) speaker.speak() speaker.ding() r.train_s_record() speaker.dong() bt.tts(askt, tok) speaker.speak() speaker.ding() r.train_t_record() speaker.dong() wav1 = "/home/pi/old_xiaolan/old_xiaolan/train_f.wav" wav2 = "/home/pi/old_xiaolan/old_xiaolan/train_s.wav" wav3 = "/home/pi/old_xiaolan/old_xiaolan/train_t.wav" out = "/home/pi/old_xiaolan/old_xiaolan/snowboy/blueberry.pmdl" data = { "name": hotword_name, "language": language, "age_group": age_group, "gender": gender, "microphone": microphone, "token": token, "voice_samples": [{ "wave": get_wave(wav1) }, { "wave": get_wave(wav2) }, { "wave": get_wave(wav3) }] } response = requests.post(endpoint, json=data) if response.ok: with open(out, "w") as outfile: outfile.write(response.content) print "Saved model to '%s'." % out trueback = "Saved model to '%s'." % out bt.tts(tureback, tok) speaker.speak() else: print "Request failed." print response.text
cursor.execute(sql) conn.commit() #主控包含传感器、蜂鸣器 class sth(object): sensor = Adafruit_DHT.DHT11 @property def sensor(self): return self.sensor def __init__(self): #初始化mode os.system('gpio -g mode %s out' % gpio.pdht11.value) os.system('gpio -g mode %s out' % gpio.pmq2.value) os.system('gpio -g mode %s out' % gpio.pbuzzer.value) #蜂鸣器 def buzzer(self): os.system('gpio -g write %s 1' % gpio.pbuzzer.value) time.sleep(0.5) os.system('gpio -g write %s 0' % gpio.pbuzzer.value) #读取dth11温度 def readDth(self): bt = baidu_tts() bs = baidu_stt(1, 'a', '{', 3) humidity,temperature = Adafruit_DHT.read_retry(self.sensor,gpio.pdht11.value) if humidity is not None and temperature is not None: saytext = '湿度是,' + humidity + '温度是' + temperature tok = bt.get_token() bt.tts(saytext, tok) speaker.speak()
def cortol(self, cortolmode, cortolthings, tok): bt = baidu_tts() bs = baidu_stt(1, 2, 3, 4) r = recorder() h = hass() setting = setting.setting() url = setting['smarthome']['url'] port = setting['smarthome']['port'] passwd = setting['smarthome']['passwd'] service = '/api/states' headers = {'x-ha-access': passwd, 'content-type': 'application/json'} try: e_id = h.e_id(tok) cortolthings = unicode(cortolthings, "utf-8", "ignore") if cortolmode == 'turn_on': if e_id[cortolthings] != None: if 'switch' in e_id[cortolthings]: color_name = 'none' service = '/api/services/switch/turn_on' elif 'light' in e_id[cortolthings]: bt.tts('请问要设置什么颜色,可以忽略', tok) speaker.speak() speaker.ding() r.record() speaker.dong() text = bs.stt('./voice.wav', tok) color_name = h.choose_color(text) service = '/api/services/light/turn_on' if cortolmode == 'turn_off': if e_id[cortolthings] != None: if 'switch' in e_id[cortolthings]: color_name = 'none' service = '/api/services/switch/turn_off' elif 'light' in e_id[cortolthings]: color_name = 'none' service = '/api/services/light/turn_off' if color_name != 'none': dataf = { "color_name": color_name, "entity_id": e_id[cortolthings].encode('utf-8') } data = json.dumps(dataf) else: dataf = {"entity_id": e_id[cortolthings].encode('utf-8')} data = json.dumps(dataf) except KeyError: bt.tts( '对不起,控制设备不存在,请注意!控制设备的名称得跟在homeassistant上设置的friendly,name一样', tok) speaker.speak() except TypeError: bt.tts( '对不起,控制设备不存在,请注意!控制设备的名称得跟在homeassistant上设置的friendly,name一样', tok) speaker.speak() except ValueError: bt.tts( '对不起,控制设备不存在,请注意!控制设备的名称得跟在homeassistant上设置的friendly,name一样', tok) speaker.speak() else: #print data r = requests.post(url + ':' + port + services, headers=headers, data=data) if cortolback.status_code == 200 or cortolback.status_code == 201: bt.tts('执行成功', tok) speaker.speak() else: bt.tts('执行错误', tok) speaker.speak()
def sui_ji(self, services, tok): bt = baidu_tts() bs = baidu_stt(1, 2, 3, 4) r = recorder() m = xlMusic() url = 'http://tingapi.ting.baidu.com/v1/restserver/ting?' song_name_c = random.uniform(0, 12) if song_name_c == 0: song_name = '皮皮虾我们走' elif song_name_c == 1: song_name = 'I hope you think of me' elif song_name_c == 2: song_name = '小幸运' elif song_name_c == 3: song_name = '全部都是你' elif song_name_c == 4: song_name = '佛系少女' elif song_name_c == 5: song_name = 'Something just like this' elif song_name_c == 6: song_name = 'Feel this Moment' elif song_name_c == 7: song_name = 'Welcome to NewYork' elif song_name_c == 8: song_name = '洛天依投食歌' elif song_name_c == 9: song_name = '带你去旅行' elif song_name_c == 10: song_name = '死机之歌' elif song_name_c == 11: song_name = 'Shape of You' elif song_name_c == 12: song_name = 'Kiss Fight' elif song_name_c == 13: song_name = 'Hot song' if song_name != 'Hot song': get_song_id_rawj = requests.get(url + services['search'] + song_name) elif song_name == 'Hot song': get_song_id_rawj = requests.get(url + services['hot']) get_song_id_j = get_song_id_rawj.json() try: if song_name == 'Hot song': id = get_song_id_j['result']['list'][song_name_c]['song_id'] else: id = get_song_id_j['song'][song_name_c]['songid'] except KeyError: try: if song_name == 'Hot song': id = get_song_id_j['result']['list'][song_name_c][ 'song_id'] else: id = get_song_id_j['song'][song_name_c]['songid'] except KeyError: bt.tts('对不起,播放错误') speaker.speak() else: pass else: get_song_url_rawj = requests.get(url + services['musicurl_get'] + id) get_song_url_j = get_song_url_rawj.json() song_name = get_song_url_j['songinfo']['title'] song_url_f = get_song_url_j['bitrate']['file_link'] song_url = (song_url_f.replace('\', ' '')) download = requests.get(song_url) with open("/home/pi/xiaolan/xiaolan/musiclib/music.mp3", "wb") as code: code.write(download.content) m.play(song_name, tok)
def main(tok): APPKEY = 'b8fff66168feb233d5cdb2f7931750f3' url = 'http://v.juhe.cn/toutiao/index?type=' r = recorder() bt = baidu_tts() bs = baidu_stt(1, 'a', 2, '{') newsserviceasktext = '请问您要听什么新闻?' bt.tts(newsserviceasktext, tok) speaker.speak() speaker.ding() r.record() speaker.dong() text = bs.stt('./voice.wav', tok) chose = random.randint(0, 9) if '国内新闻' in text: newsservice = 'chinanews' elif '国际新闻' in text: newsservice = 'nationnews' elif '科技新闻' in text: newsservice = 'kejinews' elif '体育新闻' in text: newsservice = 'tiyunews' else: newsservice = 't' if newsservice == 'chinanews': newstype = 'guonei' r = requests.post(url + newstype + '&key=' + APPKEY) json = r.json() if chose == 0: saytext = json['result']['data'][0]['title'] elif chose == 1: saytext = json['result']['data'][1]['title'] elif chose == 2: saytext = json['result']['data'][2]['title'] elif chose == 3: saytext = json['result']['data'][3]['title'] elif chose == 4: saytext = json['result']['data'][4]['title'] elif chose == 5: saytext = json['result']['data'][5]['title'] elif chose == 6: saytext = json['result']['data'][6]['title'] elif chose == 7: saytext = json['result']['data'][7]['title'] elif chose == 8: saytext = json['result']['data'][8]['title'] elif chose == 9: saytext = json['result']['data'][9]['title'] else: saytext = json['result']['data'][0]['title'] bt.tts(saytext, tok) speaker.speak() elif newsservice == 'nationnews': newstype = 'guoji' r = requests.post(url + newstype + '&key=' + APPKEY) json = r.json() if chose == 0: saytext = json['result']['data'][0]['title'] elif chose == 1: saytext = json['result']['data'][1]['title'] elif chose == 2: saytext = json['result']['data'][2]['title'] elif chose == 3: saytext = json['result']['data'][3]['title'] elif chose == 4: saytext = json['result']['data'][4]['title'] elif chose == 5: saytext = json['result']['data'][5]['title'] elif chose == 6: saytext = json['result']['data'][6]['title'] elif chose == 7: saytext = json['result']['data'][7]['title'] elif chose == 8: saytext = json['result']['data'][8]['title'] elif chose == 9: saytext = json['result']['data'][9]['title'] else: saytext = json['result']['data'][0]['title'] bt.tts(saytext, tok) speaker.speak() elif newsservice == 'kejinews': newstype = 'keji' r = requests.post(url + newstype + '&key=' + APPKEY) json = r.json() if chose == 0: saytext = json['result']['data'][0]['title'] elif chose == 1: saytext = json['result']['data'][1]['title'] elif chose == 2: saytext = json['result']['data'][2]['title'] elif chose == 3: saytext = json['result']['data'][3]['title'] elif chose == 4: saytext = json['result']['data'][4]['title'] elif chose == 5: saytext = json['result']['data'][5]['title'] elif chose == 6: saytext = json['result']['data'][6]['title'] elif chose == 7: saytext = json['result']['data'][7]['title'] elif chose == 8: saytext = json['result']['data'][8]['title'] elif chose == 9: saytext = json['result']['data'][9]['title'] else: saytext = json['result']['data'][0]['title'] elif newsservice == 'tiyunews': newstype = 'tiyu' r = requests.post(url + newstype + '&key=' + APPKEY) json = r.json() if chose == 0: saytext = json['result']['data'][0]['title'] elif chose == 1: saytext = json['result']['data'][1]['title'] elif chose == 2: saytext = json['result']['data'][2]['title'] elif chose == 3: saytext = json['result']['data'][3]['title'] elif chose == 4: saytext = json['result']['data'][4]['title'] elif chose == 5: saytext = json['result']['data'][5]['title'] elif chose == 6: saytext = json['result']['data'][6]['title'] elif chose == 7: saytext = json['result']['data'][7]['title'] elif chose == 8: saytext = json['result']['data'][8]['title'] elif chose == 9: saytext = json['result']['data'][9]['title'] else: saytext = json['result']['data'][0]['title'] else: newstype = 'top' r = requests.post(url + newstype + '&key=' + APPKEY) json = r.json() if chose == 0: saytext = json['result'][0]['title'] elif chose == 1: saytext = json['result'][1]['title'] elif chose == 2: saytext = json['result'][2]['title'] elif chose == 3: saytext = json['result'][3]['title'] elif chose == 4: saytext = json['result'][4]['title'] elif chose == 5: saytext = json['result'][5]['title'] elif chose == 6: saytext = json['result'][6]['title'] elif chose == 7: saytext = json['result'][7]['title'] elif chose == 8: saytext = json['result'][8]['title'] elif chose == 9: saytext = json['result'][9]['title'] else: saytext = json['result'][0]['title'] bt.tts(saytext, tok) speaker.speak()
def do_intent(text, tok=""): sm = hass() m = xlMusic() services = {'musicurl_get': 'method=baidu.ting.song.play&songid=', 'search': 'method=baidu.ting.search.catalogSug&query=', 'hot': 'method=baidu.ting.song.getRecommandSongList&song_id=877578&num=12'} if text is not None: if '闹钟' in text: intent = 'clock' return intent elif '打开' in text: cortolthings = text[6:-2] print cortolthings cortolmode = 'turn_on' sm.cortol(cortolthings, cortolmode, tok) elif '关闭' in text: cortolthings = text[6:-2] cortolmode = 'turn_off' sm.cortol(cortolthings, cortolmode, tok) elif '天气' in text: intent = 'weather' return intent elif '重新说' in text or '重复' in text: intent = 'respeaker' return intent elif '翻译' in text: intent = 'translate' return intent elif '搜索' in text: intent = 'ser' return intent elif '闲聊' in text: intent = 'tuling' return intent elif '关机' in text: print ('SHUTDOWING...') os.system('sudo poweroff') elif '重启' in text: print ('REBOOTING...') os.system('sudo reboot') elif '怎么走' in text: intent = 'map' return intent elif '酒店' in text: intent = 'hotel' return intent elif '旅游' in text: intent = 'travel' return intent elif '做游戏' in text: intent = 'minigame' return intent elif '新闻' in text: intent = 'news' return intent elif '傻逼' in text: speaker.falu() elif '拍照' in text: intent = 'camera' return intent elif '邮件' in text or '邮件助手' in text: intent = 'mail' return intent elif '快递' in text: intent = 'experss' return intent elif '笑话' in text: intent = 'joke' return intent elif '训练' in text: intent= 'snowboytrain' return intent elif '播放' in text: if '音乐' in text: m.sui_ji(services, tok) else: songname = text[2:-1] m.sou_suo(services, songname, tok) elif "你知道我们现在在哪里" in text: baidu_tts().tts("我们现在在《中国少年说》的舞台上,如果我没记错的话,你回深圳的飞机票还没有买吧?", tok) elif "你要帮我买" in text: baidu_tts().tts("我可没有你的账户,但是我可以帮你查一下从北京到深圳的航班哦", tok) return "flight" elif '我想听' in text: if '音乐' in text: m.sui_ji(services, tok) else: songname = text[3:-1] m.sou_suo(services, songname, tok) else: tuling.start(text, tok) else: intent = 'no' return intent