OpenWeatherMap接口API
- 接口文档
- 接口特点
外国的这个接口,付费和收费的差别是接口的访问频率限制,获得的内容是一样的!我们现在这种测试项目程序,用这个接口非常合适。接口返回的信息中,也含有weather_code,并且也提供了相应的图标,但图标比较小,不够心知天气的图标漂亮,所以选择还是用心知天气的图标进行显
示,自己做一次weather_code的手工转码。

- Weather_code转码

对应心知天气的code,自己写一个根据OWM天气代码返回心知天气代码的函数:
def convert_weather_code(argument):
"""Convert this API code to API_Thinkpage, so MainWindow can reuse the
pictures of API_Thinkpage.
Args:
A string of weather code, like '200'
Returns:
A string of weather code, like '16'
"""
switcher = {
#thunder
'200': '16',
'201': '16',
'202': '16',
'210': '16',
'211': '16',
'212': '16',
'221': '16',
'230': '16',
'231': '16',
'232': '16',
#drizzle
'300': '10',
'301': '10',
'302': '10',
'310': '10',
'311': '10',
'312': '10',
'313': '10',
'314': '10',
'321': '10',
#rain
'500': '13',
'501': '14',
'502': '15',
'503': '15',
'504': '15',
'511': '20',
'521': '10',
'522': '14',
'531': '15',
#snow
'600': '22',
'601': '23',
'602': '24',
'611': '20',
'612': '20',
'615': '20',
'616': '20',
'620': '22',
'621': '23',
'622': '24',
#atomosphere
'701': '26',
'711': '26',
'721': '31',
'731': '28',
'741': '30',
'751': '31',
'761': '26',
'762': '99',
'771': '34',
'781': '36',
#clear
'800': '0',
#clouds
'801': '5',
'802': '5',
'803': '7',
'804': '4'
}
return switcher.get(argument, '99')
99为心知天气未知状态的图片,如果未查询到对应的代码,就返回“未知”状态。
- 读取天气信息,返回自定义参数
自己将返回信息定义得和心知天气API的格式一样,窗体可以用一样的代码读取,并放入对应的控件中。
def get_weather_now(city_input):
"""Get realtime weather info from http://api.thinkpage.cn/
Args:
city_input: The name of the city input by user.
Returns:
dict_weather_now:{
"status_code": "200",
"message": "ok",
"name": city_name,
"weather": "city_weather",
"weather_code": "0",
"temp": "city_temp"
}
"""
def get_weather_forecast(city_input):
"""Get weather forecast from http://api.thinkpage.cn/
Args:
city_input: The name of the city input by user.
Returns:
dict_weather_forecast:{
"status_code": "200",
"message": "ok",
"name": city_name,
"date_d0": date_d0,
"weather_d0": city_weather_d0,
"weather_code_d0": weather_code_d0,
"temp_max_d0": temp_max_d0,
"temp_min_d0": temp_min_d0,
"date_d1": date_d1,
"weather_d1": city_weather_d1,
"weather_code_d1": weather_code_d1,
"temp_max_d1": temp_max_d1,
"temp_min_d1": temp_min_d1,
"date_d2": date_d2,
"weather_d2": city_weather_d2,
"weather_code_d2": weather_code_d2,
"temp_max_d2": temp_max_d2,
"temp_min_d2": temp_min_d2
}
"""