python3 compatible. add hostname lookup
This commit is contained in:
parent
f7d0b4c8e0
commit
036bd2ac32
34
plugin.py
34
plugin.py
|
|
@ -15,6 +15,7 @@ import supybot.registry as registry
|
|||
import supybot.ircdb as ircdb
|
||||
import tarfile
|
||||
import gzip
|
||||
import socket
|
||||
|
||||
try:
|
||||
import geoip2.database
|
||||
|
|
@ -26,28 +27,24 @@ class Geo(callbacks.Plugin):
|
|||
"""
|
||||
Geolocation using GeoLiteCity
|
||||
"""
|
||||
|
||||
def make_sure_path_exists(self, path):
|
||||
try:
|
||||
os.makedirs(path)
|
||||
except OSError as exception:
|
||||
if exception.errno != errno.EEXIST:
|
||||
raise
|
||||
|
||||
|
||||
# **********************************************************************
|
||||
|
||||
def geo( self, irc, msgs, args, stuff):
|
||||
"""[<ip> | <hostname>]
|
||||
|
||||
Geolocation of an ip or host.
|
||||
"""
|
||||
if stuff.lower()=='update':
|
||||
irc.reply('Updating data file...')
|
||||
self.update()
|
||||
irc.reply('Update finished.')
|
||||
|
||||
return
|
||||
|
||||
|
||||
try:
|
||||
reader = geoip2.database.Reader('%s%sgeo%sGeoLite2-City.mmdb' % (conf.supybot.directories.data(), os.sep, os.sep))
|
||||
except:
|
||||
|
|
@ -58,15 +55,19 @@ class Geo(callbacks.Plugin):
|
|||
except:
|
||||
irc.reply("Update failed.")
|
||||
return
|
||||
|
||||
|
||||
if not utils.net.isIP(stuff):
|
||||
irc.reply('Error: Not a valid ip.')
|
||||
try:
|
||||
ip = socket.gethostbyname(stuff)
|
||||
except:
|
||||
irc.reply("Invalid Hostname.")
|
||||
return
|
||||
else:
|
||||
ip=stuff
|
||||
res = reader.city(stuff)
|
||||
|
||||
ip = stuff
|
||||
res = reader.city(ip)
|
||||
if res:
|
||||
irc.reply('%s, %s, %s' % (res.city.name, res.subdivisions.most_specific.name, res.country.name ))
|
||||
else:
|
||||
irc.reply("No results found")
|
||||
geo = wrap(geo, ['text'])
|
||||
|
||||
def update(self):
|
||||
|
|
@ -88,19 +89,15 @@ class Geo(callbacks.Plugin):
|
|||
self.log.info("Starting update of Geo data files...")
|
||||
self.getfile()
|
||||
return
|
||||
|
||||
|
||||
def getfile(self):
|
||||
"""grabs the data file"""
|
||||
u='https://geolite.maxmind.com/download/geoip/database/GeoLite2-City.tar.gz'
|
||||
f = '%s%sgeo%sGeoLite2-City.tar.gz' % (conf.supybot.directories.data(), os.sep, os.sep)
|
||||
f2 = '%s%sgeo%sGeoLite2-City.tar' % (conf.supybot.directories.data(), os.sep, os.sep)
|
||||
|
||||
self.make_sure_path_exists(r'%s%sgeo' % (conf.supybot.directories.data(),os.sep))
|
||||
|
||||
path = '%s%sgeo' % (conf.supybot.directories.data(),os.sep)
|
||||
|
||||
self.log.info('Starting download: %s' % f)
|
||||
|
||||
h = utils.web.getUrl(u)
|
||||
if h:
|
||||
tempfile=open(f, 'w+b')
|
||||
|
|
@ -118,16 +115,13 @@ class Geo(callbacks.Plugin):
|
|||
self.log.info('Untarring: %s' % f2)
|
||||
tar = tarfile.open(f2)
|
||||
tar.getmembers()
|
||||
|
||||
for member in tar.getmembers():
|
||||
if "GeoLite2-City.mmdb" in member.name:
|
||||
member.name = "GeoLite2-City.mmdb"
|
||||
self.log.info(member.name)
|
||||
tar.extract(member, path=path)
|
||||
self.log.info('Finished Untarring: %s' % f2)
|
||||
|
||||
tar.close()
|
||||
|
||||
os.remove(f)
|
||||
os.remove(f2)
|
||||
else:
|
||||
|
|
|
|||
Loading…
Reference in New Issue