通过python,每天定时发送今明俩天的天气信息
测试的时候可以发送给自己,后面可以发送给指定的人或群发
from __future__ import unicode_literals
from threading import Timer
from wxpy import *
import requests
import urllib.request
import json
def get_weather(city_code):
url = "http://t.weather.sojson.com/api/weather/city/" + city_code
#print(url)
content = urllib.request.urlopen(url).read()
all_data =json.loads(content)
today = all_data['date']
#print(today)
city_info = all_data['cityInfo']
city = city_info['city']
#print(city)
data = all_data['data']
temperature = data['wendu']
#print(data['wendu'])
humidity = data['shidu']
#print(humidity)
air_quality = data['quality']
#print(air_quality)
forecast_info = data['forecast']
#print(forecast_info)
tomorrow_info = forecast_info[1]
#print(tomorrow_info)
tomorrow_date = tomorrow_info['date']
#print(tomorrow_date)
tomorrow_sunrise = tomorrow_info['sunrise']
#print(tomorrow_sunrise)
tomorrow_sunset = tomorrow_info['sunset']
#print(tomorrow_sunset)
tomorrow_date = tomorrow_info['date']
#print(tomorrow_date)
tomorrow_low = tomorrow_info['low']
#print(tomorrow_low)
tomorrow_high = tomorrow_info['high']
#print(tomorrow_high)
return ("日期:{0}\n城市:{1}\n温度:{2}\n湿度:{3}\n 空气质量:{4}\n\
明天:{5}\n明天气温:{6}~{7}\n明天日出:{8}\n明天日落:{9}"\
.format(today, city,temperature,humidity,air_quality,\
tomorrow_date,tomorrow_low,tomorrow_high,tomorrow_sunrise,tomorrow_sunset))
def send_news():
'''城市码可以在网上查询'''
codes = ["101280301","101280303","101280304","101280401","101280601"]
data = get_weather(codes[0])
bot = Bot(cache_path=True)
'''这是是发送给文件助手,也可以发送给指定的朋友或群发'''
myself = bot.self
bot.file_helper.send(data)
'''一天86400秒,测试的时候可以改短'''
t = Timer(86400, send_news)
t.start()
if __name__ == "__main__":
send_news()