Skip to content

Commit 73d68b1

Browse files
committed
Merge pull request #151 from epyatopal/master
http://ipinfo.io/ provider added
2 parents 96c5d8c + f24b4e4 commit 73d68b1

File tree

4 files changed

+151
-2
lines changed

4 files changed

+151
-2
lines changed

docs/providers/IpinfoIo.md

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
# IpinfoIo (IP Address)
2+
3+
## Python Example
4+
5+
```python
6+
>>> import geocoder
7+
>>> g = geocoder.ipinfoio('199.7.157.0')
8+
>>> g.latlng
9+
[45.413140, -75.656703]
10+
>>> g.city
11+
'Toronto'
12+
...
13+
```
14+
15+
To retrieve your own IP address, simply have `''` as the input.
16+
17+
```python
18+
>>> import geocoder
19+
>>> g = geocoder.ipinfoio('')
20+
>>> g.latlng
21+
[45.413140, -75.656703]
22+
>>> g.ip
23+
'199.7.157.0'
24+
...
25+
```
26+
27+
## Geocoder Attributes
28+
29+
```
30+
g.address g.ip g.postal g.y
31+
g.city g.json g.lat g.provider
32+
g.content g.latlng g.state
33+
g.country g.lng g.status
34+
g.debug g.location g.status_code
35+
g.error g.ok g.url
36+
g.geometry g.params g.wkt
37+
g.headers g.parse g.x
38+
```
39+
40+
## Parameters
41+
42+
* :param location: Your search IP Address you want geocoded.
43+
* :param location: (optional) `''` will return your current IP address's location.
44+
45+
## References
46+
47+
* [GitHub Repo](https://github.com/DenisCarriere/geocoder)
48+
* [GitHub Wiki](https://github.com/DenisCarriere/geocoder/wiki)
49+
* [IpinfoIo](https://ipinfo.io)

geocoder/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@
3535
# CORE
3636
from geocoder.api import get, yahoo, bing, geonames, mapquest, google, mapbox # noqa
3737
from geocoder.api import nokia, osm, tomtom, geolytica, arcgis, opencage # noqa
38-
from geocoder.api import maxmind, freegeoip, ottawa, here, baidu, w3w, yandex # noqa
38+
from geocoder.api import maxmind, ipinfoio, freegeoip, ottawa, here, baidu, w3w, yandex # noqa
3939

4040
# EXTRAS
4141
from geocoder.api import timezone, elevation, ip, canadapost, reverse, distance, location # noqa

geocoder/api.py

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
from geocoder.google import Google
1717
from geocoder.mapbox import Mapbox
1818
from geocoder.maxmind import Maxmind
19+
from geocoder.ipinfoio import IpinfoIo
1920
from geocoder.location import Location
2021
from geocoder.opencage import OpenCage
2122
from geocoder.geonames import Geonames
@@ -61,6 +62,7 @@ def get(location, **kwargs):
6162
'reverse': MapboxReverse,
6263
},
6364
'maxmind': {'geocode': Maxmind},
65+
'ipinfoio': {'geocode': IpinfoIo},
6466
'geonames': {'geocode': Geonames},
6567
'freegeoip': {'geocode': FreeGeoIP},
6668
'w3w': {
@@ -101,7 +103,6 @@ def get(location, **kwargs):
101103
else:
102104
if method not in options[provider]:
103105
raise ValueError("Invalid method")
104-
105106
return options[provider][method](location, **kwargs)
106107

107108

@@ -341,6 +342,16 @@ def maxmind(location='me', **kwargs):
341342
return get(location, provider='maxmind', **kwargs)
342343

343344

345+
def ipinfoio(location='', **kwargs):
346+
"""IpinfoIo Provider
347+
348+
:param location: Your search IP Address you want geocoded.
349+
:param location: (optional) if left blank will return your
350+
current IP address's location.
351+
"""
352+
return get(location, provider='ipinfoio', **kwargs)
353+
354+
344355
def freegeoip(location, **kwargs):
345356
"""FreeGeoIP Provider
346357

geocoder/ipinfoio.py

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
#!/usr/bin/python
2+
# coding: utf8
3+
4+
from __future__ import absolute_import
5+
from geocoder.base import Base
6+
7+
8+
class IpinfoIo(Base):
9+
"""
10+
API Reference
11+
-------------
12+
https://ipinfo.io
13+
"""
14+
provider = 'ipinfoio'
15+
method = 'geocode'
16+
def __init__(self, location='', **kwargs):
17+
self.location = location
18+
self.url = 'http://ipinfo.io/{0}/json'.format(self.location)
19+
self._initialize(**kwargs)
20+
self._ipinfoio_catch_errors()
21+
22+
def _ipinfoio_catch_errors(self):
23+
error = self.content.get('error')
24+
if error:
25+
code = self.content.get('code')
26+
self.error = code
27+
28+
def _exceptions(self):
29+
subdivisions = self.content.get('subdivisions')
30+
if subdivisions:
31+
self.content['subdivision'] = subdivisions[0]
32+
33+
# Grab all names in [en] and place them in self.parse
34+
for key, value in self.content.items():
35+
if isinstance(value, dict):
36+
for minor_key, minor_value in value.items():
37+
if minor_key == 'names':
38+
self.parse[key] = minor_value['en']
39+
40+
@property
41+
def lat(self):
42+
return self.parse.get('loc').split(',')[0]
43+
44+
@property
45+
def lng(self):
46+
return self.parse.get('loc').split(',')[1]
47+
48+
@property
49+
def address(self):
50+
if self.city:
51+
return '{0}, {1}, {2}'.format(self.city, self.state, self.country)
52+
elif self.state:
53+
return '{0}, {1}'.format(self.state, self.country)
54+
elif self.country:
55+
return '{0}'.format(self.country)
56+
else:
57+
return ''
58+
59+
@property
60+
def postal(self):
61+
return self.parse.get('postal')
62+
63+
@property
64+
def city(self):
65+
return self.parse.get('city')
66+
67+
@property
68+
def state(self):
69+
return self.parse.get('region')
70+
71+
@property
72+
def country(self):
73+
return self.parse.get('country')
74+
75+
@property
76+
def hostname(self):
77+
return self.parse.get('hostname')
78+
79+
@property
80+
def ip(self):
81+
return self.parse.get('ip')
82+
83+
@property
84+
def org(self):
85+
return self.parse.get('org')
86+
87+
if __name__ == '__main__':
88+
g = IpinfoIo('')
89+
g.debug()

0 commit comments

Comments
 (0)