From dafa9d4d94155529f7d4f8bdd13aa943b3a0bcbc Mon Sep 17 00:00:00 2001 From: Luke Rogers Date: Wed, 5 Sep 2012 20:56:50 +1200 Subject: [PATCH] GeoIP now shows regions for all countire --- plugins/geoip.py | 33 +++++++++++++++++++++++++++++---- 1 file changed, 29 insertions(+), 4 deletions(-) diff --git a/plugins/geoip.py b/plugins/geoip.py index e30fa48..858f475 100755 --- a/plugins/geoip.py +++ b/plugins/geoip.py @@ -1,11 +1,23 @@ from util import hook import os.path +import csv import pygeoip # initalise geolocation database 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 def geoip(inp): "geoip -- Gets the location of " @@ -14,7 +26,20 @@ def geoip(inp): except: return "Sorry, I can't locate that in my database." - record["_cc"] = record["country_code"] or "N/A" - record["_country"] = record["country_name"] or "Unknown" - record["_city"] = record["metro_code"] or record["city"] or "Unknown" - return "Country: %(_country)s (%(_cc)s), City: %(_city)s" % record + print record + data = {} + + 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