Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

using json

  • Loading branch information...
commit 53b2b0dc690a54696da40c59a2bb529c2da7690c 1 parent a4b6990
@fiorix authored
Showing with 11 additions and 10 deletions.
  1. +11 −10 extplugins/geoip.py
View
21 extplugins/geoip.py
@@ -2,12 +2,13 @@
# coding: utf-8
# (c) 2010 /dev/null
-__version__ = "0.3"
+__version__ = "0.4"
__author__ = "/dev/null"
-import urllib
import functools
+import json
import unicodedata
+import urllib
import b3.plugin
import b3.events
@@ -123,20 +124,20 @@ def reply(self, say, ip, nick):
"``result``: %s" % e)
def geoip_query(self, ip):
- fd = urllib.urlopen("http://freegeoip.net/csv/" + ip)
+ fd = urllib.urlopen("http://freegeoip.net/json/%s" % ip)
data = fd.read()
fd.close()
- data = normalize(data.decode("utf-8")).split(",")
+ data = json.loads(data)
self.debug("querying %s: %s" % (ip, data))
info = []
for fmt in self._format:
- if fmt == "country" and data[2]:
- info.append(data[2].strip())
- elif fmt == "region" and data[4]:
- info.append(data[4].strip())
- elif fmt == "city" and data[5]:
- info.append(data[5].strip())
+ if fmt == "country" and "country_name" in data:
+ info.append(normalize(data["country_name"]))
+ elif fmt == "region" and "region_name" in data:
+ info.append(normalize(data["region_name"]))
+ elif fmt == "city" and "city" in data:
+ info.append(normalize(data["city"]))
return ", ".join(info)
Please sign in to comment.
Something went wrong with that request. Please try again.