]> jfr.im git - irc/rizon/acid.git/blob - pyva/src/main/python/internets/api/ipinfo.py
.gitignore: Ignore all pyva logs
[irc/rizon/acid.git] / pyva / src / main / python / internets / api / ipinfo.py
1 import urllib
2 from feed import XmlFeed
3
4 class IpInfo(object):
5 def __init__(self, api_key):
6 self.api_key = api_key
7 cache = {} # todo
8
9 def get_info(self, target):
10 url = 'http://api.ipinfodb.com/v3/ip-city/?key=%s&format=xml&' % self.api_key
11 url += urllib.urlencode({'ip': target})
12 reply = XmlFeed(url)
13 r = reply.elements('/Response')[0]
14 return {
15 'ip_addr': r.text('ipAddress'),
16 'status': r.text('statusMessage'),
17 'country_code': r.text('countryCode', 'N/A'),
18 'country_name': r.text('countryName', 'N/A').capitalize(),
19 'region': r.text('regionName', 'N/A').capitalize(),
20 'city': r.text('cityName', 'N/A').capitalize(),
21 'zip': r.text('zipCode', 'N/A'),
22 'latitude': r.text('latitude'),
23 'longitude': r.text('longitude'),
24 'timezone': r.text('timezone', 'N/A')
25 }