summaryrefslogtreecommitdiff
path: root/AyaBot/plugins/weather.py
blob: f6a78fe28d74f84d41f471d39c13aa37f7cff217 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
import json
import requests
from nonebot import on_command, CommandSession


API_URL = 'https://www.tianqiapi.com/free/day?appid=36628957&appsecret=WKn4dtVg&city='


LIST = """{city} 今日信息如下:
更新时间:{time}
天气情况:{wea}
空气质量:{air}
温度:
    现在温度:{tem}
    最高温度:{temday}
    最低温度:{temnight}
风向:{win}
风力等级:{winspeed}
风速:{winmeter}"""


@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