利用Raspberry Pi 3和爬虫做 语音闹钟
2018-03-21 本文已影响45人
JaeGwen
运行环境:python3
由于有中文的编码问题,直接使用virtualenv
创建虚拟环境运行该脚本
测试通过3.5mm的音频输出text语音内容之后,加入到定时任务crontab中
就能实现当天天气预报的语音闹钟
依赖库
sudo aptitude install mplayer #语音播放器
sudo pip3 install requests lxml comtypes
#!/usr/bin/env python
#-*- encoding: utf-8 -*-
import os
import re
import time
import types
import requests
from datetime import datetime, timedelta
from lxml import etree
class NotIntegerError(Exception):
pass
class OutOfRangeError(Exception):
pass
_MAPPING = (u'零', u'一', u'二', u'三', u'四', u'五', u'六', u'七', u'八', u'九', )
_P0 = (u'', u'十', u'百', u'千', )
_S4 = 10 ** 4
_nagetive_S4 = -10 ** 4
_MIN, _MAX = 0, 99999
def _to_chinese4(num):
'''转换[0, 10000)之间的阿拉伯数字
'''
assert(0 <= num and num < _S4)
if num < 10:
return _MAPPING[num]
else:
lst = [ ]
while num >= 10:
lst.append(num % 10)
num = num // 10
lst.append(num)
c = len(lst) # 位数
result = u''
for idx, val in enumerate(lst):
if val != 0:
result += str(_P0[idx]) + str(_MAPPING[val])
if idx < c - 1 and lst[idx + 1] == 0:
result += u'零'
return result[::-1].replace(u'一十', u'十')
def _nagetive_to_chinese4(num):
'''转换[-10000, 0)之间的阿拉伯数字
'''
assert(_nagetive_S4 <= num and num < 0)
abs_num = abs(num)
if abs_num < 10:
return u'零下' + _MAPPING[abs_num]
else:
lst = [ ]
while abs_num >= 10:
lst.append(abs_num % 10)
abs_num = abs_num // 10
lst.append(abs_num)
c = len(lst)
result = u''
for idx, val in enumerate(lst):
if val != 0:
result += str(_P0[idx]) + str(_MAPPING[val])
if idx < c - 1 and lst[idx + 1] == 0:
result += u'零'
return u'零下' + result[::-1].replace(u'一十', u'十')
def trans2cn(num):
'''
if type(abs(num)) != types.IntType and type(abs(num)) != types.LongType:
raise NotIntegerError(u'%s is not a integer.' % num)
if abs(num) < _MIN or abs(num) > _MAX:
raise OutOfRangeError(u'%d is out of range[%d,%d)' % (num, _MIN, _MAX))
'''
if 0 <= num < _S4:
return _to_chinese4(num)
if _nagetive_S4 <= num < 0:
return _nagetive_to_chinese4(num)
def request_weather_html(url):
html = requests.get(url, headers=headers)
selector = etree.HTML(html.text)
temp = selector.xpath('//div [@class="wea_weather clearfix"]/em/text()')[0] #温度
temp = trans2cn(int(temp)) #转为中文数字温度
weather = selector.xpath('//div [@class="wea_weather clearfix"]/b/text()')[0] #天气
humidity_info = selector.xpath('//div [@class="wea_about clearfix"]/span/text()')[0] #湿度
humidity = re.search(r'\d+', humidity_info).group()
humidity_zh = trans2cn(int(humidity)) #转为中文数字湿度
humidity_info.replace(' ', u'百分之').replace('%',' ')
wind = selector.xpath('//div [@class="wea_about clearfix"]/em/text()')[0] #风级
aqi = selector.xpath('//div [@class="wea_alert clearfix"]/ul/li/a/em/text()')[0] #AQI 空气质量
aqi_num = re.search(r'\d+', aqi).group()
aqi_num_zh = trans2cn(int(aqi_num))
aqi = aqi.replace(aqi_num, aqi_num_zh).replace(' ',u'空气质量')
aqi = 'AQI' + aqi
wea_tips = selector.xpath('//div [@class="wea_tips clearfix"]/em/text()')[0] #穿衣小贴士
wea_tips = wea_tips.replace(', ', ',')
now = datetime.now().date() #当天日期
text = '早上好!今天是%s年%s月%s日,天气%s,温度%s摄氏度,%s,%s,%s,%s' % \
(now.year, now.month, now.day, weather, temp, humidity_info, wind, aqi, wea_tips)
return text
def text2voice(text):
#利用Baidu的api合成语音
url = 'http://tts.baidu.com/text2audio?idx=1&tex={0}&cuid=baidu_speech_' \
'demo&cod=2&lan=zh&ctp=1&pdt=1&spd=4&per=4&vol=5&pit=5'.format(text)
# 直接播放语音
os.system('mplayer "%s"' % url)
if __name__ == '__main__':
headers = {
'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/64.0.3282.186 Safari/537.36'
}
url = 'http://tianqi.moji.com/' #墨迹天气根据访问ip定位获取实时天气信息
text = request_weather_html(url)
mp3path2 = os.path.join(os.path.dirname(__file__), 'music.mp3')
os.system('mplayer %s' % mp3path2)
text2voice(text)
输出text内容:
早上好!今天是2018年3月21日,天气晴,温度九摄氏度,湿度 59%,西风2级,AQI二十一空气质量优,天冷了,该加衣服了!
添加到定时任务
30 7 * * * /var/local/voice/bin/python3 /var/local/voice/scripts/weather_voice.py