要实现天气预报功能,少不了要了解一下天气 API 。通过搜索,我找到了一款免费的天气 API —— 心知天气 。心知天气提供了天气、空气质量、生活指数等多种数据信息。其中逐日天气预报是免费的,可以利用来实现天气预报查询插件。
选择心知天气的另一个理由是他们的 API 文档非常详细,还提供了多种语言的 demo (连 common-lisp 都有,点个赞! )。下面是官方提供的一个 Python 版的示例:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
import requests from utils.const_value import API, KEY, UNIT, LANGUAGE from utils.helper import getLocation
deffetchWeather(location): result = requests.get(API, params={ 'key': KEY, 'location': location, 'language': LANGUAGE, 'unit': UNIT }, timeout=1) return result.text
if __name__ == '__main__': location = getLocation() result = fetchWeather(location) print(result)
其中,API 是 API 的地址,逐日天气预报的 API 地址是 https://api.seniverse.com/v3/weather/daily.json ;KEY 则是心知天气的 API 密钥,每个注册账户都可以得到一个密钥;location 是城市名,例如深圳就是 深圳 或者 shenzhen;而 language 和 unit 分别表示语言和单位,由于是可选参数,这里不做详细介绍。有兴趣的朋友请阅读官方文档。
整段代码也没有什么特别好说的:先是定义了一个 fetchWeather 函数,该函数使用 requests 模块发起 API 请求,请求超时设置为 1 秒。之后调用这个函数并打印返回的结果。
defhandle(text, mic, profile, wxbot=None): """ Responds to user-input, typically speech text Arguments: text -- user-input, typically transcribed speech mic -- used to interact with the user (for both input and output) profile -- contains information related to the user (e.g., phone number) wxbot -- wechat bot instance """ pass
defisValid(text): """ Returns True if the input is related to weather. Arguments: text -- user-input, typically transcribed speech """ pass
defisValid(text): """ Returns True if the input is related to weather. Arguments: text -- user-input, typically transcribed speech """ returnu"天气"in text
defhandle(text, mic, profile, wxbot=None): """ Responds to user-input, typically speech text Arguments: text -- user-input, typically transcribed speech mic -- used to interact with the user (for both input and output) profile -- contains information related to the user (e.g., phone number) wxbot -- wechat bot instance """ pass
defhandle(text, mic, profile, wxbot=None): """ Responds to user-input, typically speech text Arguments: text -- user-input, typically transcribed speech mic -- used to interact with the user (for both input and output) profile -- contains information related to the user (e.g., phone number) wxbot -- wechat bot instance """ logger = logging.getLogger(__name__) # get config if 'weather' not in profile or \ not profile[SLUG].has_key('key') or \ not profile[SLUG].has_key('location'): mic.say('天气插件配置有误,插件使用失败') return key = profile[SLUG]['key'] location = profile[SLUG]['location'] WEATHER_API = 'https://api.seniverse.com/v3/weather/daily.json' try: weather = fetch_weather(WEATHER_API, key, location) logger.debug("Weather report: ", weather) if weather.has_key('results'): daily = weather['results'][0]['daily'] day_text = [u'今天', u'明天', u'后天'] responds = u'%s天气:' % location for day inrange(len(day_text)): responds += u'%s:%s,%s到%s摄氏度。' % (day_text[day], daily[day]['text_day'], daily[day]['low'], daily[day]['high']) mic.say(responds) else: mic.say('抱歉,我获取不到天气数据,请稍后再试') except Exception, e: logger.error(e) mic.say('抱歉,我获取不到天气数据,请稍后再试')