iShotaBot/modules/ip.py

85 lines
3.2 KiB
Python
Raw Normal View History

2022-07-25 10:04:36 +00:00
from urllib.parse import urlparse
from pyrogram import Client, filters
from pyrogram.types import Message
from defs.ip import ip_info
from init import request, bot
2022-07-25 10:04:36 +00:00
@bot.on_message(filters.incoming & filters.command(["ip", f"ip@{bot.me.username}"]))
2022-07-25 10:04:36 +00:00
async def ip_command(_: Client, message: Message):
2023-01-12 13:19:54 +00:00
msg = await message.reply("正在查询中...")
rep_text = ""
2022-07-25 10:04:36 +00:00
reply = message.reply_to_message
if reply:
if reply.entities:
for num in range(0, len(reply.entities)):
url = reply.text[
2023-01-12 13:19:54 +00:00
reply.entities[num].offset : reply.entities[num].offset
+ reply.entities[num].length
]
2022-07-25 10:04:36 +00:00
url = urlparse(url)
if url.hostname or url.path:
if url.hostname:
url = url.hostname
else:
url = url.path
2023-01-12 13:19:54 +00:00
ipinfo_json = (
await request.get(
"http://ip-api.com/json/"
+ url
+ "?fields=status,message,country,regionName,city,"
"lat,lon,isp,"
"org,as,mobile,proxy,hosting,query"
)
).json()
2023-05-27 13:45:56 +00:00
if ipinfo_json["status"] == "success":
2022-07-25 10:04:36 +00:00
rep_text = ip_info(url, ipinfo_json)
2023-01-12 13:19:54 +00:00
text = ""
2022-07-25 10:04:36 +00:00
if message.entities:
for num in range(0, len(message.entities)):
url = message.text[
2023-01-12 13:19:54 +00:00
message.entities[num].offset : message.entities[num].offset
+ message.entities[num].length
]
2022-07-25 10:04:36 +00:00
url = urlparse(url)
if url.hostname or url.path:
if url.hostname:
url = url.hostname
else:
url = url.path
2023-01-12 13:19:54 +00:00
ipinfo_json = (
await request.get(
"http://ip-api.com/json/"
+ url
+ "?fields=status,message,country,regionName,city,lat,"
"lon,isp,"
"org,as,mobile,proxy,hosting,query"
)
).json()
2023-05-27 13:45:56 +00:00
if ipinfo_json["status"] == "success":
2022-07-25 10:04:36 +00:00
text = ip_info(url, ipinfo_json)
2023-01-12 13:19:54 +00:00
if text == "":
2022-07-25 10:04:36 +00:00
url = message.text[4:]
2023-05-27 13:45:56 +00:00
if url != "":
2023-01-12 13:19:54 +00:00
ipinfo_json = (
await request.get(
"http://ip-api.com/json/"
+ url
+ "?fields=status,message,country,regionName,city,lat,"
"lon,isp,"
"org,as,mobile,proxy,hosting,query"
)
).json()
2023-05-27 13:45:56 +00:00
if ipinfo_json["status"] == "success":
2022-07-25 10:04:36 +00:00
text = ip_info(url, ipinfo_json)
2023-01-12 13:19:54 +00:00
if rep_text == "" and text == "":
await msg.edit("没有找到要查询的 ip/域名 ...")
2023-05-27 13:45:56 +00:00
elif rep_text != "" and text != "":
2023-08-25 14:10:13 +00:00
await msg.edit(
f"{rep_text}\n================\n{text}", disable_web_page_preview=True
)
2022-07-25 10:04:36 +00:00
else:
2023-08-25 14:10:13 +00:00
await msg.edit(f"{rep_text}{text}", disable_web_page_preview=True)