标签:utf-8 baidu mp3 while for div key ctp seconds
1 # -*- coding:utf-8 -*- 2 import sys 3 import requests 4 import json 5 import mp3play 6 import time 7 8 def talk(info): 9 appkey = "e5ccc9c7c8834ec3b08940e290ff1559" 10 headers = { 11 ‘User-Agent‘: ‘Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit‘ 12 ‘/537.36 (KHTML, like Gecko) Chrome/53.0.2785.143 Safar‘ 13 ‘i/537.36‘, 14 } 15 url = "http://www.tuling123.com/openapi/api?key=%s&info=%s" %(appkey, info) 16 content = requests.get(url, headers=headers) 17 answer = json.loads(content.text) 18 baidu_api = ‘http://tts.baidu.com/text2audio?idx=1&tex={0}&cuid=baidu_speech_‘ 19 ‘demo&cod=2&lan=zh&ctp=1&pdt=1&spd=4&per=4&vol=5&pit=5‘.format(answer[‘text‘]) 20 res = requests.get(baidu_api, headers=headers) 21 with open(r‘E:\python_demo\tuling.mp3‘, ‘wb‘) as f: 22 f.write(res.content) 23 return answer[‘text‘] 24 25 def main(info): 26 info = info.decode(‘gb2312‘).encode(‘utf-8‘) 27 answer = talk(info) 28 mp3 = mp3play.load(r‘E:\python_demo\tuling.mp3‘) 29 mp3.play() 30 time.sleep(min(40, mp3.seconds())) 31 mp3.stop() 32 try: 33 print ‘Tuling: %s‘ % answer 34 except UnicodeEncodeError: 35 print u‘我说了个啥子哟。‘ 36 37 if __name__ == ‘__main__‘: 38 print u‘开始你的表演。‘ 39 while True: 40 info = raw_input(‘Studog:‘) 41 if info == ‘88‘: 42 break 43 main(info)
标签:utf-8 baidu mp3 while for div key ctp seconds
原文地址:http://www.cnblogs.com/laresh/p/6526409.html