iShotaBot/modules/geo.py

56 lines
1.9 KiB
Python
Raw Normal View History

2022-10-13 14:36:43 +00:00
import contextlib
from urllib.parse import quote
from pyrogram import Client, filters
from pyrogram.types import Message
from defs.glover import amap_key
from init import user_me, request
REQUEST_URL = f"https://restapi.amap.com/v3/geocode/geo?key={amap_key}&"
2023-01-12 13:19:54 +00:00
@Client.on_message(
filters.incoming & filters.command(["geo", f"geo@{user_me.username}"])
)
2022-10-13 14:36:43 +00:00
async def geo_command(_: Client, message: Message):
if len(message.command) <= 1:
2023-01-12 13:19:54 +00:00
await message.reply("没有找到要查询的中国 经纬度/地址 ...")
2022-10-13 14:36:43 +00:00
return
mode, lat, lon = "address", 0, 0 # noqa
with contextlib.suppress(ValueError, IndexError):
lat, lon = float(message.command[1]), float(message.command[2])
mode = "location"
if mode == "location":
try:
2023-01-12 13:19:54 +00:00
geo = (
await request.get(
f"{REQUEST_URL.replace('/geo?', '/regeo?')}location={lat},{lon}"
)
).json()
2022-10-13 14:36:43 +00:00
formatted_address = geo["regeocode"]["formatted_address"]
assert isinstance(formatted_address, str)
except (KeyError, AssertionError):
await message.reply(f"无法解析经纬度 {lat}, {lon}", quote=True)
return
else:
try:
2023-01-12 13:19:54 +00:00
geo = (
await request.get(
f"{REQUEST_URL}address={quote(' '.join(message.command[1:]).strip())}"
)
).json()
2022-10-13 14:36:43 +00:00
formatted_address = geo["geocodes"][0]["formatted_address"]
lat, lon = geo["geocodes"][0]["location"].split(",")
except (KeyError, IndexError, ValueError):
2023-01-12 13:19:54 +00:00
await message.reply(
f"无法解析地址 {' '.join(message.command[1:]).strip()}", quote=True
)
2022-10-13 14:36:43 +00:00
return
2023-01-12 13:19:54 +00:00
msg = await message.reply_location(
longitude=float(lat), latitude=float(lon), quote=True
)
await msg.reply(f"坐标:`{lat},{lon}`\n" f"地址:<b>{formatted_address}</b>", quote=True)