2020-08-11 15:47:41 +00:00
|
|
|
import json
|
2021-02-09 03:00:30 +00:00
|
|
|
import datetime
|
2020-08-11 15:47:41 +00:00
|
|
|
from requests import get
|
2022-01-18 08:47:20 +00:00
|
|
|
from pagermaid import version
|
2020-08-11 15:47:41 +00:00
|
|
|
from pagermaid.listener import listener
|
2021-06-16 07:09:40 +00:00
|
|
|
from pagermaid.utils import obtain_message, alias_command
|
2020-08-11 15:47:41 +00:00
|
|
|
|
|
|
|
icons = {
|
|
|
|
"01d": "🌞",
|
|
|
|
"01n": "🌚",
|
|
|
|
"02d": "⛅️",
|
|
|
|
"02n": "⛅️",
|
|
|
|
"03d": "☁️",
|
|
|
|
"03n": "☁️",
|
|
|
|
"04d": "☁️",
|
|
|
|
"04n": "☁️",
|
|
|
|
"09d": "🌧",
|
|
|
|
"09n": "🌧",
|
|
|
|
"10d": "🌦",
|
|
|
|
"10n": "🌦",
|
|
|
|
"11d": "🌩",
|
|
|
|
"11n": "🌩",
|
|
|
|
"13d": "🌨",
|
|
|
|
"13n": "🌨",
|
|
|
|
"50d": "🌫",
|
|
|
|
"50n": "🌫",
|
|
|
|
}
|
|
|
|
|
2021-06-16 07:09:40 +00:00
|
|
|
|
2021-02-09 03:00:30 +00:00
|
|
|
def timestamp_to_time(timestamp, timeZoneShift):
|
|
|
|
timeArray = datetime.datetime.utcfromtimestamp(timestamp) + datetime.timedelta(seconds=timeZoneShift)
|
|
|
|
return timeArray.strftime("%H:%M")
|
2021-06-16 07:09:40 +00:00
|
|
|
|
|
|
|
|
2021-02-09 03:00:30 +00:00
|
|
|
def calcWindDirection(windDirection):
|
|
|
|
dirs = ['N', 'NNE', 'NE', 'ENE', 'E', 'ESE', 'SE', 'SSE', 'S', 'SSW', 'SW', 'WSW', 'W', 'WNW', 'NW', 'NNW']
|
|
|
|
ix = round(windDirection / (360. / len(dirs)))
|
|
|
|
return dirs[ix % len(dirs)]
|
|
|
|
|
2021-06-16 07:09:40 +00:00
|
|
|
|
|
|
|
@listener(is_plugin=True, outgoing=True, command=alias_command("weather"),
|
2020-08-11 15:47:41 +00:00
|
|
|
description="查询天气",
|
|
|
|
parameters="<城市>")
|
|
|
|
async def weather(context):
|
|
|
|
await context.edit("获取中 . . .")
|
|
|
|
try:
|
|
|
|
message = await obtain_message(context)
|
|
|
|
except ValueError:
|
|
|
|
await context.edit("出错了呜呜呜 ~ 无效的参数。")
|
|
|
|
return
|
2021-02-09 03:00:30 +00:00
|
|
|
try:
|
2021-06-16 07:09:40 +00:00
|
|
|
req = get(
|
|
|
|
"http://api.openweathermap.org/data/2.5/weather?appid=973e8a21e358ee9d30b47528b43a8746&units=metric&lang"
|
|
|
|
"=zh_cn&q=" + message)
|
2021-02-09 03:00:30 +00:00
|
|
|
if req.status_code == 200:
|
|
|
|
data = json.loads(req.text)
|
|
|
|
cityName = "{}, {}".format(data["name"], data["sys"]["country"])
|
|
|
|
timeZoneShift = data["timezone"]
|
|
|
|
temp_Max = round(data["main"]["temp_max"], 2)
|
|
|
|
temp_Min = round(data["main"]["temp_min"], 2)
|
|
|
|
pressure = data["main"]["pressure"]
|
|
|
|
humidity = data["main"]["humidity"]
|
|
|
|
windSpeed = data["wind"]["speed"]
|
|
|
|
windDirection = calcWindDirection(data["wind"]["deg"])
|
2021-06-16 07:09:40 +00:00
|
|
|
sunriseTimeunix = data["sys"]["sunrise"]
|
2021-02-09 03:00:30 +00:00
|
|
|
sunriseTime = timestamp_to_time(sunriseTimeunix, timeZoneShift)
|
|
|
|
sunsetTimeunix = data["sys"]["sunset"]
|
|
|
|
sunsetTime = timestamp_to_time(sunsetTimeunix, timeZoneShift)
|
|
|
|
fellsTemp = data["main"]["feels_like"]
|
|
|
|
tempInC = round(data["main"]["temp"], 2)
|
|
|
|
tempInF = round((1.8 * tempInC) + 32, 2)
|
|
|
|
icon = data["weather"][0]["icon"]
|
|
|
|
desc = data["weather"][0]["description"]
|
|
|
|
res = "{} {}{} 💨{} {}m/s\n大气🌡 {}℃ ({}℉) 💦 {}% \n体感🌡 {}℃\n气压 {}hpa\n🌅{} 🌇{} ".format(
|
2021-06-16 07:09:40 +00:00
|
|
|
cityName, icons[icon], desc, windDirection, windSpeed, tempInC, tempInF, humidity, fellsTemp, pressure,
|
|
|
|
sunriseTime, sunsetTime
|
2021-02-09 03:00:30 +00:00
|
|
|
)
|
|
|
|
await context.edit(res)
|
|
|
|
if req.status_code == 404:
|
|
|
|
await context.edit("出错了呜呜呜 ~ 无效的城市名,请使用拼音输入 ~ ")
|
|
|
|
return
|
|
|
|
except Exception:
|
|
|
|
await context.edit("出错了呜呜呜 ~ 无法访问到 openweathermap.org 。")
|