操作步骤:
百度的天气接口:http://api.map.baidu.com/telematics/v3/weather?location=city&output=json&ak=api_key
百度的语音合成接口:http://tsn.baidu.com/text2audio?tex=text&lan=zh&cuid=cuid&ctp=1&tok=token
python脚本内容
#!/usr/bin/python3
# -*- coding: utf-8 -*-
import os
import urllib.request
import sys
import json
import time
#get token
def get_token():
api_key="your api key"
sec_key="your secret key"
auth_url="https://openapi.baidu.com/oauth/2.0/token?grant_type=client_credentials&client_id="+api_key+"&client_secret="+sec_key
res = urllib.request.urlopen(auth_url)
#print(res.read())
json_data = res.read().decode('utf-8')
#print(json_data)
result = json.loads(json_data)['access_token']
return result
def get_weather(city):
api_key="your api key"
weather_url="http://api.map.baidu.com/telematics/v3/weather?location="+city+"&output=json&ak="+api_key
res = urllib.request.urlopen(weather_url)
json_data = res.read().decode('utf-8')
#print(json_data)
json_data = json.loads(json_data)
#get json data
#check result status
status = json_data['status']
isTom = time.localtime().tm_hour>18
index = 1 if isTom else 0
initStr = '明天' if isTom else '今天'
if status=='success':
results=json_data['results'][0]
pm25=results['pm25']
weather_data=results['weather_data']
date=weather_data[index]['date']
weather=weather_data[index]['weather']
wind=weather_data[index]['wind']
temperature=weather_data[index]['temperature'].replace('~','到')
if not isTom:
return'{}是{} PM2.5是{} 天气{} {} 温度为{}'.format(initStr,date,pm25,weather,wind,temperature)
else:
return'{}是{} 天气{} {} 温度为{}'.format(initStr,date,weather,wind,temperature)
else:
return ''
token=get_token()
weather=get_weather('beijing')
if os.path.exists('/home/pi/weather/weather.txt'):
with open('/home/pi/weather/weather.txt','wt') as f:
f.write(weather)
f.close()
print(weather)
else:
print('weather.txt not exists')
#tts
url='http://tsn.baidu.com/text2audio?tex='+weather+'&lan=zh&cuid=raspberrypi2&ctp=1&tok='+token
#print(url)
try:
os.system('/usr/bin/mplayer "%s"' %(url))
except Exception as e:
print('Exception',e)
添加到crontab 定时计划里,每天早上7:10执行
10 7 * * * /usr/local/bin/python3.5 /home/pi/weather/weather.py
v附录:百度天气API返回的JSON数据
{
"error": 0,
"status": "success",
"date": "2016-02-25",
"results": [
{
"currentCity": "beijing",
"pm25": "33",
"index": [
{
"title": "穿衣",
"zs": "较冷",
"tipt": "穿衣指数",
"des": "建议着厚外套加毛衣等服装。年老体弱者宜着大衣、呢外套加羊毛衫。"
},
{
"title": "洗车",
"zs": "较适宜",
"tipt": "洗车指数",
"des": "较适宜洗车,未来一天无雨,风力较小,擦洗一新的汽车至少能保持一天。"
},
{
"title": "旅游",
"zs": "适宜",
"tipt": "旅游指数",
"des": "天气较好,同时又有微风伴您一路同行。虽会让人感觉有点凉,但仍适宜旅游,可不要错过机会呦!"
},
{
"title": "感冒",
"zs": "易发",
"tipt": "感冒指数",
"des": "昼夜温差大,且空气湿度较大,易发生感冒,请注意适当增减衣服,加强自我防护避免感冒。"
},
{
"title": "运动",
"zs": "较适宜",
"tipt": "运动指数",
"des": "天气较好,无雨水困扰,较适宜进行各种运动,但因天气凉,在户外运动请注意增减衣物。"
},
{
"title": "紫外线强度",
"zs": "最弱",
"tipt": "紫外线强度指数",
"des": "属弱紫外线辐射天气,无需特别防护。若长期在户外,建议涂擦SPF在8-12之间的防晒护肤品。"
}
],
"weather_data": [
{
"date": "周四 02月25日 (实时:4℃)",
"dayPictureUrl": "http://api.map.baidu.com/images/weather/day/qing.png",
"nightPictureUrl": "http://api.map.baidu.com/images/weather/night/qing.png",
"weather": "晴",
"wind": "微风",
"temperature": "7 ~ -5℃"
},
{
"date": "周五",
"dayPictureUrl": "http://api.map.baidu.com/images/weather/day/duoyun.png",
"nightPictureUrl": "http://api.map.baidu.com/images/weather/night/qing.png",
"weather": "多云转晴",
"wind": "微风",
"temperature": "8 ~ -3℃"
},
{
"date": "周六",
"dayPictureUrl": "http://api.map.baidu.com/images/weather/day/duoyun.png",
"nightPictureUrl": "http://api.map.baidu.com/images/weather/night/yin.png",
"weather": "多云转阴",
"wind": "微风",
"temperature": "6 ~ -3℃"
},
{
"date": "周日",
"dayPictureUrl": "http://api.map.baidu.com/images/weather/day/duoyun.png",
"nightPictureUrl": "http://api.map.baidu.com/images/weather/night/qing.png",
"weather": "多云转晴",
"wind": "北风3-4级",
"temperature": "6 ~ -4℃"
}
]
}
]
}
还没有人评论,抢个沙发吧...