Skip to content

Commit

Permalink
Merge pull request #35 from rednaks/33-ipdataco
Browse files Browse the repository at this point in the history
implemment ipdataco #33
  • Loading branch information
rednaks authored Feb 29, 2020
2 parents eb10248 + ca54204 commit e14e5d0
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 1 deletion.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
[![Downloads](https://pepy.tech/badge/django-ip-geolocation)](https://pepy.tech/project/django-ip-geolocation) [![Build Status](https://travis-ci.org/rednaks/django-ip-geolocation.svg?branch=release)](https://travis-ci.org/rednaks/django-ip-geolocation)

# Django Ip Geolocation
Django request/response hooks to geolocate visitors by their ip address.

Expand Down Expand Up @@ -81,6 +83,7 @@ Those are the default settings, that will be overwritten by those set in `settin
* `django_ip_geolocation.backends.IPGeolocationAPI` : (Default) Using https://ipgeolocationapi.com/
* `django_ip_geolocation.backends.IPStack` : (Require `BACKEND_API_KEY`) Using https://ipstack.com/documentation
* `django_ip_geolocation.backends.IP2LocationCom` : (Require `BACKEND_API_KEY`, Accepts `BACKEND_EXTRA_PARAMS`) Using https://www.ip2location.com/web-service/ip2location
* `django_ip_geolocation.backends.IPDataCo` : (Require `BACKEND_API_KEY`) Using https://docs.ipdata.co/


## Implementing your own backend
Expand Down
3 changes: 2 additions & 1 deletion django_ip_geolocation/backends/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
from django_ip_geolocation.backends.ipgeolocationapi import IPGeolocationAPI
from django_ip_geolocation.backends.ipstack import IPStack
from django_ip_geolocation.backends.ip2locationcom import IP2LocationCom
from django_ip_geolocation.backends.ipdataco import IPDataCo


__all__ = ['GeolocationBackend', 'IPGeolocationAPI',
'IPStack', 'IP2LocationCom']
'IPStack', 'IP2LocationCom', 'IPDataCo']
35 changes: 35 additions & 0 deletions django_ip_geolocation/backends/ipdataco.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
"""ipdata.co service integration."""
import requests
from django_ip_geolocation.backends import GeolocationBackend
from django_ip_geolocation import settings


class IPDataCo(GeolocationBackend):
"""ipdata.co backend implementation."""

def geolocate(self):
"""Call ipdata api."""
api_key = settings.IP_GEOLOCATION_SETTINGS.get('BACKEND_API_KEY')

if not api_key:
msg = "BACKEND_API_KEY is required. Please provide an API_KEY in IP_GEOLOCATION_SETTINGS" # noqa: E501
raise Exception(msg)

payload = {'api-key': api_key}
url = 'https://api.ipdata.co/{}'.format(self._ip)
res = requests.get(url, params=payload)
if res.ok:
self._raw_data = res.json()

def _parse(self):
"""Parse raw data."""
self._continent = self._raw_data.get('continent_name')
self._country = {
'code': self._raw_data.get('country_code'),
'name': self._raw_data.get('country_name'),
}

self._geo_data = {
'latitude': self._raw_data.get('latitude'),
'longitude': self._raw_data.get('longitude'),
}

0 comments on commit e14e5d0

Please sign in to comment.