def main(tok): bt = baidu_tts() bs = baidu_stt(1, 'a', 2, '{') r = recorder() selfset = setting.setting() host = 'https://api.seniverse.com/v3/weather/now.json?key=' key = selfset['weather']['key'] data = {'key': key, 'location': 'ip', 'language': 'zh-Hans', 'unit': 'c'} r = requests.get(host, data=data) json = r.json() bt.tts( ',今天的天气是,' + json['results'][0]['now']['text'] + ',温度是,' + json['results'][0]['now']['temperature'] + ',摄氏度,', tok) speaker.speak()
def choose_command(self, text, tok): m = maps() bt = baidu_tts() bs = baidu_stt(1, 2, 3, 4) r = recorder() if '从' in text and '怎么去' in text: m.go_way(text[0:-7], text[-8:-4], tok) elif '我在哪' in text or '我的位置,' == text: m.ip_loc(tok) elif '从' in text and '怎么走' in text: m.go_way(text[0:-7], text[-8:-4], tok) elif '附近' in text: m.near(text[0:5], tok) else: bt.tts('对不起,暂时不支持该功能', tok) speaker.speak()
def main(tok): bt = baidu_tts() yt = youdao_tts() bs = baidu_stt(1, 2, 3, 4) r = recorder() selfset = setting.setting() hash = hashlib.md5() bt.tts('您好,请说出您的快递单号和快递公司', tok) speaker.speak() speaker.ding() r.exrecord() speaker.dong() idss = bs.stt('./voice.wav', tok) ids = number_choose(ids, tok) requestData = { 'OrderCode': ids, 'ShipperCode': service, 'LogisticCode':'12345678' } data = { 'EBusinessID': '1349773', 'RequestType': '1002', 'RequestData': parse.quote(str(requestData)), 'DataType': '2', } strings = str(requestData) + '1f0c5c35-67a8-495f-b3ab-a7fc534a826f' string = strings.encode(encoding='UTF-8',errors='strict') hashs.update(string) s = hashs.hexdigest().encode('utf-8') y = base64.b64encode(s) z = str(y, 'utf-8') data['DataSign'] = parse.quote(z, 'utf-8') r = requests.post('http://api.kdniao.cc/Ebusiness/EbusinessOrderHandle.aspx', data=data) json = r.json() try: bt.tts(json['Traces'][-1]['AcceptStation'], tok) speaker.speak() except KeyError: bt.tts('对不起,包裹信息查询失败', tok) speaker.speak()
def start(self, tok): #判断 bt = baidu_tts() bs = baidu_stt(1, 2, 3, 4) r = recorder() h = hass() welcome = '欢迎使用小蓝专用智能家居控制系统!请在,滴,一声之后说出指令' bt.tts(welcome, tok) speaker.speak() speaker.ding() r.record() speaker.dong() text = bs.stt('./voice.wav', tok) while text == None: sorry = '对不起,我没有听清楚,请重复一遍' bt.tts(sorry, tok) speaker.speak() speaker.ding() r.record() speaker.speak() text = bs.stt('./voice.wav', tok) if text != None: break texts = str.encode(encoding='UTF-8', errors='strict') if '打开' in text: cortolthings = texts[6:-2] cortolmode = 'turn_on' h.cortol(cortolthings, cortolmode, tok) elif '关闭' in text: cortolthings = texts[6:-2] cortolmode = 'turn_off' h.cortol(cortolthings, cortolmode, tok) elif '查看' in text: if '传感器' in text: getstatethings = texts[6:-5] getmode = 'sensor' h.sensor(getstatethings, tok) if '红外' in text: if '学习' in text: h.study(tok) else: h.usuallycortol(text, tok)
def sou_suo(self, services, song_name, tok): bt = baidu_tts() bs = baidu_stt(1, 2, 3, 4) r = recorder() m = xlMusic() url = 'http://tingapi.ting.baidu.com/v1/restserver/ting?' if song_name == '' or song_name == None: bt.tts('请问您要听什么歌?', tok) speaker.speak() speaker.ding() r.record() speaker.dong() song_name = bs.stt('./voice.wav', tok) else: get_song_id_rawj = requests.get(url + services['search'] + song_name) get_song_id_j = get_song_id_rawj.json() try: id = get_song_id_j['song'][song_name_c]['songid'] except KeyError: try: id = get_song_id_j['song'][0]['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(self, tok): bt = baidu_tts() bs = baidu_stt(1, 2, 3, 4) r = recorder() m = maps() bt.tts('欢迎使用小蓝地图技能', tok) speaker.speak() bt.tts('支持查找路线,附近有什么服务等,如:从这里到美丽公园怎么走?,又比如:美丽公园附近有什么吃的?', tok) speaker.speak() bt.tts('请说出您的需要', tok) speaker.speak() speaker.ding() r.record() speaker.dong() text = bs.stt('./voice.wav', tok) command = m.choose_command(text, tok)
def paly(self, song_name, tok): bt = baidu_tts() bs = baidu_stt(1, 2, 3, 4) r = recorder() m = xlMusic() while song_name != None: speaker.play() bt.tts('请问还要听什么吗?可以输入指令,如,这首歌叫什么,或者,搜索歌曲', tok) speaker.speak() speaker.ding() r.record() speaker.dong() m.command_choose(bs.stt('./voice.wav', tok), tok) else: bt.tts('对不起,发生了故障', tok) speaker.speak()
def sensor(self, getstatethings, 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'} e_id = e_id() getstatesthings = unicode(getstatethings, "utf-8", "ignore") getstatesthings_eid = e_id[getstatesthings] getstatesthings_l = getstatesthings_eid.encode('utf-8') service = '/api/states' + getstatesthings_l r = requests.get(url + ':' + port + service, headers=headers) json = r.json() if cortolback.status_code == 200 or cortolback.status_code == 201: state = json['state'] if state == 'on': say = '此设备为开启状态' bt.tts(say, tok) speaker.speak() elif state == 'off': say = '此设备为关闭状态' bt.tts(say, tok) speaker.speak() elif state == 'below_horizon': say = '太阳已经下山了' bt.tts(say, tok) speaker.speak() else: if state.isdigit() == True: say = '数据是' + state bt.tts(say, tok) speaker.speak() else: sayback = '执行错误' bt.tts(sayback, tok) speaker.speak()
def main(self, tok): bt = baidu_tts() bs = baidu_stt(1, 2, 3, 4) r = recorder() h = hass() bt.tts('欢迎使用小蓝专用智能家居控制系统!请在,滴,一声之后说出指令,目前仅支持homeassistant', tok) speaker.speak() speaker.ding() r.record() speaker.dong() commands = bs.stt('./voice.wav', tok) while commands == None: sorry = '对不起,我没有听清楚,请重复一遍' bt.tts(sorry, tok) speaker.speak() speaker.ding() r.record() speaker.speak() commands = bs.stt('./voice.wav', tok) if commands != None: break else: pass #print commands[6:-2] command = hass.choose_command(commands) if command == 'turn_on': h.cortol('turn_on', commands[6:-2], tok) elif command == 'turn_off': h.cortol('turn_off', commands[6:-2], tok) elif command == 'get_sensor': if '传感器' in command: getstatemode = 'sensor' else: getstatemode = 'switch' h.sensor(getstatemode, commands[6:-3], tok)
def main(tok): setting = selfset.setting() APPKEY = selfset['news']['key'] url = 'http://v.juhe.cn/toutiao/index?type=' r = recorder() bt = baidu_tts() bs = baidu_stt(1, 'a', 2, '{') bt.tts('请问您要听什么新闻?', tok) speaker.speak() speaker.ding() r.record() speaker.dong() text = bs.stt('./voice.wav', tok) if '国内新闻' in text: r = requests.post(url + 'guonei' + '&key=' + APPKEY) json = r.json() bt.tts(json['result']['data'][random.randint(0, 9)]['title'], tok) speaker.speak() elif '国际新闻' in text: r = requests.post(url + 'guoji' + '&key=' + APPKEY) json = r.json() saytext = json['result']['data'][random.randint(0, 9)]['title'] bt.tts(saytext, tok) speaker.speak() elif '科技新闻' in text: r = requests.post(url + 'keji' + '&key=' + APPKEY) json = r.json() bt.tts(json['result'][random.randint(0, 9)]['title'], tok) speaker.speak() elif '体育新闻' in text: r = requests.post(url + 'tiyu' + '&key=' + APPKEY) json = r.json() bt.tts(json['result'][random.randint(0, 9)]['title'], tok) speaker.speak() else: r = requests.post(url + 'top' + '&key=' + APPKEY) json = r.json() bt.tts(json['result'][random.randint(0, 9)]['title'], tok) speaker.speak()
def main(tok): bt = baidu_tts() bs = baidu_stt(1, 'a', 2, '{') r = recorder() host = 'https://api.seniverse.com/v3/weather/now.json?key=' key = setting.setting()['weather']['key'] APIURL = key + '&location=ip&language=zh-Hans&unit=c' url = host + APIURL r = requests.get(url) json = r.json() print json weather = json['results'][0]['now']['text'] temperature = json['results'][0]['now']['temperature'] tweatherstates = ',今天,' + weather + ',温度是,' + temperature + ',摄氏度,' bt.tts(saytext, tok) speaker.speak()
def main(tok): bt = baidu_tts() yt = youdao_tts() bs = baidu_stt(1, 2, 3, 4) r = recorder() selfset = setting.setting() hash = hashlib.md5() bt.tts('您好,请说出您要查询的快递单号', tok) speaker.speak() speaker.ding() r.exrecord() speaker.dong() requestData = { 'OrderCode': '', 'ShipperCode': 'YTO', 'LogisticCode': number_choose(bs.stt('./voice.wav', tok), tok) } data = { 'EBusinessID': selfset['express']['EBusinessID'], 'RequestType': '1002', 'RequestData': urllib.urlencode(str(requestData)), 'DataType': '2', } hash.update(str(requestData) + selfset['express']['key'], encoding='utf-8') data['DataSign'] = urllib.urlencode(base64.b64encode(hash.hexdigest())) json = requests.post( 'http://api.kdniao.cc/Ebusiness/EbusinessOrderHandle.aspx', data=data, headers='application/x-www-form-urlencoded;charset=utf-8') try: bt.tts(json['Traces'][-1]['AcceptStation'], tok) speaker.speak() except KeyError: bt.tts('对不起,包裹信息查询失败', tok) speaker.speak()
def sensor(self, getstatemode, getstatethings, 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'} if passwd != None or passwd != '': e_id = h.e_id(tok) service = '/api/states' + e_id[getstatethings] r = requests.get(url + ':' + port + service, headers=headers) if cortolback.status_code == 200 or cortolback.status_code == 201: json = r.json() if json['state'] == 'turn_on' and getstatemode == 'switch': bt.tts(getstatethins + '为开启状态', tok) speaker.speak() elif json['state'] == 'turn_on' and getstatemode == 'switch': bt.tts(getstatethings + '为关闭状态', tok) speaker.speak() elif json['state'] == 'turn_on' and getstatemode == 'sensor': bt.tts(getstatethings + '为关闭状态', tok) speaker.speak() elif json['state'].isdigit() == True: bt.tts(getstatethings + '的数据是' + json['state'], tok) speaker.speak() else: bt.tts('对不起,暂时不支持读取改设备的状态', tok) speaker.speak() else: bt.tts('执行错误', tok) speaker.speak() else: pass
def main(tok): url = 'http://v.juhe.cn/joke/content/list.php' key = '21e780488098fa8b77394405421d8ae1' bt = baidu_tts() bs = baidu_stt(1, 'a', 2, '{') chose = random.randint(0, 9) r = requests.get(url + "key=" + key + "&page=10&pagesize=10&sort=asc&time=" + str(int(time.time()))) json = r.json() if chose == 0: saytext = json['result']["data"][0]['content'] elif chose == 1: saytext = json['result']["data"][1]['content'] elif chose == 2: saytext = json['result']["data"][2]['content'] elif chose == 3: saytext = json['result']["data"][3]['content'] elif chose == 4: saytext = json['result']["data"][4]['content'] elif chose == 5: saytext = json['result']["data"][5]['content'] elif chose == 6: saytext = json['result']["data"][6]['content'] elif chose == 7: saytext = json['result']["data"][7]['content'] elif chose == 8: saytext = json['result']["data"][8]['content'] elif chose == 9: saytext = json['result']["data"][9]['content'] else: saytext = json['result']["data"][0]['content'] bt.tts(saytext, tok) speaker.speak()
def main(tok): url = 'http://api.avatardata.cn/Joke/QueryJokeByTime?key=' setting = setting.setting() key = setting['joke']['key'] bt = baidu_tts() bs = baidu_stt(1, 'a', 2, '{') chose = random.randint(0, 9) r = requests.post(url + key + '&sort=asc&time=1418745237') json = r.json() if chose == 0: saytext = json['result'][0]['content'] elif chose == 1: saytext = json['result'][1]['content'] elif chose == 2: saytext = json['result'][2]['content'] elif chose == 3: saytext = json['result'][3]['content'] elif chose == 4: saytext = json['result'][4]['content'] elif chose == 5: saytext = json['result'][5]['content'] elif chose == 6: saytext = json['result'][6]['content'] elif chose == 7: saytext = json['result'][7]['content'] elif chose == 8: saytext = json['result'][8]['content'] elif chose == 9: saytext = json['result'][9]['content'] else: saytext = json['result'][0]['content'] saytext = jokefj + '第二个笑话' + jokesj bt.tts(saytext, tok) speaker.speak()
def send(self, mail_host, mail_user, mail_pass, mail_port, sender, tok): bt = baidu_tts() bs = baidu_stt(1, 2, 3, 4) r = recorder() e = email() receivers = [] bt.tts('请问您要给谁发送邮件呢?格式为,邮箱数字加邮箱服务商,如,一五二六七八九,QQ邮箱', tok) speaker.speak() speaker.ding() r.tsrecord() speaker.dong() receivers[0] = bs.stt('./voice.wav', tok) bt.tts('请问您要发送什么内容?', tok) speaker.speak() speaker.ding() r.tsrecord() speaker.dong() messages = bs.stt('./voice.wav', tok) message = MIMEText(messages, 'plain', 'utf-8') message['From'] = Header("小蓝邮件代发", 'utf-8') message['To'] = Header("You", 'utf-8') subject = '小蓝邮件代发' message['Subject'] = Header(subject, 'utf-8') try: smtpObj = smtplib.SMTP() smtpObj.connect(mail_host, mail_port) smtpObj.login(mail_user,mail_pass) smtpObj.sendmail(sender, receivers, message.as_string()) bt.tts('邮件发送成功', tok) speaker.speak() except smtplib.SMTPException: bt.tts('无法发送邮件', tok) speaker.speak()
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, 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 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 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 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)