-
Notifications
You must be signed in to change notification settings - Fork 0
/
geo.py
51 lines (36 loc) · 1.11 KB
/
geo.py
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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# jsb/plugs/common/geo.py
#
#
""" This product includes GeoLite data created by MaxMind, available from http://maxmind.com/ """
## jsb imports
from jsb.lib.commands import cmnds
from jsb.utils.url import geturl2
from jsb.imports import getjson
## system imports
from socket import gethostbyname
import re
## defines
URL = "http://geoip.pidgets.com/?ip=%s&format=json"
def querygeoipserver(ip):
ipinfo = getjson().loads(geturl2(URL % ip))
return ipinfo
## geo command
def handle_geo(bot, event):
if not event.rest:
event.missing("<ip>")
return
query = event.rest.strip()
ippattern = re.match(r"^([0-9]{1,3}\.){3}[0-9]{1,3}$", query)
hostpattern = re.match(r"(\w+://)?(?P<hostname>\S+\.\w+)", query)
ip = ""
if ippattern:
ip = ippattern.group(0)
elif hostpattern:
try:
ip = gethostbyname(hostpattern.group('hostname'))
except:
event.reply("Couldn't look up the hostname")
return
else: return
event.reply("geo of %s is: " % ip, querygeoipserver(ip))
cmnds.add("geo", handle_geo, ["OPER", "GEO"])