-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathip.py
More file actions
36 lines (30 loc) · 1.39 KB
/
ip.py
File metadata and controls
36 lines (30 loc) · 1.39 KB
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
import hexchat
import requests
__module_name__ = "IP Lookup"
__module_author__ = "ComputerTech"
__module_version__ = "1.0"
def ip_lookup(ip, fields):
if fields == "all":
url = f"http://ip-api.com/json/{ip}"
else:
url = f"http://ip-api.com/json/{ip}?fields={fields}"
response = requests.get(url)
data = response.json()
if 'status' in data and data["status"] == "success":
output = ""
for key, value in data.items():
output += f"{key}: {value}\n"
return output
else:
return "Could not retrieve location information."
def ip_lookup_cb(word, word_eol, userdata):
if len(word) < 2:
hexchat.prnt("Please provide an IP address to look up.")
return hexchat.EAT_ALL
ip = word[1]
fields = ",".join(word[2:]) if len(word) > 2 else "country,city"
location = ip_lookup(ip, fields)
hexchat.prnt(f"Location for IP {ip}: {location}")
return hexchat.EAT_ALL
hexchat.hook_command("IPLOOKUP", ip_lookup_cb, help="/IPLOOKUP <ip> [fields] - Look up the location of an IP address using the ip-api service. Fields is a comma-separated list of fields to retrieve, e.g. 'country,regionName,city'. Use 'all' to retrieve all available fields. The default fields are 'country' and 'city'.")
hexchat.prnt(f"{__module_name__} version {__module_version__} by {__module_author__} loaded.")