Skip to content

Commit 3b40706

Browse files
authored
Merge pull request #6 from ronlut/master
Add support for timezoneJSON endpoint of geonames
2 parents 4741766 + 4cc3cc5 commit 3b40706

File tree

2 files changed

+64
-1
lines changed

2 files changed

+64
-1
lines changed

geocoder/api.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@
6262
from geocoder.geonames_details import GeonamesDetails
6363
from geocoder.geonames_children import GeonamesChildren
6464
from geocoder.geonames_hierarchy import GeonamesHierarchy
65+
from geocoder.geonames_timezone import GeonamesTimezone
6566

6667
# Google Services
6768
from geocoder.google import GoogleQuery
@@ -107,7 +108,7 @@
107108
'geonames': {
108109
'geocode': GeonamesQuery,
109110
'details': GeonamesDetails,
110-
'timezone': GeonamesDetails,
111+
'timezone': GeonamesTimezone,
111112
'children': GeonamesChildren,
112113
'hierarchy': GeonamesHierarchy
113114
},

geocoder/geonames_timezone.py

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
from __future__ import absolute_import
2+
3+
from geocoder.geonames import GeonamesQuery, GeonamesResult
4+
from geocoder.location import Location
5+
6+
7+
class GeonamesTimezoneResult(GeonamesResult):
8+
""" Get timezone information for given lat,lng"""
9+
10+
@property
11+
def sunrise(self):
12+
return self.raw.get('sunrise')
13+
14+
@property
15+
def gmt_offset(self):
16+
return self.raw.get('gmtOffset')
17+
18+
@property
19+
def raw_offset(self):
20+
return self.raw.get('rawOffset')
21+
22+
@property
23+
def dst_offest(self):
24+
return self.raw.get('dstOffset')
25+
26+
@property
27+
def sunset(self):
28+
return self.raw.get('sunset')
29+
30+
@property
31+
def timezone_id(self):
32+
return self.raw.get('timezoneId')
33+
34+
@property
35+
def time(self):
36+
return self.raw.get('time')
37+
38+
39+
class GeonamesTimezone(GeonamesQuery):
40+
""" Details:
41+
http://api.geonames.org/timezoneJSON?lat=47.01&lng=10.2
42+
"""
43+
44+
provider = 'geonames'
45+
method = 'timezone'
46+
47+
_URL = 'http://api.geonames.org/timezoneJSON'
48+
_RESULT_CLASS = GeonamesTimezoneResult
49+
50+
def _build_params(self, location, provider_key, **kwargs):
51+
"""Will be overridden according to the targetted web service"""
52+
location = Location(location)
53+
return {
54+
'lat': location.latitude,
55+
'lng': location.longitude,
56+
'username': provider_key
57+
}
58+
59+
def _adapt_results(self, json_response):
60+
# the returned JSON contains the object.
61+
# Need to wrap it into an array
62+
return [json_response]

0 commit comments

Comments
 (0)