Rewrote geolocation code (more features and size optimisations coming soon

This commit is contained in:
Luke Rogers 2012-09-05 16:25:51 +12:00
parent 4562f59cab
commit 21153b8246
7 changed files with 2163 additions and 38 deletions

View file

@ -1,43 +1,17 @@
from util import hook, http
from util import hook
import os.path
import pygeoip
def find_location(ip, api):
response = http.get("http://api.ipinfodb.com/v3/ip-city/", key=api, ip=ip)
response = response.split(";")
results = {}
results["country"] = response[4].title()
results["country_short"] = response[3].upper()
results["state"] = response[5].title()
results["city"] = response[6].title()
results["timezone"] = response[10].title()
return results
def timezone(ip):
time = find_location(ip)["timezone"]
time = time.replace(":", ".")
time = time.replace(".00", "")
return int(time)
# initalise geolocation database
geo = pygeoip.GeoIP(os.path.abspath("./plugins/data/geoip.dat"))
@hook.command
@hook.command("location")
def geoip(inp, say=None, bot=None):
"geoip <ip> - Performs a location check on <ip>."
api_key = bot.config.get("api_keys", {}).get("geoip", None)
if api_key is None:
return "error: no api key set"
def geoip(inp):
"geoip <host/ip> -- Gets the location of <host/ip>"
try:
record = geo.record_by_name(inp)
except:
return "Sorry, I can't locate that in my database."
give = find_location(inp, api_key)
if give["country"] not in ["", " ", "-", " - ", None]:
if give["state"] == give["city"]:
localstring = give["city"]
else:
localstring = give["city"] + ", " + give["state"]
say("That IP comes from " + give["country"] +
" (" + give["country_short"] + ")")
say("I think it's in " + localstring +
" with a timezone of " + give["timezone"] + "GMT")
else:
say("Either that wasn't an IP or I cannot locate it in my database.")
return
return "Country: %(country_name)s (%(country_code)s), City: %(city)s" % record