1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
| # -*- coding: utf-8 -*-
# @Time : 2017/1/15 15:16
# @Author : woodenrobot
import os
import re
import time
import requests
from datetime import datetime, timedelta
from bs4 import BeautifulSoup
headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit'
'/537.36 (KHTML, like Gecko) Chrome/53.0.2785.143 Safar'
'i/537.36',
}
def numtozh(num):
num_dict = {1: '一', 2: '二', 3: '三', 4: '四', 5: '五', 6: '六', 7: '七',
8: '八', 9: '九', 0: '零'}
num = int(num)
if 100 <= num < 1000:
b_num = num // 100
s_num = (num-b_num*100) // 10
g_num = (num-b_num*100) % 10
if g_num == 0 and s_num == 0:
num = '%s百' % (num_dict[b_num])
elif s_num == 0:
num = '%s百%s%s' % (num_dict[b_num], num_dict.get(s_num, ''), num_dict.get(g_num, ''))
elif g_num == 0:
num = '%s百%s十' % (num_dict[b_num], num_dict.get(s_num, ''))
else:
num = '%s百%s十%s' % (num_dict[b_num], num_dict.get(s_num, ''), num_dict.get(g_num, ''))
elif 10 <= num < 100:
s_num = num // 10
g_num = (num-s_num*10) % 10
if g_num == 0:
g_num = ''
num = '%s十%s' % (num_dict[s_num], num_dict.get(g_num, ''))
elif 0 <= num < 10:
g_num = num
num = '%s' % (num_dict[g_num])
elif -10 < num < 0:
g_num = -num
num = '零下%s' % (num_dict[g_num])
elif -100 < num <= -10:
num = -num
s_num = num // 10
g_num = (num-s_num*10) % 10
if g_num == 0:
g_num = ''
num = '零下%s十%s' % (num_dict[s_num], num_dict.get(g_num, ''))
return num
def get_seconds(h='07', m='30', s='00'):
"""获取当前时间与程序启动时间间隔秒数"""
# 设置程序启动的时分秒
time_pre = '%s:%s:%s' % (h, m, s)
# 获取当前时间
time1 = datetime.now()
# 获取程序今天启动的时间的字符串格式
time2 = time1.date().strftime('%Y-%m-%d') + ' ' + time_pre
# 转换为datetime格式
time2 = datetime.strptime(time2, '%Y-%m-%d %H:%M:%S')
# 判断当前时间是否晚于程序今天启动时间,若晚于则程序启动时间增加一天
if time1 > time2:
time2 = time2 + timedelta(days=1)
return time.mktime(time2.timetuple()) - time.mktime(time1.timetuple())
def get_weather():
# 下载墨迹天气主页源码
res = requests.get('http://tianqi.moji.com/', headers=headers)
# 用BeautifulSoup获取所需信息
soup = BeautifulSoup(res.text, "html.parser")
temp = soup.find('div', attrs={'class': 'wea_weather clearfix'}).em.getText()
temp = numtozh(int(temp))
weather = soup.find('div', attrs={'class': 'wea_weather clearfix'}).b.getText()
sd = soup.find('div', attrs={'class': 'wea_about clearfix'}).span.getText()
sd_num = re.search(r'\d+', sd).group()
sd_num_zh = numtozh(int(sd_num))
sd = sd.replace(sd_num, sd_num_zh)
wind = soup.find('div', attrs={'class': 'wea_about clearfix'}).em.getText()
aqi = soup.find('div', attrs={'class': 'wea_alert clearfix'}).em.getText()
aqi_num = re.search(r'\d+', aqi).group()
aqi_num_zh = numtozh(int(aqi_num))
aqi = aqi.replace(aqi_num, aqi_num_zh).replace(' ', ',空气质量')
info = soup.find('div', attrs={'class': 'wea_tips clearfix'}).em.getText()
sd = sd.replace(' ', '百分之').replace('%', '')
aqi = 'aqi' + aqi
info = info.replace(',', ',')
# 获取今天的日期
today = datetime.now().date().strftime('%Y年%m月%d日')
# 将获取的信息拼接成一句话
text = '早上好!今天是%s,天气%s,温度%s摄氏度,%s,%s,%s,%s' % \
(today, weather, temp, sd, wind, aqi, info)
return text
def text2voice(text):
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)
# 下载转换后的mp3格式语音
res = requests.get(url, headers=headers)
# 将MP3存入本地
with open('1.mp3', 'wb') as f:
f.write(res.content)
def main():
while True:
s = get_seconds()
time.sleep(s)
# 获取需要转换语音的文字
text = get_weather()
print(text)
# 将文字转换为语音并存入程序所在文件夹
text2voice(text)
# 获取音乐文件绝对地址
mp3path2 = os.path.join(os.path.dirname(__file__), '2.mp3')
# 先播放一首音乐做闹钟
os.system('mplayer %s' % mp3path2)
# 播报语音天气
mp3path1 = os.path.join(os.path.dirname(__file__), '1.mp3')
os.system('mplayer %s' % mp3path1)
os.remove(mp3path1)
if __name__ == '__main__':
main()
|