moonphase depends on the hemishphere (#247)

This commit is contained in:
Igor Chubin 2020-09-20 11:09:57 +02:00
parent a62e3d979d
commit 79eea95904
3 changed files with 28 additions and 3 deletions

View File

@ -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):
"""
"""
@ -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
@ -331,4 +350,5 @@ def location_processing(location, ip_addr):
override_location_name, \
full_address, \
country, \
query_source_location
query_source_location, \
hemisphere

View File

@ -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:
@ -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)

View File

@ -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' \
@ -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