GeoIP now shows regions for all countire

This commit is contained in:
Luke Rogers 2012-09-05 20:56:50 +12:00
parent 366cb44f0a
commit dafa9d4d94

View file

@ -1,11 +1,23 @@
from util import hook from util import hook
import os.path import os.path
import csv
import pygeoip import pygeoip
# initalise geolocation database # initalise geolocation database
geo = pygeoip.GeoIP(os.path.abspath("./plugins/data/geoip.dat")) geo = pygeoip.GeoIP(os.path.abspath("./plugins/data/geoip.dat"))
regions = {}
# read region database
with open("./plugins/data/geoip_regions.csv", "rb") as f:
reader = csv.reader(f)
for row in reader:
country, region, region_name = row
if not regions.has_key(country):
regions[country] = {}
regions[country][region] = region_name
@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>"
@ -14,7 +26,20 @@ def geoip(inp):
except: except:
return "Sorry, I can't locate that in my database." return "Sorry, I can't locate that in my database."
record["_cc"] = record["country_code"] or "N/A" print record
record["_country"] = record["country_name"] or "Unknown" data = {}
record["_city"] = record["metro_code"] or record["city"] or "Unknown"
return "Country: %(_country)s (%(_cc)s), City: %(_city)s" % record if "region_name" in record:
# we try catching an exception here because the region DB is missing a few areas
# it's a lazy patch, but it should do the job
try:
data["region"] = ", " + regions[record["country_code"]][record["region_name"]]
except:
data["region"] = ""
else:
data["region"] = ""
data["cc"] = record["country_code"] or "N/A"
data["country"] = record["country_name"] or "Unknown"
data["city"] = record["city"] or "Unknown"
return "\x02Country:\x02 %(country)s (%(cc)s), \x02City:\x02 %(city)s%(region)s" % data