Skip to content

Commit df174d9

Browse files
committed
✨ update weather.py
Get tomorrow weather info and run it by specified time.
1 parent 7937220 commit df174d9

File tree

1 file changed

+89
-20
lines changed

1 file changed

+89
-20
lines changed

weather.py

Lines changed: 89 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
'''
22
Author: GoogTech
33
Date: 2020-12-20 11:08:38
4-
LastEditTime: 2020-12-20 17:17:24
4+
LastEditTime: 2020-12-24 20:16:49
55
Description: Get Today Weather INFO Then Ouput Voice Prompt And Send It To Your WeChat
66
Version: v0.0.2
77
'''
88

9+
import os
910
import urllib.parse
1011
import urllib.request
1112
import gzip
@@ -15,14 +16,16 @@
1516
from aip import AipSpeech
1617
import re
1718
from config import *
19+
from apscheduler.schedulers.blocking import BlockingScheduler
1820

1921

2022
# 获取天气数据
2123
def Get_weather_data():
22-
print('=============== 天气查询 ===============')
23-
city_name = input('请输入要查询的城市名称: ')
24+
print('\n\n=============== 天气查询 ===============')
25+
# city_name = input('请输入要查询的城市名称: ')
26+
# url = 'http://wthrcdn.etouch.cn/weather_mini?city=' + urllib.parse.quote(city_name)
2427
url = 'http://wthrcdn.etouch.cn/weather_mini?city=' + urllib.parse.quote(
25-
city_name)
28+
"南京")
2629
weather_data = urllib.request.urlopen(url).read()
2730
# 读取网页数据
2831
weather_data = gzip.decompress(weather_data).decode('utf-8')
@@ -31,12 +34,13 @@ def Get_weather_data():
3134
return weather_dict
3235

3336

34-
# 获取天气情况
37+
# 获取今日天气情况
3538
def Show_weather(weather_data):
3639
weather_dict = weather_data
37-
if weather_dict.get('desc') == 'invilad-citykey':
38-
print('你输入的城市有误或未收录该城市的天气, 请您重新输入...')
39-
elif weather_dict.get('desc') == 'OK':
40+
# if weather_dict.get('desc') == 'invilad-citykey':
41+
# print('你输入的城市有误或未收录该城市的天气, 请您重新输入...')
42+
# elif weather_dict.get('desc') == 'OK':
43+
if weather_dict.get('desc') == 'OK':
4044
forecast = weather_dict.get('data').get('forecast')
4145
print('日期: ', forecast[0].get('date'))
4246
print('城市: ', weather_dict.get('data').get('city'))
@@ -48,7 +52,7 @@ def Show_weather(weather_data):
4852
'风级: ', forecast[0].get('fengli').split('CDATA')[1].split(']')
4953
[0].split('[')[1])
5054
print('风向: ', forecast[0].get('fengxiang'))
51-
print('温馨提示: ', weather_dict.get('data').get('ganmao'))
55+
# print('温馨提示: ', weather_dict.get('data').get('ganmao'))
5256
weather_forecast_txt = \
5357
'您好,您所在的城市是%s, ' \
5458
'天气%s, ' \
@@ -59,18 +63,53 @@ def Show_weather(weather_data):
5963
'风向为%s, ' \
6064
'温馨提示: %s' % \
6165
(
62-
weather_dict.get('data').get('city'),
63-
forecast[0].get('type'),
64-
weather_dict.get('data').get('wendu') + '℃ ',
65-
forecast[0].get('high'),
66-
forecast[0].get('low'),
67-
forecast[0].get('fengli').split('CDATA')[1].split(']')[0].split('[')[1],
68-
forecast[0].get('fengxiang'),
69-
weather_dict.get('data').get('ganmao')
66+
weather_dict.get('data').get('city'),
67+
forecast[0].get('type'),
68+
weather_dict.get('data').get('wendu') + '℃ ',
69+
forecast[0].get('high'),
70+
forecast[0].get('low'),
71+
forecast[0].get('fengli').split('CDATA')[1].split(']')[0].split('[')[1],
72+
forecast[0].get('fengxiang'),
73+
weather_dict.get('data').get('ganmao')
7074
)
7175
return weather_forecast_txt
7276

7377

78+
# 获取明日天气情况( 代码有待重构哟 )
79+
def Show_weather_tomorrow(weather_data):
80+
weather_dict = weather_data
81+
if weather_dict.get('desc') == 'OK':
82+
forecast = weather_dict.get('data').get('forecast')
83+
print('明天日期: ', forecast[1].get('date'))
84+
print('城市: ', weather_dict.get('data').get('city'))
85+
print('明天天气: ', forecast[1].get('type'))
86+
print('明天温度: ', weather_dict.get('data').get('wendu') + '℃ ')
87+
print('明天高温: ', forecast[1].get('high'))
88+
print('明天低温: ', forecast[1].get('low'))
89+
print(
90+
'明天风级: ', forecast[1].get('fengli').split('CDATA')[1].split(']')
91+
[0].split('[')[1])
92+
print('明天风向: ', forecast[1].get('fengxiang'))
93+
weather_forecast_tomorrow_txt = \
94+
'您好,接下来播报的是明天的天气预报,您所在的城市是%s, ' \
95+
'明天天气%s, ' \
96+
'温度为%s, ' \
97+
'最高温度为%s, ' \
98+
'最低温度为%s, ' \
99+
'风级为%s, ' \
100+
'风向为%s, ' % \
101+
(
102+
weather_dict.get('data').get('city'),
103+
forecast[1].get('type'),
104+
weather_dict.get('data').get('wendu') + '℃ ',
105+
forecast[1].get('high'),
106+
forecast[1].get('low'),
107+
forecast[1].get('fengli').split('CDATA')[1].split(']')[0].split('[')[1],
108+
forecast[1].get('fengxiang'),
109+
)
110+
return weather_forecast_tomorrow_txt
111+
112+
74113
# 百度语音播报今天天气状况
75114
def Voice_broadcast(weather_forcast_txt):
76115
weather_info = weather_forcast_txt
@@ -90,21 +129,51 @@ def Voice_broadcast(weather_forcast_txt):
90129
f.close()
91130
# 使用 playsound 模块播放语音
92131
playsound.playsound(BAIDU_TTS_MP3)
132+
# 删除 BAIDU_TTS_MP3 文件, 防止 PermissionError: [Errno 13] Permission denied: '.tts.mp3'
133+
# refer to: https://www.it1352.com/1641930.html
134+
os.remove(BAIDU_TTS_MP3)
93135

94136

95137
# 将获取的天气信息推送到微信
96138
def SendToWeChat(weather_forecast_txt):
97139
# text 为推送的 title, desp 为推送的 description
98-
title = "今日天气预报来咯~"
140+
title = "天气预报来咯~"
99141
content = weather_forecast_txt
100142
data = {"text": title, "desp": content}
101143
# 发送( 同样内容的消息一分钟只能发送一次, 服务器只保留一周的消息记录 )
102144
return True if requests.post(SERVER_API,
103145
data=data).status_code == 200 else False
104146

105147

106-
# 主函数
107-
if __name__ == '__main__':
148+
# 获取今日天气预报信息并播报
149+
def run_today():
108150
weather_data = Get_weather_data()
109151
weather_forecast_txt = Show_weather(weather_data)
110152
Voice_broadcast(weather_forecast_txt)
153+
154+
155+
# 获取明日天气预报信息并播报
156+
def run_tomorrow():
157+
weather_data = Get_weather_data()
158+
weather_forecast_tomorrow_txt = Show_weather_tomorrow(weather_data)
159+
Voice_broadcast(weather_forecast_tomorrow_txt)
160+
161+
162+
# 主函数
163+
if __name__ == '__main__':
164+
scheduler = BlockingScheduler()
165+
# scheduler.add_job(run, 'interval', seconds = 90, id = 'job-one') # 每 90 秒执行一次,用于测试
166+
# scheduler.add_job(run, 'cron', hour='08-22', minute='10', second = '00', id = 'job-one') # 每天 08:10:00 和 22:10:00 点分别执行一次
167+
scheduler.add_job(run_today,
168+
'cron',
169+
hour='08',
170+
minute='10',
171+
second='00',
172+
id='job-today')
173+
scheduler.add_job(run_tomorrow,
174+
'cron',
175+
hour='22',
176+
minute='10',
177+
second='00',
178+
id='job-tomorrow')
179+
scheduler.start()

0 commit comments

Comments
 (0)