From 87a720b339efb03b78b72b568984543b2344ccf5 Mon Sep 17 00:00:00 2001 From: ZhehaoMi <1125263974@qq.com> Date: Wed, 20 Feb 2019 14:29:18 +0800 Subject: [PATCH] whether --- plugins/whether/config_default.py | 3 ++ plugins/whether/whether.py | 74 +++++++++++++++++++++++++++++++ 2 files changed, 77 insertions(+) create mode 100644 plugins/whether/config_default.py create mode 100644 plugins/whether/whether.py diff --git a/plugins/whether/config_default.py b/plugins/whether/config_default.py new file mode 100644 index 0000000..5536780 --- /dev/null +++ b/plugins/whether/config_default.py @@ -0,0 +1,3 @@ +# 和风天气 https://www.heweather.com + +KEY = "" diff --git a/plugins/whether/whether.py b/plugins/whether/whether.py new file mode 100644 index 0000000..afbce71 --- /dev/null +++ b/plugins/whether/whether.py @@ -0,0 +1,74 @@ +from register import command +from global_vars import CONFIG +import threading +import urllib +config = CONFIG[__name__] + +def plugin(): + return { + "author": "Antares", + "version": 1.0, + "description": "天气查询" + } + +def get_now_whether(local, key): + url = "https://free-api.heweather.net/s6/weather/now?location=%s&key=%s" % (str(urllib.parse.quote(local)), key) + import json + data = None + with urllib.request.urlopen(url) as f: + data = json.JSONDecoder().decode(f.read().decode("utf-8")) + return data['HeWeather6'][0] + +def get_air(local, key): + url = "https://free-api.heweather.net/s6/air/now?location=%s&key=%s" % (str(urllib.parse.quote(local)), key) + import json + data = None + with urllib.request.urlopen(url) as f: + data = json.JSONDecoder().decode(f.read().decode("utf-8")) + return data['HeWeather6'][0] + +def get_forecast_whether(local, key): + url = "https://free-api.heweather.net/s6/weather/forecast?location=%s&key=%s" % (str(urllib.parse.quote(local)), key) + import json + data = None + with urllib.request.urlopen(url) as f: + data = json.JSONDecoder().decode(f.read().decode("utf-8")) + return data['HeWeather6'][0] + +@command(name="天气", help="查询天气") +def whether(bot, context, args): + + while args[-1] == "": + del args[-1] + + def handle(): + now_whether = get_now_whether(args[1], config.KEY) + forecast_whether = get_forecast_whether(args[1], config.KEY) + + if not now_whether['status'] == "ok": + bot.send(context,"Error: %s"% now_whether['status']) + return + + now_air = get_air(now_whether['basic']['parent_city'], config.KEY) + + location_data = "查询位置:%s,%s,%s,%s\n时区:%s\n更新时间:%s\n" % ( + now_whether['basic']['location'], now_whether['basic']['parent_city'], + now_whether['basic']['admin_area'], now_whether['basic']['cnty'], + now_whether['basic']['tz'], now_whether['update']['loc']) + + now_data = "当前天气:%s\n当前温度:%s摄氏度\n风向风力:%s %s级\n空气质量:%s\n空气质量指数(AQI):%s\n" % ( + now_whether['now']['cond_txt'], now_whether['now']['tmp'], + now_whether['now']['wind_dir'], now_whether['now']['wind_sc'], + now_air['air_now_city']['qlty'], now_air['air_now_city']['aqi']) + + more_days = [] + for item in forecast_whether['daily_forecast']: + more_days.append("天(%s):\n白天天气:%s\n夜间天气:%s\n最高温度:%s摄氏度\n最低温度:%s摄氏度" %( + item['date'],item['cond_txt_d'],item['cond_txt_n'],item['tmp_max'],item['tmp_min'] + )) + + message="%s\n%s\n最近三天:\n今%s\n明%s\n后%s" % ( + location_data, now_data, more_days[0], more_days[1], more_days[2]) + bot.send(context,message) + + threading.Thread(target=handle).start() -- Gitee