#!/usr/bin/env python
import requests
import json
import schedule
import time
import json, urllib
from urllib import request
def requestw():
now = 'https://free-api.heweather.net/s6/weather/now?location=hangzhou&key=**'
forecast = 'https://free-api.heweather.net/s6/weather/forecast?location=hangzhou&key=**'
lifestyle = 'https://free-api.heweather.net/s6/weather/lifestyle?location=hangzhou&key=**'
request_now = request.Request(now)
result_now = urllib.request.urlopen(request_now)
jsonarr_now = json.loads(result_now.read())
#fl:体感温度,cond_txt:实况天气状况描述,wind_sc:风力
fl= jsonarr_now['HeWeather6'][0]['now']['fl']
tmp=jsonarr_now['HeWeather6'][0]['now']['tmp']
cond_txt= jsonarr_now['HeWeather6'][0]['now']['cond_txt']
wind_sc= jsonarr_now['HeWeather6'][0]['now']['wind_sc']
request_forecast = request.Request(forecast)
result_forecast = urllib.request.urlopen(request_forecast)
jsonarr_forecast = json.loads(result_forecast.read())
#cond_txt_d_1:明天实况天气状况描述,tmp_max_1:最高温度,
cond_txt_d_1= jsonarr_forecast['HeWeather6'][0]['daily_forecast'][1]['cond_txt_d']
tmp_max_1= jsonarr_forecast['HeWeather6'][0]['daily_forecast'][1]['tmp_max']
tmp_min_1= jsonarr_forecast['HeWeather6'][0]['daily_forecast'][1]['tmp_min']
cond_txt_d_2= jsonarr_forecast['HeWeather6'][0]['daily_forecast'][2]['cond_txt_d']
tmp_max_2= jsonarr_forecast['HeWeather6'][0]['daily_forecast'][2]['tmp_max']
tmp_min_2= jsonarr_forecast['HeWeather6'][0]['daily_forecast'][2]['tmp_min']
request_lifestyle = request.Request(lifestyle)
result_lifestyle = urllib.request.urlopen(request_lifestyle)
jsonarr_lifestyle = json.loads(result_lifestyle.read())
#comf:舒适度指数,drsg:穿衣指数
comf= jsonarr_lifestyle['HeWeather6'][0]['lifestyle'][0]['txt']
drsg= jsonarr_lifestyle['HeWeather6'][0]['lifestyle'][1]['txt']
url = 'https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=**'
headers = {'content-type': "application/json", 'Authorization': 'APP appid = **,token = **'}
body = {
"msgtype": "markdown",
"markdown": {
"content": "**今日天气状况:** "+ cond_txt +" \n"
">实时温度: "+ tmp + "摄氏度\n"
">体感温度: "+ fl + "摄氏度\n"
">风力: "+ wind_sc + "\n"
">舒适度指数: "+ comf + "\n"
">穿衣指数: "+ drsg + "\n\n"
"**明日天气状况:** "+ cond_txt_d_1 +" \n"
">明日最高温度: "+ tmp_max_1 + "摄氏度\n"
">明日最低温度: "+ tmp_min_1 + "摄氏度\n\n"
"**后日天气状况:** "+ cond_txt_d_2 +" \n"
">后日最高温度: "+ tmp_max_2 + "摄氏度\n"
">后日最低温度: "+ tmp_min_2 + "摄氏度\n"
}
}
response = requests.post(url, data = json.dumps(body), headers = headers)
schedule.every(30).minutes.do(requestw)
while True:
schedule.run_pending()
time.sleep(1)