Skip to content

Commit 82a11a5

Browse files
committed
Update IPInfo.io Provider
- Changed the provider name shorter from ipinfoio > ipinfo - Added Command line support for ipinfo. - Parsing lat & lng into floats instead of strings. - Converted documentation to RST instead of Markdown. - Catching errors if you enter incorrect IP address. - Remove ipinfo exceptions, there isn't any. - Added IPInfo in Index.rst for docs.
1 parent 73d68b1 commit 82a11a5

File tree

9 files changed

+141
-132
lines changed

9 files changed

+141
-132
lines changed

README.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,7 @@ Providers
121121
GeoOttawa_, Ottawa
122122
Google_, World, Rate Limit
123123
HERE_, World, API key
124+
IPInfo_, World
124125
Mapbox_, World, API key
125126
MapQuest_, World, API key
126127
MaxMind_, World
@@ -200,3 +201,4 @@ Please feel free to give any feedback on this module. If you find any bugs or an
200201
.. _What3Words: http://geocoder.readthedocs.org/providers/What3Words.html
201202
.. _CanadaPost: http://geocoder.readthedocs.org/providers/CanadaPost.html
202203
.. _GeoNames: http://geocoder.readthedocs.org/providers/GeoNames.html
204+
.. _IPInfo: http://geocoder.readthedocs.org/providers/IPInfo.html

docs/index.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ Detailed information about each individual provider that are within Geocoder.
7575
providers/Geocoder-ca.rst
7676
providers/GeoOttawa.rst
7777
providers/HERE.rst
78+
providers/IPInfo.rst
7879

7980

8081
Contributor Guide

docs/providers/IPInfo.rst

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
IP Info.io (IP Address)
2+
=======================
3+
4+
Use the ipinfo.io IP lookup API to quickly and simply integrate IP geolocation
5+
into your script or website. Save yourself the hassle of setting up local GeoIP
6+
libraries and having to remember to regularly update the data.
7+
8+
Examples
9+
~~~~~~~~
10+
11+
Basic Geocoding
12+
---------------
13+
14+
.. code-block:: python
15+
16+
>>> import geocoder
17+
>>> g = geocoder.ipinfo('199.7.157.0')
18+
>>> g.latlng
19+
[45.413140, -75.656703]
20+
>>> g.city
21+
'Toronto'
22+
>>> g.json
23+
...
24+
25+
Lookup your own IP
26+
------------------
27+
28+
To retrieve your own IP address, simply have `''` as the input.
29+
30+
.. code-block:: python
31+
32+
>>> import geocoder
33+
>>> g = geocoder.ipinfo('')
34+
>>> g.latlng
35+
[45.413140, -75.656703]
36+
>>> g.ip
37+
'199.7.157.0'
38+
>>> g.json
39+
...
40+
41+
Command Line Interface
42+
----------------------
43+
44+
.. code-block:: bash
45+
46+
$ geocode '199.7.157.0' --provider ipinfo | jq .
47+
48+
Parameters
49+
~~~~~~~~~~
50+
51+
- `location`: Your search location you want geocoded.
52+
- `location`: (optional) `''` will return your current IP address's location.
53+
- `method`: (default=geocode) Use the following:
54+
- geocode
55+
56+
References
57+
~~~~~~~~~~
58+
59+
- `IpinfoIo <https://ipinfo.io>`_

docs/providers/IpinfoIo.md

Lines changed: 0 additions & 49 deletions
This file was deleted.

geocoder/__init__.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,14 +28,14 @@
2828
__title__ = 'geocoder'
2929
__author__ = 'Denis Carriere'
3030
__author_email__ = 'carriere.denis@gmail.com'
31-
__version__ = '1.5.2'
31+
__version__ = '1.5.3'
3232
__license__ = 'MIT'
3333
__copyright__ = 'Copyright (c) 2013-2015 Denis Carriere'
3434

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, ipinfoio, freegeoip, ottawa, here, baidu, w3w, yandex # noqa
38+
from geocoder.api import maxmind, ipinfo, 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: 55 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@
1515
from geocoder.yandex import Yandex
1616
from geocoder.google import Google
1717
from geocoder.mapbox import Mapbox
18+
from geocoder.ipinfo import Ipinfo
1819
from geocoder.maxmind import Maxmind
19-
from geocoder.ipinfoio import IpinfoIo
2020
from geocoder.location import Location
2121
from geocoder.opencage import OpenCage
2222
from geocoder.geonames import Geonames
@@ -37,63 +37,66 @@
3737
from geocoder.opencage_reverse import OpenCageReverse
3838

3939

40+
options = {
41+
'osm': {'geocode': Osm},
42+
'here': {
43+
'geocode': Here,
44+
'reverse': HereReverse,
45+
},
46+
'baidu': {'geocode': Baidu},
47+
'yahoo': {'geocode': Yahoo},
48+
'tomtom': {'geocode': Tomtom},
49+
'arcgis': {'geocode': Arcgis},
50+
'ottawa': {'geocode': Ottawa},
51+
'mapbox': {
52+
'geocode': Mapbox,
53+
'reverse': MapboxReverse,
54+
},
55+
'maxmind': {'geocode': Maxmind},
56+
'ipinfo': {'geocode': Ipinfo},
57+
'geonames': {'geocode': Geonames},
58+
'freegeoip': {'geocode': FreeGeoIP},
59+
'w3w': {
60+
'geocode': W3W,
61+
'reverse': W3WReverse,
62+
},
63+
'yandex': {
64+
'geocode': Yandex,
65+
'reverse': YandexReverse,
66+
},
67+
'mapquest': {
68+
'geocode': Mapquest,
69+
'reverse': MapquestReverse,
70+
},
71+
'geolytica': {'geocode': Geolytica},
72+
'canadapost': {'geocode': Canadapost},
73+
'opencage': {
74+
'geocode': OpenCage,
75+
'reverse': OpenCageReverse,
76+
},
77+
'bing': {
78+
'geocode': Bing,
79+
'reverse': BingReverse,
80+
},
81+
'google': {
82+
'geocode': Google,
83+
'reverse': GoogleReverse,
84+
'timezone': Timezone,
85+
'elevation': Elevation,
86+
},
87+
}
88+
89+
4090
def get(location, **kwargs):
4191
"""Get Geocode
4292
4393
:param ``location``: Your search location you want geocoded.
4494
:param ``provider``: The geocoding engine you want to use.
95+
4596
:param ``method``: Define the method (geocode, method).
4697
"""
4798
provider = kwargs.get('provider', 'bing').lower().strip()
4899
method = kwargs.get('method', 'geocode').lower().strip()
49-
options = {
50-
'osm': {'geocode': Osm},
51-
'here': {
52-
'geocode': Here,
53-
'reverse': HereReverse,
54-
},
55-
'baidu': {'geocode': Baidu},
56-
'yahoo': {'geocode': Yahoo},
57-
'tomtom': {'geocode': Tomtom},
58-
'arcgis': {'geocode': Arcgis},
59-
'ottawa': {'geocode': Ottawa},
60-
'mapbox': {
61-
'geocode': Mapbox,
62-
'reverse': MapboxReverse,
63-
},
64-
'maxmind': {'geocode': Maxmind},
65-
'ipinfoio': {'geocode': IpinfoIo},
66-
'geonames': {'geocode': Geonames},
67-
'freegeoip': {'geocode': FreeGeoIP},
68-
'w3w': {
69-
'geocode': W3W,
70-
'reverse': W3WReverse,
71-
},
72-
'yandex': {
73-
'geocode': Yandex,
74-
'reverse': YandexReverse,
75-
},
76-
'mapquest': {
77-
'geocode': Mapquest,
78-
'reverse': MapquestReverse,
79-
},
80-
'geolytica': {'geocode': Geolytica},
81-
'canadapost': {'geocode': Canadapost},
82-
'opencage': {
83-
'geocode': OpenCage,
84-
'reverse': OpenCageReverse,
85-
},
86-
'bing': {
87-
'geocode': Bing,
88-
'reverse': BingReverse,
89-
},
90-
'google': {
91-
'geocode': Google,
92-
'reverse': GoogleReverse,
93-
'timezone': Timezone,
94-
'elevation': Elevation,
95-
},
96-
}
97100
if isinstance(location, (list, dict)) and method == 'geocode':
98101
raise ValueError("Location should be a string")
99102

@@ -342,14 +345,14 @@ def maxmind(location='me', **kwargs):
342345
return get(location, provider='maxmind', **kwargs)
343346

344347

345-
def ipinfoio(location='', **kwargs):
346-
"""IpinfoIo Provider
348+
def ipinfo(location='', **kwargs):
349+
"""IP Info.io Provider
347350
348351
:param location: Your search IP Address you want geocoded.
349352
:param location: (optional) if left blank will return your
350353
current IP address's location.
351354
"""
352-
return get(location, provider='ipinfoio', **kwargs)
355+
return get(location, provider='ipinfo', **kwargs)
353356

354357

355358
def freegeoip(location, **kwargs):

geocoder/base.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ class Base(object):
2323
'xy', 'northeast', 'northwest', 'southeast', 'southwest',
2424
'road_long', 'city_long', 'state_long', 'country_long',
2525
'postal_town_long', 'province_long', 'road_long',
26-
'street_long', 'interpolated']
26+
'street_long', 'interpolated', 'method', 'geometry']
2727
fieldnames = []
2828
error = None
2929
status_code = None
@@ -87,7 +87,8 @@ def _connect(self, **kwargs):
8787
)
8888
self.status_code = r.status_code
8989
self.url = r.url
90-
self.content = r.json()
90+
if r.content:
91+
self.status_code = 200
9192
except KeyboardInterrupt:
9293
sys.exit()
9394
except requests.exceptions.SSLError:

geocoder/cli.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
11
#!/usr/bin/python
22
# coding: utf8
33

4+
from __future__ import absolute_import
45
import click
56
import json
67
import geocoder
78
import os
89
import fileinput
10+
from geocoder.api import options
911

1012

11-
providers = ['google', 'bing', 'osm', 'here', 'w3w', 'opencage', 'yandex',
12-
'arcgis', 'tomtom', 'mapquest', 'maxmind', 'baidu', 'canadapost',
13-
'freegeoip', 'geolytica', 'ottawa', 'geonames', 'yahoo', 'mapbox']
13+
providers = options.keys()
1414
methods = ['geocode', 'reverse', 'elevation', 'timezone']
1515
outputs = ['json', 'osm', 'geojson', 'wkt']
1616
units = ['kilometers', 'miles', 'feet', 'meters']

0 commit comments

Comments
 (0)