Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for timezoneJSON endpoint of geonames #6

Merged
merged 2 commits into from
May 10, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion geocoder/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -107,7 +108,7 @@
'geonames': {
'geocode': GeonamesQuery,
'details': GeonamesDetails,
'timezone': GeonamesDetails,
'timezone': GeonamesTimezone,
'children': GeonamesChildren,
'hierarchy': GeonamesHierarchy
},
Expand Down
62 changes: 62 additions & 0 deletions geocoder/geonames_timezone.py
Original file line number Diff line number Diff line change
@@ -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=47.01&lng=10.2
"""

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]