diff options
Diffstat (limited to 'AyaBot/plugins/weather.py')
| -rw-r--r-- | AyaBot/plugins/weather.py | 54 | 
1 files changed, 39 insertions, 15 deletions
diff --git a/AyaBot/plugins/weather.py b/AyaBot/plugins/weather.py index f02198d..f6a78fe 100644 --- a/AyaBot/plugins/weather.py +++ b/AyaBot/plugins/weather.py @@ -1,22 +1,46 @@ -import re +import json +import requests  from nonebot import on_command, CommandSession -@on_command('weather', aliases=['天气', '查天气', '天气查询']) -async def weather(session: CommandSession): -    city = session.get('city', prompt='你想查哪个城市呢?') -    date = session.get('date', prompt='你想查哪一天呢?(格式:20200427)') -    await session.send('你查询的情况如下' + city) -    await session.send('你想查询的日期' + date) +API_URL = 'https://www.tianqiapi.com/free/day?appid=36628957&appsecret=WKn4dtVg&city=' -@weather.args_parser -async def _(session: CommandSession): -    if session.is_first_run: -        return +LIST = """{city} 今日信息如下: +更新时间:{time} +天气情况:{wea} +空气质量:{air} +温度: +    现在温度:{tem} +    最高温度:{temday} +    最低温度:{temnight} +风向:{win} +风力等级:{winspeed} +风速:{winmeter}""" -    if session.current_key == 'date': -        if not re.fullmatch(r'\d{8}', session.current_arg_text): -            session.pause('日期格式有误,请重新输入') -    session.args[session.current_key] = session.current_arg_text +@on_command('weather', aliases=['天气', '查天气', '天气查询'], only_to_me=False) +async def weather(session: CommandSession): +    city = session.get('city', prompt='你想查哪个城市呢?') +    try: +        res = API_URL + city +        res1 = requests.get(res) +        res1.encoding = 'utf-8' +        html = res1.text +        wt = json.loads(html) +        await session.send(LIST.format( +            city=wt["city"], +            time=wt["update_time"], +            wea=wt["wea"], +            tem=wt["tem"], +            temday=wt["tem_day"], +            temnight=wt["tem_night"], +            win=wt["win"], +            winspeed=wt["win_speed"], +            winmeter=wt["win_meter"], +            air=wt["air"] +            ) +        ) +    except: +        await session.send('获取数据时出问题,请重试') +        return
\ No newline at end of file  | 
