Skip to content

Commit

Permalink
Update code to work with new MapQuest API. gh-421 (#429)
Browse files Browse the repository at this point in the history
  • Loading branch information
jmathai authored Nov 18, 2022
1 parent 449056e commit 18677aa
Show file tree
Hide file tree
Showing 7 changed files with 115 additions and 105 deletions.
2 changes: 1 addition & 1 deletion elodie/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
exiftool_config = path.join(script_directory, 'configs', 'ExifTool_config')

#: Path to MapQuest base URL
mapquest_base_url = 'https://open.mapquestapi.com'
mapquest_base_url = 'https://www.mapquestapi.com'
if (
'ELODIE_MAPQUEST_BASE_URL' in environ and
environ['ELODIE_MAPQUEST_BASE_URL'] != ''
Expand Down
90 changes: 75 additions & 15 deletions elodie/geolocation.py
Original file line number Diff line number Diff line change
Expand Up @@ -191,26 +191,30 @@ def lookup(**kwargs):
):
return None

if('lat' in kwargs and 'lon' in kwargs):
kwargs['location'] = '{},{}'.format(kwargs['lat'], kwargs['lon'])

key = get_key()
prefer_english_names = get_prefer_english_names()

if(key is None):
return None

try:
headers = {}
params = {'format': 'json', 'key': key}
if(prefer_english_names):
headers = {'Accept-Language':'en-EN,en;q=0.8'}
params['locale'] = 'en_US'
params.update(kwargs)
path = '/geocoding/v1/address'
if('lat' in kwargs and 'lon' in kwargs):
path = '/nominatim/v1/reverse.php'
path = '/geocoding/v1/reverse'
url = '%s%s?%s' % (
constants.mapquest_base_url,
path,
urllib.parse.urlencode(params)
)
headers = {}
if(prefer_english_names):
headers = {'Accept-Language':'en-EN,en;q=0.8'}
r = requests.get(url, headers=headers)
return parse_result(r.json())
except requests.exceptions.RequestException as e:
Expand All @@ -223,18 +227,74 @@ def lookup(**kwargs):


def parse_result(result):
if('error' in result):
# gh-421
# Return None if statusCode is not 0
# https://developer.mapquest.com/documentation/geocoding-api/status-codes/
if( 'info' not in result or
'statuscode' not in result['info'] or
result['info']['statuscode'] != 0
):
return None

if(
'results' in result and
len(result['results']) > 0 and
'locations' in result['results'][0]
and len(result['results'][0]['locations']) > 0 and
'latLng' in result['results'][0]['locations'][0]
):
latLng = result['results'][0]['locations'][0]['latLng']
if(latLng['lat'] == 39.78373 and latLng['lng'] == -100.445882):
return None
address = parse_result_address(result)
if(address is None):
return None

result['address'] = address
result['latLng'] = parse_result_latlon(result)

return result

def parse_result_address(result):
# We want to store the city, state and country
# The only way determined to identify an unfound address is
# that none of the indicies were found
if( 'results' not in result or
len(result['results']) == 0 or
'locations' not in result['results'][0] or
len(result['results'][0]['locations']) == 0
):
return None

index_found = False
addresses = {'city': None, 'state': None, 'country': None}
result_compat = {}
result_compat['address'] = {}


locations = result['results'][0]['locations'][0]
# We are looping over locations to find the adminAreaNType key which
# has a value of City, State or Country.
# Once we find it then we obtain the value from the key adminAreaN
# where N is a numeric index.
# For example
# * adminArea1Type = 'City'
# * adminArea1 = 'Sunnyvale'
for key in locations:
# Check if the key is of the form adminArea1Type
if(key[-4:] == 'Type'):
# If it's a type then check if it corresponds to one we are intereated in
# and store the index by parsing the key
key_prefix = key[:-4]
key_index = key[-5:-4]
if(locations[key].lower() in addresses):
addresses[locations[key].lower()] = locations[key_prefix]
index_found = True

if(index_found is False):
return None

return addresses

def parse_result_latlon(result):
if( 'results' not in result or
len(result['results']) == 0 or
'locations' not in result['results'][0] or
len(result['results'][0]['locations']) == 0 or
'latLng' not in result['results'][0]['locations'][0]
):
return None

latLng = result['results'][0]['locations'][0]['latLng'];

return {'lat': latLng['lat'], 'lon': latLng['lng']}
2 changes: 1 addition & 1 deletion elodie/tests/constants_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ def test_exiftool_config():
assert '{}/configs/ExifTool_config'.format(path) == constants.exiftool_config, constants.exiftool_config

def test_mapquest_base_url_default():
assert constants.mapquest_base_url == 'https://open.mapquestapi.com', constants.mapquest_base_url
assert constants.mapquest_base_url == 'https://www.mapquestapi.com', constants.mapquest_base_url

def test_mapquest_base_url_override():
os.environ['ELODIE_MAPQUEST_BASE_URL'] = 'foobar'
Expand Down
28 changes: 14 additions & 14 deletions elodie/tests/elodie_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ def test_import_file_text():
shutil.rmtree(folder)
shutil.rmtree(folder_destination)

assert helper.path_tz_fix(os.path.join('2016-04-Apr','London','2016-04-07_11-15-26-valid-sample-title.txt')) in dest_path, dest_path
assert helper.path_tz_fix(os.path.join('2016-04-Apr','Rainham','2016-04-07_11-15-26-valid-sample-title.txt')) in dest_path, dest_path

def test_import_file_audio():
temporary_folder, folder = helper.create_working_folder()
Expand Down Expand Up @@ -90,7 +90,7 @@ def test_import_file_video():
shutil.rmtree(folder)
shutil.rmtree(folder_destination)

assert helper.path_tz_fix(os.path.join('2015-01-Jan','California','2015-01-19_12-45-11-video.mov')) in dest_path, dest_path
assert helper.path_tz_fix(os.path.join('2015-01-Jan','Pinecrest','2015-01-19_12-45-11-video.mov')) in dest_path, dest_path

def test_import_file_path_utf8_encoded_ascii_checkmark():
temporary_folder, folder = helper.create_working_folder()
Expand All @@ -109,7 +109,7 @@ def test_import_file_path_utf8_encoded_ascii_checkmark():
shutil.rmtree(folder)
shutil.rmtree(folder_destination)

assert helper.path_tz_fix(os.path.join('2016-04-Apr','London',u'2016-04-07_11-15-26-unicode\u2713filename-sample-title.txt')) in dest_path, dest_path
assert helper.path_tz_fix(os.path.join('2016-04-Apr','Rainham',u'2016-04-07_11-15-26-unicode\u2713filename-sample-title.txt')) in dest_path, dest_path

def test_import_file_path_unicode_checkmark():
temporary_folder, folder = helper.create_working_folder()
Expand All @@ -126,7 +126,7 @@ def test_import_file_path_unicode_checkmark():
shutil.rmtree(folder)
shutil.rmtree(folder_destination)

assert helper.path_tz_fix(os.path.join('2016-04-Apr','London',u'2016-04-07_11-15-26-unicode\u2713filename-sample-title.txt')) in dest_path, dest_path
assert helper.path_tz_fix(os.path.join('2016-04-Apr','Rainham',u'2016-04-07_11-15-26-unicode\u2713filename-sample-title.txt')) in dest_path, dest_path

def test_import_file_path_utf8_encoded_ascii_latin_nbsp():
temporary_folder, folder = helper.create_working_folder()
Expand All @@ -145,7 +145,7 @@ def test_import_file_path_utf8_encoded_ascii_latin_nbsp():
shutil.rmtree(folder)
shutil.rmtree(folder_destination)

assert helper.path_tz_fix(os.path.join('2016-04-Apr','London',u'2016-04-07_11-15-26-unicode\xa0filename-sample-title.txt')) in dest_path, dest_path
assert helper.path_tz_fix(os.path.join('2016-04-Apr','Rainham',u'2016-04-07_11-15-26-unicode\xa0filename-sample-title.txt')) in dest_path, dest_path

def test_import_file_path_unicode_latin_nbsp():
temporary_folder, folder = helper.create_working_folder()
Expand All @@ -162,7 +162,7 @@ def test_import_file_path_unicode_latin_nbsp():
shutil.rmtree(folder)
shutil.rmtree(folder_destination)

assert helper.path_tz_fix(os.path.join('2016-04-Apr','London',u'2016-04-07_11-15-26-unicode\xa0filename-sample-title.txt')) in dest_path, dest_path
assert helper.path_tz_fix(os.path.join('2016-04-Apr','Rainham',u'2016-04-07_11-15-26-unicode\xa0filename-sample-title.txt')) in dest_path, dest_path

def test_import_file_allow_duplicate_false():
temporary_folder, folder = helper.create_working_folder()
Expand Down Expand Up @@ -432,8 +432,8 @@ def test_update_location_on_audio():

assert status == True, status
assert metadata['latitude'] != metadata_processed['latitude'], metadata_processed['latitude']
assert helper.isclose(metadata_processed['latitude'], 37.36883), metadata_processed['latitude']
assert helper.isclose(metadata_processed['longitude'], -122.03635), metadata_processed['longitude']
assert helper.isclose(metadata_processed['latitude'], 37.37187), metadata_processed['latitude']
assert helper.isclose(metadata_processed['longitude'], -122.03749), metadata_processed['longitude']

def test_update_location_on_photo():
temporary_folder, folder = helper.create_working_folder()
Expand All @@ -457,8 +457,8 @@ def test_update_location_on_photo():

assert status == True, status
assert metadata['latitude'] != metadata_processed['latitude']
assert helper.isclose(metadata_processed['latitude'], 37.36883), metadata_processed['latitude']
assert helper.isclose(metadata_processed['longitude'], -122.03635), metadata_processed['longitude']
assert helper.isclose(metadata_processed['latitude'], 37.37187), metadata_processed['latitude']
assert helper.isclose(metadata_processed['longitude'], -122.03749), metadata_processed['longitude']

def test_update_location_on_text():
temporary_folder, folder = helper.create_working_folder()
Expand All @@ -482,8 +482,8 @@ def test_update_location_on_text():

assert status == True, status
assert metadata['latitude'] != metadata_processed['latitude']
assert helper.isclose(metadata_processed['latitude'], 37.36883), metadata_processed['latitude']
assert helper.isclose(metadata_processed['longitude'], -122.03635), metadata_processed['longitude']
assert helper.isclose(metadata_processed['latitude'], 37.37187), metadata_processed['latitude']
assert helper.isclose(metadata_processed['longitude'], -122.03749), metadata_processed['longitude']

def test_update_location_on_video():
temporary_folder, folder = helper.create_working_folder()
Expand All @@ -507,8 +507,8 @@ def test_update_location_on_video():

assert status == True, status
assert metadata['latitude'] != metadata_processed['latitude']
assert helper.isclose(metadata_processed['latitude'], 37.36883), metadata_processed['latitude']
assert helper.isclose(metadata_processed['longitude'], -122.03635), metadata_processed['longitude']
assert helper.isclose(metadata_processed['latitude'], 37.37187), metadata_processed['latitude']
assert helper.isclose(metadata_processed['longitude'], -122.03749), metadata_processed['longitude']

def test_update_time_on_audio():
temporary_folder, folder = helper.create_working_folder()
Expand Down
2 changes: 1 addition & 1 deletion elodie/tests/files/plugins/googlephotos/auth_file.json
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{"scopes": ["https://www.googleapis.com/auth/photoslibrary", "https://www.googleapis.com/auth/photoslibrary.appendonly", "https://www.googleapis.com/auth/photoslibrary.sharing"], "id_token": null, "token": "ya29.Gls1B3ymBdURb3tBLAUJgxQtfTzKry0eaUqplbkHxfIYJH9sWvkalLwXprfAW-Ku0Fz8aP3jz7NrncJ2h58idSEgrYXPQ14iVSkwgUGE2gnsxZM6w4TbHz8ny8Yf", "client_id": "1004259275591-ogsk179e96cs0h126qj590mofk86gdqo.apps.googleusercontent.com", "token_uri": "https://oauth2.googleapis.com/token", "client_secret": "p84GOD_i7_PwDGbAmpnbwKiH", "refresh_token": "1/iHiK9Vbq9i5ebXScyQPDZf9_GJrDOWMvu-2zG9wRsDA"}
{"token": "ya29.a0AeTM1idGAHvw-zLeCprgQHCcupfejlqTrYR6TwjyD_XLwMNnDpyg5XqeIkyKL5d4n4aY40CqDmmr-2zWijdcEN5N75Sdi6208X1QX-wdTim9_5oJMItheGWPRkVpb6axXYurZQhn_bDMvDfJ1AtUKyuS7WAFaCgYKARQSARMSFQHWtWOmHcHdWbLPcrjxV-9XfRC7uw0163", "refresh_token": "1//06ArUL6W7UtL0CgYIARAAGAYSNwF-L9IrUKbhHdzrRqELx73vB7259gEgZ6Er2DMRB7bQrFu-usNSuDHaMWEGbTjts1Tz2SwbqX0", "id_token": null, "scopes": ["https://www.googleapis.com/auth/photoslibrary.appendonly"], "token_uri": "https://oauth2.googleapis.com/token", "client_id": "1004259275591-ogsk179e96cs0h126qj590mofk86gdqo.apps.googleusercontent.com", "client_secret": "p84GOD_i7_PwDGbAmpnbwKiH"}
24 changes: 12 additions & 12 deletions elodie/tests/filesystem_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -543,7 +543,7 @@ def test_get_folder_path_with_custom_path():
if hasattr(load_config, 'config'):
del load_config.config

assert path == os.path.join('2015-12-05','United States of America-California-Sunnyvale'), path
assert path == os.path.join('2015-12-05','US-CA-Sunnyvale'), path

@mock.patch('elodie.config.config_file', '%s/config.ini-fallback' % gettempdir())
def test_get_folder_path_with_fallback_folder():
Expand Down Expand Up @@ -588,7 +588,7 @@ def test_get_folder_path_with_with_more_than_two_levels():
if hasattr(load_config, 'config'):
del load_config.config

assert path == os.path.join('2015','12','Sunnyvale, California'), path
assert path == os.path.join('2015','12','Sunnyvale, CA'), path

@mock.patch('elodie.config.config_file', '%s/config.ini-location-date' % gettempdir())
def test_get_folder_path_with_with_only_one_level():
Expand Down Expand Up @@ -621,7 +621,7 @@ def test_parse_folder_name_default():
if hasattr(load_config, 'config'):
del load_config.config
filesystem = FileSystem()
place_name = {'default': u'California', 'country': u'United States of America', 'state': u'California', 'city': u'Sunnyvale'}
place_name = {'default': u'CA', 'country': u'US', 'state': u'CA', 'city': u'Sunnyvale'}
mask = '%city'
location_parts = re.findall('(%[^%]+)', mask)
path = filesystem.parse_mask_for_location(mask, location_parts, place_name)
Expand All @@ -634,20 +634,20 @@ def test_parse_folder_name_multiple():
if hasattr(load_config, 'config'):
del load_config.config
filesystem = FileSystem()
place_name = {'default': u'California', 'country': u'United States of America', 'state': u'California', 'city': u'Sunnyvale'}
place_name = {'default': u'CA', 'country': u'US', 'state': u'CA', 'city': u'Sunnyvale'}
mask = '%city-%state-%country'
location_parts = re.findall('(%[^%]+)', mask)
path = filesystem.parse_mask_for_location(mask, location_parts, place_name)
if hasattr(load_config, 'config'):
del load_config.config

assert path == 'Sunnyvale-California-United States of America', path
assert path == 'Sunnyvale-CA-US', path

def test_parse_folder_name_static_chars():
if hasattr(load_config, 'config'):
del load_config.config
filesystem = FileSystem()
place_name = {'default': u'California', 'country': u'United States of America', 'state': u'California', 'city': u'Sunnyvale'}
place_name = {'default': u'CA', 'country': u'US', 'state': u'CA', 'city': u'Sunnyvale'}
mask = '%city-is-the-city'
location_parts = re.findall('(%[^%]+)', mask)
path = filesystem.parse_mask_for_location(mask, location_parts, place_name)
Expand All @@ -660,40 +660,40 @@ def test_parse_folder_name_key_not_found():
if hasattr(load_config, 'config'):
del load_config.config
filesystem = FileSystem()
place_name = {'default': u'California', 'country': u'United States of America', 'state': u'California'}
place_name = {'default': u'CA', 'country': u'US', 'state': u'CA'}
mask = '%city'
location_parts = re.findall('(%[^%]+)', mask)
path = filesystem.parse_mask_for_location(mask, location_parts, place_name)
if hasattr(load_config, 'config'):
del load_config.config

assert path == 'California', path
assert path == 'CA', path

def test_parse_folder_name_key_not_found_with_static_chars():
if hasattr(load_config, 'config'):
del load_config.config
filesystem = FileSystem()
place_name = {'default': u'California', 'country': u'United States of America', 'state': u'California'}
place_name = {'default': u'CA', 'country': u'US', 'state': u'CA'}
mask = '%city-is-not-found'
location_parts = re.findall('(%[^%]+)', mask)
path = filesystem.parse_mask_for_location(mask, location_parts, place_name)
if hasattr(load_config, 'config'):
del load_config.config

assert path == 'California', path
assert path == 'CA', path

def test_parse_folder_name_multiple_keys_not_found():
if hasattr(load_config, 'config'):
del load_config.config
filesystem = FileSystem()
place_name = {'default': u'United States of America', 'country': u'United States of America'}
place_name = {'default': u'US', 'country': u'US'}
mask = '%city-%state'
location_parts = re.findall('(%[^%]+)', mask)
path = filesystem.parse_mask_for_location(mask, location_parts, place_name)
if hasattr(load_config, 'config'):
del load_config.config

assert path == 'United States of America', path
assert path == 'US', path

def test_process_file_invalid():
filesystem = FileSystem()
Expand Down
Loading

0 comments on commit 18677aa

Please sign in to comment.