From 5fc1bea1bdc23a06cc4b24f3b459dcef300391d9 Mon Sep 17 00:00:00 2001 From: Julien Veyssier Date: Sun, 22 Feb 2015 00:02:35 +0100 Subject: [PATCH] freegeoip module for CapGeolocip --- modules/freegeoip/__init__.py | 3 ++ modules/freegeoip/favicon.png | Bin 0 -> 1318 bytes modules/freegeoip/module.py | 57 ++++++++++++++++++++++++++++++++++ modules/freegeoip/test.py | 28 +++++++++++++++++ 4 files changed, 88 insertions(+) create mode 100644 modules/freegeoip/__init__.py create mode 100644 modules/freegeoip/favicon.png create mode 100644 modules/freegeoip/module.py create mode 100644 modules/freegeoip/test.py 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 0000000000000000000000000000000000000000..e59c966c36325cf542828442d8e07400d670ca96 GIT binary patch literal 1318 zcmV+>1=;$EP)?iA~#D+j2A9h{0LDJ zugv1MG#iUcyz#~hZ+4+KCYqU$&6sS}sfcrmMN`U0S3cIlX~&=iTH4|Ta?TjEo>Q7C z>-$fdHs^iLo4&t3=YP)ol!}KdR*=K>6mq->00MvjAOHve0)PM@00;mAfB+x>2!LEH zI^7c$tSjYsJX}E>@I*UcJO0}Li32|TD2UdM36vik1)Dzvz0r-9_Hki66iVRz4`xt) za1`o17xtKk(RpeTcYj;jFqa&y)S}YO1Rw1IsT{CbuV^}^#r10@%+1A7-#m^#oRJM0 zm5Rd68z$6LYXCr&HtP;hu&#BlWHsw6LvX4TpL}ct0N8s1!h)`Tx)&k73}1X+2mpvi zS8(<`FPtk!n{@}I>ItcwVzG_DJ043qk4{J7p}$>J@VIVrY8kZ+ zUQAAHw0Grx6$bh(X=%5O1Bwcj_~kZ-t~Yn0#G-^!2`~(WeKr-2)@#$&X2k(yH%I^w z00aO5KmZT`S*fL^1lnGnfX*<4&Qp_!MptqRz@58G=)E4s;^GRvycU9UFp^sUo_~%( zOEU|Gq0rvS;y|T3V{?-yLDB>z*1|#p_MQL^9ri$H7=k*_g~F0y)Hiw2)$L!Wl@neD zgK@n3-ZU(>5vcQA;L1nP)$Iq*$2ROLACKI+y8Wm-?nPnAFw}W2=nO+Rbl8In7kPxk ziOgLBb8~Stwv6NVf#l6+ZL2FMyyW>9Y8$;69b4X@eTGxwr(0(1-c2XnR;P;P4pgde z>xLP6y<+PQu=fOn#UE?b!s{+Uh%Za3l^R~2vj>F5cXsG-=LZg*9XbGj=iV~T*#nZT z91fIWV%Uo2 z1@~|iwyLpzG5{K0w^fn@){Z#`BdB`OvpRn<4UfyZbq7pLiWaq1q|V~xgqPDZqSkCu z0suI(QaI+Hj>&FnWu@Zz_kFRt4}0F%~lxh?%U< zA6xeh00n#GXSOrE`2BY6Qe+0eky`N|7yG3E=I0am^RLYtqMY#Zr8b@L`SO(@W@qEL zd?hFxYdgtGUbu5Gg8BIb?3cvrUP@FZsR#7+FQUH5i&zRrtzCn1!b_fyp{Cv|^p{7g zxQIrdgM-nS-d%uYDfBrw?6ax2?tq$V4ZiI);b?;vS}g^Jp-@}H2;)0;h||akFAWA7 zeGW6;I-^H%5e=FK3ir}D^ST~Ce2mk_r c0Laby8yF}?Ndxy)hyVZp07*qoM6N<$f)6}bG5`Po literal 0 HcmV?d00001 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')