mirror of
https://github.com/oddluck/limnoria-plugins.git
synced 2025-04-25 20:41:21 -05:00
Add 'Geo/' from commit '660d96f113144ef153f7fad22e52aa655bb51d93'
git-subtree-dir: Geo git-subtree-mainline: 63eaa55e2c07bac5a0c5ece2cd821ca3acb8fc22 git-subtree-split: 660d96f113144ef153f7fad22e52aa655bb51d93
This commit is contained in:
commit
7eaa72197b
4
Geo/README.txt
Normal file
4
Geo/README.txt
Normal file
@ -0,0 +1,4 @@
|
||||
Provides geographical information from an ip address using GeoLite2.
|
||||
|
||||
requires GeoIP2-python:
|
||||
pip install geoip2
|
38
Geo/__init__.py
Normal file
38
Geo/__init__.py
Normal file
@ -0,0 +1,38 @@
|
||||
###
|
||||
# by SpiderDave
|
||||
###
|
||||
|
||||
"""
|
||||
Plugin to Geoloacate an IP
|
||||
"""
|
||||
|
||||
import supybot
|
||||
import supybot.world as world
|
||||
import importlib
|
||||
|
||||
# Use this for the version of this plugin. You may wish to put a CVS keyword
|
||||
# in here if you're keeping the plugin in CVS or some similar system.
|
||||
__version__ = "0"
|
||||
|
||||
__author__ = "SpiderDave"
|
||||
|
||||
# This is a dictionary mapping supybot.Author instances to lists of
|
||||
# contributions.
|
||||
__contributors__ = {}
|
||||
|
||||
from . import config
|
||||
from . import plugin
|
||||
from imp import reload
|
||||
importlib.reload(config)
|
||||
importlib.reload(plugin) # In case we're being reloaded.
|
||||
# Add more reloads here if you add third-party modules and want them to be
|
||||
# reloaded when this plugin is reloaded. Don't forget to import them as well!
|
||||
|
||||
if world.testing:
|
||||
from . import test
|
||||
|
||||
Class = plugin.Class
|
||||
configure = config.configure
|
||||
|
||||
|
||||
# vim:set shiftwidth=4 softtabstop=4 expandtab textwidth=79:
|
25
Geo/config.py
Normal file
25
Geo/config.py
Normal file
@ -0,0 +1,25 @@
|
||||
import supybot.conf as conf
|
||||
import supybot.registry as registry
|
||||
|
||||
try:
|
||||
from supybot.i18n import PluginInternationalization
|
||||
_ = PluginInternationalization('Geo')
|
||||
except:
|
||||
# Placeholder that allows to run the plugin on a bot
|
||||
# without the i18n module
|
||||
_ = lambda x:x
|
||||
|
||||
def configure(advanced):
|
||||
# This will be called by supybot to configure this module. advanced is
|
||||
# a bool that specifies whether the user identified himself as an advanced
|
||||
# user or not. You should effect your configuration by manipulating the
|
||||
# registry as appropriate.
|
||||
from supybot.questions import expect, anything, something, yn
|
||||
conf.registerPlugin('Geo', True)
|
||||
|
||||
|
||||
Geo = conf.registerPlugin('Geo')
|
||||
conf.registerGlobalValue(Geo, 'datalastupdated',
|
||||
registry.PositiveInteger(1, """An integer representing the time since epoch the .dat file was last updated."""))
|
||||
|
||||
# vim:set shiftwidth=4 softtabstop=4 expandtab textwidth=79:
|
131
Geo/plugin.py
Normal file
131
Geo/plugin.py
Normal file
@ -0,0 +1,131 @@
|
||||
###
|
||||
# Geo/plugin.py
|
||||
# by SpiderDave
|
||||
###
|
||||
|
||||
import time
|
||||
import supybot.conf as conf
|
||||
import supybot.utils as utils
|
||||
from supybot.commands import *
|
||||
import supybot.ircmsgs as ircmsgs
|
||||
import supybot.ircutils as ircutils
|
||||
import supybot.callbacks as callbacks
|
||||
import os, errno
|
||||
import supybot.registry as registry
|
||||
import supybot.ircdb as ircdb
|
||||
import tarfile
|
||||
import gzip
|
||||
import socket
|
||||
|
||||
try:
|
||||
import geoip2.database
|
||||
except:
|
||||
raise ImportError("The Geo plugin requires geoip2 be installed. Load aborted.")
|
||||
|
||||
class Geo(callbacks.Plugin):
|
||||
threaded=True
|
||||
"""
|
||||
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:
|
||||
irc.reply("Error: GeoLite2-City database not found, attempting to update...")
|
||||
try:
|
||||
self.update()
|
||||
irc.reply('Update finished.')
|
||||
except:
|
||||
irc.reply("Update failed.")
|
||||
return
|
||||
if not utils.net.isIP(stuff):
|
||||
try:
|
||||
ip = socket.gethostbyname(stuff)
|
||||
except:
|
||||
irc.reply("Invalid Hostname.")
|
||||
return
|
||||
else:
|
||||
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):
|
||||
"""update the geo files"""
|
||||
now=int(time.time())
|
||||
try:
|
||||
lastupdate=self.registryValue('datalastupdated')
|
||||
self.log.info('Last update: %s seconds ago.' % lastupdate)
|
||||
except registry.NonExistentRegistryEntry:
|
||||
self.log.info('supybot.plugins.%s.datalastupdate not set. Creating...' % self.name)
|
||||
conf.registerGlobalValue(conf.supybot.plugins.get(self.name), 'datalastupdated', registry.PositiveInteger(1, """An integer representing the time since epoch the .dat file was last updated."""))
|
||||
self.log.info('...success!')
|
||||
lastupdate=1
|
||||
self.log.info('Last update: Unknown/Never')
|
||||
|
||||
#if (now-lastupdate)>604800: # updated weekly
|
||||
if 1==1:
|
||||
self.setRegistryValue('datalastupdated', now)
|
||||
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')
|
||||
if tempfile:
|
||||
tempfile.write(h)
|
||||
tempfile.close()
|
||||
self.log.info('Completed: %s' % f)
|
||||
self.log.info('Unzipping: %s' % f)
|
||||
f_in = gzip.open(f, 'rb')
|
||||
f_out=open(f2, 'wb')
|
||||
f_out.writelines(f_in)
|
||||
f_out.close()
|
||||
f_in.close()
|
||||
self.log.info('Finished Unzipping: %s' % f)
|
||||
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:
|
||||
self.log.info('Could not download: %s' % f)
|
||||
return
|
||||
|
||||
Class = Geo
|
Loading…
x
Reference in New Issue
Block a user