summaryrefslogtreecommitdiff
path: root/AyaBot/plugins/weather.py
blob: b068279fa2a5a45e134799bf0616f7bb441be867 (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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
# -*- coding:utf-8 -*-
import os
import re
import sys
sys.path.append(r'请改成你机器人的对应目录\Aya\AyaBot\plugins\Module')
import json
import requests
from nonebot import on_command, CommandSession
import google_translate

API_URL = 'https://api.weatherbit.io/v2.0/current?city='
API_URL_2 = '&key=1df2eb2951f3470a94cb323bb4647c18'
API_URL_3 = 'https://www.tianqiapi.com/free/day?appid=36628957&appsecret=WKn4dtVg&city='


LIST = """{city} 情况如下:
截至: {ob_time}
天气: {description}
温度: {temp}
风速: {wind_spd}
风向: {wind_cdir}
云覆盖率(%): {clouds}"""

LIST_ALL = """{city} 详细情况如下:
纬度(°): {lat}
经度(°): {lon}
日出时间(HH:MM): {sunrise}
日落时间(HH:MM): {sunset}
本地时区: {timezone}
源站ID: {station}
上次观察时间(YYYY-MM-DD HH:MM): {ob_time}
当前周期小时(YYYY-MM-DD HH:MM): {datetime}
压力(mb): {pres}
海平面压力(mb): {slp}
风速(m/s): {wind_spd}
风向(°): {wind_dir}
缩写风向: {wind_cdir}
风向全称: {wind_cdir_full}
温度(℃): {temp}
感觉温度(℃): {app_temp}
相对湿度(%): {rh}
露点(℃): {dewpt}
云覆盖率(%): {clouds}
一天的一部分(d/n): {pod}
现在天气: {description}
可见度(km): {vis}
液体当量沉淀速率(mm/hr): {precip}
降雪(mm/h): {snow}
紫外线指数(0-11+): {uv}
空气质量指数[美国-EPA标准0-+500] [Clear Sky]: {aqi}
漫射水平太阳辐照度(W/m^2) [Clear Sky]: {dhi}
普通太阳直射辐射(W/m^2) [Clear Sky]: {dni}
全球水平太阳辐照度(W/m^2): {ghi}
估计的太阳辐射(W/m^2): {solar_rad}
太阳斜角(°): {elev_angle}
太阳时角(°): {h_angle}"""

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


@on_command('weather', aliases=['天气'], only_to_me=False)
async def weather(session: CommandSession):
    cy_ct = session.get('weather', prompt='输入的信息似乎出问题了呢...例:天气 CN 北京 或 天气 JP 东京')
    ms = cy_ct.split(' ')
    try:
        country = ms[0]
        city = ms[1]
        re_msg = google_translate.translate(city[:4999], to='en', source='zh-CN')
        URL = API_URL + re_msg[0] + ',' + country + API_URL_2
        # print(URL)
        res = requests.get(URL)
        res.encoding = 'utf-8'
        html = res.text
        wt = json.loads(html)
        await session.send(LIST.format(
            city=wt["data"][0]["city_name"],
            ob_time=wt["data"][0]["ob_time"],
            description=wt["data"][0]["weather"]["description"],
            temp=wt["data"][0]["temp"],
            wind_spd=wt["data"][0]["wind_spd"],
            wind_cdir=wt["data"][0]["wind_cdir"],
            clouds=wt["data"][0]["clouds"],
            )
        )
    except:
        await session.send('搜索似乎出问题了呢...请重试')

@on_command('wtlist', aliases=['天气详细'])
async def wtlist(session: CommandSession):
    cy_ct = session.get('wtlist', prompt='输入的信息似乎出问题了呢...例:天气 CN 北京 或 天气 JP 东京')
    ms = cy_ct.split(' ')
    try:
        country = ms[0]
        city = ms[1]
        re_msg = google_translate.translate(city[:4999], to='en', source='zh-CN')
        URL = API_URL + re_msg[0] + ',' + country + API_URL_2
        # print(URL)
        res = requests.get(URL)
        res.encoding = 'utf-8'
        html = res.text
        wt = json.loads(html)
        await session.send(LIST_ALL.format(
            rh=wt["data"][0]["rh"],
            pod=wt["data"][0]["pod"],
            lon=wt["data"][0]["lon"],
            pres=wt["data"][0]["pres"],
            timezone=wt["data"][0]["timezone"],
            ob_time=wt["data"][0]["ob_time"],
            clouds=wt["data"][0]["clouds"],
            solar_rad=wt["data"][0]["solar_rad"],
            city=wt["data"][0]["city_name"],
            wind_spd=wt["data"][0]["wind_spd"],
            last_ob_time=wt["data"][0]["last_ob_time"],
            wind_cdir_full=wt["data"][0]["wind_cdir_full"],
            wind_cdir=wt["data"][0]["wind_cdir"],
            temp=wt["data"][0]["temp"],
            slp=wt["data"][0]["slp"],
            vis=wt["data"][0]["vis"],
            h_angle=wt["data"][0]["h_angle"],
            sunset=wt["data"][0]["sunset"],
            dni=wt["data"][0]["dni"],
            dewpt=wt["data"][0]["dewpt"],
            snow=wt["data"][0]["snow"],
            uv=wt["data"][0]["uv"],
            precip=wt["data"][0]["precip"],
            wind_dir=wt["data"][0]["wind_dir"],
            sunrise=wt["data"][0]["sunrise"],
            ghi=wt["data"][0]["ghi"],
            dhi=wt["data"][0]["dhi"],
            aqi=wt["data"][0]["aqi"],
            lat=wt["data"][0]["lat"],
            description=wt["data"][0]["weather"]["description"],
            datetime=wt["data"][0]["datetime"],
            station=wt["data"][0]["station"],
            elev_angle=wt["data"][0]["elev_angle"],
            app_temp=wt["data"][0]["app_temp"],
            )
        )
    except:
        await session.send('搜索似乎出问题了呢...请重试')


@on_command('weathercn', aliases=['国内天气详细'], only_to_me=False)
async def weathercn(session: CommandSession):
    city = session.get('weathercn', prompt='输入格式似乎有问题呢...例:国内天气详细 北京')
    ms = city.split(' ')
    try:
        msg = ms[0]
        print(msg)
        res = API_URL_3 + msg
        print(res)
        res1 = requests.get(res)
        res1.encoding = 'utf-8'
        html = res1.text
        wt = json.loads(html)
        await session.send(LIST_CN.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



@weather.args_parser
async def _(session: CommandSession):
    stripped_arg = session.current_arg_text.strip()
    if session.is_first_run:
        if stripped_arg:
            session.state['weather'] = stripped_arg
        return

    if not stripped_arg:
        session.pause('要查询的城市不能为空,请重新输入')
    session.state[session.current_key] = stripped_arg

@wtlist.args_parser
async def _(session: CommandSession):
    stripped_arg = session.current_arg_text.strip()
    if session.is_first_run:
        if stripped_arg:
            session.state['wtlist'] = stripped_arg
        return

    if not stripped_arg:
        session.pause('要查询的城市不能为空,请重新输入')
    session.state[session.current_key] = stripped_arg

@weathercn.args_parser
async def _(session: CommandSession):
    stripped_arg = session.current_arg_text.strip()
    if session.is_first_run:
        if stripped_arg:
            session.state['weathercn'] = stripped_arg
        return

    if not stripped_arg:
        session.pause('要查询的城市不能为空,请重新输入')
    session.state[session.current_key] = stripped_arg