diff --git a/modules/freegeoip/__init__.py b/modules/freegeoip/__init__.py new file mode 100644 index 00000000..d4206706 --- /dev/null +++ b/modules/freegeoip/__init__.py @@ -0,0 +1,3 @@ +from .module import FreegeoipModule + +__all__ = ['FreegeoipModule'] diff --git a/modules/freegeoip/favicon.png b/modules/freegeoip/favicon.png new file mode 100644 index 00000000..e59c966c Binary files /dev/null and b/modules/freegeoip/favicon.png differ diff --git a/modules/freegeoip/module.py b/modules/freegeoip/module.py new file mode 100644 index 00000000..29e9a815 --- /dev/null +++ b/modules/freegeoip/module.py @@ -0,0 +1,57 @@ +# -*- coding: utf-8 -*- + +# Copyright(C) 2015 Julien Veyssier +# +# This file is part of weboob. +# +# weboob is free software: you can redistribute it and/or modify +# it under the terms of the GNU Affero General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# weboob is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU Affero General Public License for more details. +# +# You should have received a copy of the GNU Affero General Public License +# along with weboob. If not, see . + + +from weboob.capabilities.geolocip import CapGeolocIp, IpLocation +from weboob.tools.backend import Module +from weboob.browser.browsers import Browser +from weboob.tools.json import json + + +__all__ = ['FreegeoipModule'] + + +class FreegeoipModule(Module, CapGeolocIp): + NAME = 'freegeoip' + MAINTAINER = u'Julien Veyssier' + EMAIL = 'julien.veyssier@aiur.fr' + VERSION = '1.1' + LICENSE = 'AGPLv3+' + DESCRIPTION = u'public API to search the geolocation of IP addresses' + BROWSER = Browser + + def get_location(self, ipaddr): + res = self.browser.location('https://freegeoip.net/json/%s' % ipaddr.encode('utf-8')) + jres = json.loads(res.text) + + iploc = IpLocation(ipaddr) + iploc.city = u'%s'%jres['city'] + iploc.region = u'%s'%jres['region_name'] + iploc.zipcode = u'%s'%jres['zip_code'] + iploc.country = u'%s'%jres['country_name'] + if jres['latitude'] != '': + iploc.lt = float(jres['latitude']) + else: + iploc.lt = 0.0 + if jres['longitude'] != '': + iploc.lg = float(jres['longitude']) + else: + iploc.lg = 0.0 + + return iploc diff --git a/modules/freegeoip/test.py b/modules/freegeoip/test.py new file mode 100644 index 00000000..b055fc7e --- /dev/null +++ b/modules/freegeoip/test.py @@ -0,0 +1,28 @@ +# -*- coding: utf-8 -*- + +# Copyright(C) 2015 Julien Veyssier +# +# This file is part of weboob. +# +# weboob is free software: you can redistribute it and/or modify +# it under the terms of the GNU Affero General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# weboob is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU Affero General Public License for more details. +# +# You should have received a copy of the GNU Affero General Public License +# along with weboob. If not, see . + + +from weboob.tools.test import BackendTest + + +class FreegeoipTest(BackendTest): + MODULE = 'freegeoip' + + def test_freegeoip(self): + self.backend.get_location('88.198.11.130')