Skip to content

Commit

Permalink
moonphase depends on the hemishphere (#247)
Browse files Browse the repository at this point in the history
  • Loading branch information
chubin committed Sep 20, 2020
1 parent a62e3d9 commit 79eea95
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 3 deletions.
22 changes: 21 additions & 1 deletion lib/location.py
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,19 @@ def is_location_blocked(location):
return location is not None and location.lower() in LOCATION_BLACK_LIST


def get_hemisphere(location):
"""
Return hemisphere of the location (True = North, False = South).
Assume North and return True if location can't be found.
"""
location_string = location[0]
if location[1] is not None:
location_string += ",%s" % location[1]
geolocation = geolocator(location_string)
if geolocation is None:
return True
return geolocation["latitude"] > 0

def location_processing(location, ip_addr):
"""
"""
Expand Down Expand Up @@ -281,6 +294,12 @@ def location_processing(location, ip_addr):

query_source_location = get_location(ip_addr)

# For moon queries, hemisphere must be found
# True for North, False for South
hemisphere = False
if location is not None and (location.lower()+"@").startswith("moon@"):
hemisphere = get_hemisphere(query_source_location)

country = None
if not location or location == 'MyLocation':
location = ip_addr
Expand Down Expand Up @@ -331,4 +350,5 @@ def location_processing(location, ip_addr):
override_location_name, \
full_address, \
country, \
query_source_location
query_source_location, \
hemisphere
4 changes: 4 additions & 0 deletions lib/view/moon.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ def get_moon(parsed_query):
location = parsed_query['orig_location']
html = parsed_query['html_output']
lang = parsed_query['lang']
hemisphere = parsed_query['hemisphere']

date = None
if '@' in location:
Expand All @@ -25,6 +26,9 @@ def get_moon(parsed_query):
if lang:
cmd += ["-l", lang]

if not hemisphere:
cmd += ["-s", "south"]

if date:
try:
dateutil.parser.parse(date)
Expand Down
5 changes: 3 additions & 2 deletions lib/wttr_srv.py
Original file line number Diff line number Diff line change
Expand Up @@ -294,7 +294,7 @@ def parse_request(location, request, query, fast_mode=False):
parsed_query["html_output"] = get_output_format(query, parsed_query)

if not fast_mode: # not png_filename and not fast_mode:
location, override_location_name, full_address, country, query_source_location = \
location, override_location_name, full_address, country, query_source_location, hemisphere = \
location_processing(parsed_query["location"], parsed_query["ip_addr"])

us_ip = query_source_location[1] == 'United States' \
Expand All @@ -309,7 +309,8 @@ def parse_request(location, request, query, fast_mode=False):
'override_location_name': override_location_name,
'full_address': full_address,
'country': country,
'query_source_location': query_source_location})
'query_source_location': query_source_location,
'hemisphere': hemisphere})

parsed_query.update(query)
return parsed_query
Expand Down

0 comments on commit 79eea95

Please sign in to comment.