From 4fa179848be4a7d271597cc7f5b9d44c47ffcbe1 Mon Sep 17 00:00:00 2001 From: Rony Lutsky Date: Sat, 30 Jun 2018 13:56:37 +0300 Subject: [PATCH 1/2] Add geonames timezone endpoint support --- geocoder/api.py | 3 +- geocoder/geonames_timezone.py | 62 +++++++++++++++++++++++++++++++++++ 2 files changed, 64 insertions(+), 1 deletion(-) create mode 100644 geocoder/geonames_timezone.py diff --git a/geocoder/api.py b/geocoder/api.py index 546d4cd1..afd4f203 100755 --- a/geocoder/api.py +++ b/geocoder/api.py @@ -62,6 +62,7 @@ from geocoder.geonames_details import GeonamesDetails from geocoder.geonames_children import GeonamesChildren from geocoder.geonames_hierarchy import GeonamesHierarchy +from geocoder.geonames_timezone import GeonamesTimezone # Google Services from geocoder.google import GoogleQuery @@ -107,7 +108,7 @@ 'geonames': { 'geocode': GeonamesQuery, 'details': GeonamesDetails, - 'timezone': GeonamesDetails, + 'timezone': GeonamesTimezone, 'children': GeonamesChildren, 'hierarchy': GeonamesHierarchy }, diff --git a/geocoder/geonames_timezone.py b/geocoder/geonames_timezone.py new file mode 100644 index 00000000..90340ac6 --- /dev/null +++ b/geocoder/geonames_timezone.py @@ -0,0 +1,62 @@ +from __future__ import absolute_import + +from geocoder.geonames import GeonamesQuery, GeonamesResult +from geocoder.location import Location + + +class GeonamesTimezoneResult(GeonamesResult): + """ Get timezone information for given lat,lng""" + + @property + def sunrise(self): + return self.raw.get('sunrise') + + @property + def gmt_offset(self): + return self.raw.get('gmtOffset') + + @property + def raw_offset(self): + return self.raw.get('rawOffset') + + @property + def dst_offest(self): + return self.raw.get('dstOffset') + + @property + def sunset(self): + return self.raw.get('sunset') + + @property + def timezone_id(self): + return self.raw.get('timezoneId') + + @property + def time(self): + return self.raw.get('time') + + +class GeonamesTimezone(GeonamesQuery): + """ Details: + http://api.geonames.org/timezoneJSON?lat=*&lng=* + """ + + provider = 'geonames' + method = 'timezone' + + _URL = 'http://api.geonames.org/timezoneJSON' + _RESULT_CLASS = GeonamesTimezoneResult + + def _build_params(self, location, provider_key, **kwargs): + """Will be overridden according to the targetted web service""" + location = Location(location) + return { + 'lat': location.latitude, + 'lng': location.longitude, + 'username': provider_key + } + + def _adapt_results(self, json_response): + # the returned JSON contains the object. + # Need to wrap it into an array + return [json_response] From 4cc3cc5e58ef0f54295b7a42821246ad688e6743 Mon Sep 17 00:00:00 2001 From: Rony Lutsky Date: Sat, 30 Jun 2018 13:57:59 +0300 Subject: [PATCH 2/2] Fix documentation --- geocoder/geonames_timezone.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/geocoder/geonames_timezone.py b/geocoder/geonames_timezone.py index 90340ac6..6987d097 100644 --- a/geocoder/geonames_timezone.py +++ b/geocoder/geonames_timezone.py @@ -38,7 +38,7 @@ def time(self): class GeonamesTimezone(GeonamesQuery): """ Details: - http://api.geonames.org/timezoneJSON?lat=*&lng=* + http://api.geonames.org/timezoneJSON?lat=47.01&lng=10.2 """ provider = 'geonames'