增加天气组件部分功能,更换ipping API (#108)

* Add: Weather, more data to parse.

* Add: Weather, more data to parse.

* Fix: ipping API

* Fix: ipping API

* Fix: localtime 导致的计算问题

* Fix: timezone for sunrise and sunset.

* Fix: pytube lib known issues.
This commit is contained in:
onoc1yn 2021-02-09 11:00:30 +08:00 committed by GitHub
parent c00bfdca1f
commit 4e86816d37
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 45 additions and 22 deletions

View File

@ -52,17 +52,17 @@
}, },
{ {
"name": "weather", "name": "weather",
"version": "1.0", "version": "1.1",
"section": "daily", "section": "daily",
"maintainer": "xtaodada", "maintainer": "xtaodada",
"size": "1.5 kb", "size": "2.9 kb",
"supported": true, "supported": true,
"des-short": "查询天气。", "des-short": "查询天气。",
"des": "这个人很懒,什么都没有留下。" "des": "这个人很懒,什么都没有留下。"
}, },
{ {
"name": "xtao-some", "name": "xtao-some",
"version": "1.12", "version": "1.13",
"section": "daily", "section": "daily",
"maintainer": "xtaodada", "maintainer": "xtaodada",
"size": "18.8 kb", "size": "18.8 kb",

View File

@ -33,7 +33,7 @@ async def vdl(context):
try: try:
from pytube import YouTube from pytube import YouTube
except ImportError: except ImportError:
await context.edit('(`pytube3`支持库未安装YouTube视频无法下载\n请使用 `-sh` `pip3` `install` `pytube3` 安装或自行ssh安装)') await context.edit('`pytube`支持库未安装YouTube视频无法下载\n请使用 `-sh pip3 install --user git+https://github.com/nficano/pytube` 安装或自行ssh安装\n\n已安装过 `pytube3` 的用户请使用 `-sh pip3 uninstall pytube3 -y` 进行卸载')
return return
url = url.replace('www.youtube.com/watch?v=', 'youtu.be/') url = url.replace('www.youtube.com/watch?v=', 'youtu.be/')
if not await youtube_dl(url, context, reply_id): if not await youtube_dl(url, context, reply_id):

View File

@ -1,4 +1,5 @@
import json import json
import datetime
from requests import get from requests import get
from pagermaid.listener import listener from pagermaid.listener import listener
from pagermaid.utils import obtain_message from pagermaid.utils import obtain_message
@ -24,6 +25,14 @@ icons = {
"50n": "🌫", "50n": "🌫",
} }
def timestamp_to_time(timestamp, timeZoneShift):
timeArray = datetime.datetime.utcfromtimestamp(timestamp) + datetime.timedelta(seconds=timeZoneShift)
return timeArray.strftime("%H:%M")
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)]
@listener(is_plugin=True, outgoing=True, command="weather", @listener(is_plugin=True, outgoing=True, command="weather",
description="查询天气", description="查询天气",
parameters="<城市>") parameters="<城市>")
@ -34,17 +43,33 @@ async def weather(context):
except ValueError: except ValueError:
await context.edit("出错了呜呜呜 ~ 无效的参数。") await context.edit("出错了呜呜呜 ~ 无效的参数。")
return return
try:
req = get("http://api.openweathermap.org/data/2.5/weather?appid=973e8a21e358ee9d30b47528b43a8746&units=metric&lang=zh_cn&q=" + message) req = get("http://api.openweathermap.org/data/2.5/weather?appid=973e8a21e358ee9d30b47528b43a8746&units=metric&lang=zh_cn&q=" + message)
if req.status_code == 200: if req.status_code == 200:
data = json.loads(req.text) data = json.loads(req.text)
cityName = "{}, {}".format(data["name"], data["sys"]["country"]) 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"])
sunriseTimeunix = data["sys"]["sunrise"]
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) tempInC = round(data["main"]["temp"], 2)
tempInF = round((1.8 * tempInC) + 32, 2) tempInF = round((1.8 * tempInC) + 32, 2)
icon = data["weather"][0]["icon"] icon = data["weather"][0]["icon"]
desc = data["weather"][0]["description"] desc = data["weather"][0]["description"]
res = "{}\n🌡{}℃ ({}F)\n{} {}".format( res = "{} {}{} 💨{} {}m/s\n大气🌡 {}℃ ({}℉) 💦 {}% \n体感🌡 {}\n气压 {}hpa\n🌅{} 🌇{} ".format(
cityName, tempInC, tempInF, icons[icon], desc cityName, icons[icon], desc, windDirection, windSpeed, tempInC, tempInF, humidity, fellsTemp, pressure, sunriseTime, sunsetTime
) )
await context.edit(res) await context.edit(res)
else: if req.status_code == 404:
await context.edit("出错了呜呜呜 ~ 无效的城市名,请使用拼音输入 ~ ")
return
except Exception:
await context.edit("出错了呜呜呜 ~ 无法访问到 openweathermap.org 。") await context.edit("出错了呜呜呜 ~ 无法访问到 openweathermap.org 。")

View File

@ -197,8 +197,7 @@ async def ipping(context):
else: else:
url = url.path url = url.path
pinginfo = requests.get( pinginfo = requests.get(
"https://helloacm.com/api/ping/?cached&host=" + url).content.decode( "https://steakovercooked.com/api/ping/?host=" + url).content.decode("utf-8")
"utf-8")
if pinginfo == 'null': if pinginfo == 'null':
pass pass
elif not pinginfo == 'null': elif not pinginfo == 'null':
@ -215,8 +214,7 @@ async def ipping(context):
await context.edit('没有找到要查询的 ip/域名 ...') await context.edit('没有找到要查询的 ip/域名 ...')
return True return True
pinginfo = requests.get( pinginfo = requests.get(
"https://helloacm.com/api/ping/?cached&host=" + url).content.decode( "https://steakovercooked.com/api/ping/?host=" + url).content.decode("utf-8")
"utf-8")
if pinginfo == 'null': if pinginfo == 'null':
pass pass
elif not pinginfo == 'null': elif not pinginfo == 'null':