CloudBot will now automatically download the geolocation database when you run the bot;

This commit is contained in:
Luke Rogers 2013-07-02 05:56:01 +12:00
parent 9779943b91
commit 75b5e2bc6d
2 changed files with 17 additions and 4 deletions

Binary file not shown.

View file

@ -1,20 +1,32 @@
from util import hook from util import hook, http
import os.path import os.path
import pygeoip import pygeoip
import json import json
import gzip
# initalise geolocation database from StringIO import StringIO
geo = pygeoip.GeoIP(os.path.abspath("./plugins/data/geoip.dat"))
# load region database # load region database
with open("./plugins/data/geoip_regions.json", "rb") as f: with open("./plugins/data/geoip_regions.json", "rb") as f:
regions = json.loads(f.read()) regions = json.loads(f.read())
if os.path.isfile(os.path.abspath("./plugins/data/GeoLiteCity.dat")):
# initalise geolocation database
geo = pygeoip.GeoIP(os.path.abspath("./plugins/data/GeoLiteCity.dat"))
else:
download = http.get("http://geolite.maxmind.com/download/geoip/database/GeoLiteCity.dat.gz")
string_io = StringIO(download)
geoip_file = gzip.GzipFile(fileobj=string_io, mode='rb')
output = open(os.path.abspath("./plugins/data/GeoLiteCity.dat"),'wb')
output.write(geoip_file.read())
output.close()
@hook.command @hook.command
def geoip(inp): def geoip(inp):
"geoip <host/ip> -- Gets the location of <host/ip>" "geoip <host/ip> -- Gets the location of <host/ip>"
try: try:
record = geo.record_by_name(inp) record = geo.record_by_name(inp)
except: except:
@ -36,3 +48,4 @@ def geoip(inp):
data["country"] = record["country_name"] or "Unknown" data["country"] = record["country_name"] or "Unknown"
data["city"] = record["city"] or "Unknown" data["city"] = record["city"] or "Unknown"
return "\x02Country:\x02 {country} ({cc}), \x02City:\x02 {city}{region}".format(**data) return "\x02Country:\x02 {country} ({cc}), \x02City:\x02 {city}{region}".format(**data)