Implements prefer english names setting for MapQuest API in config.ini (#290)
Implements prefer english names setting in config.ini which adds English as accepted header to MapQuest requests and results in the response location names being mostly in English not their native country language
This commit is contained in:
parent
69937ca1a3
commit
e5af0dfb4e
|
@ -349,6 +349,8 @@ cp config.ini-sample ~/.elodie/config.ini
|
|||
# now you're ready to add your MapQuest key
|
||||
```
|
||||
|
||||
If you're an english speaker then you will probably want to add `prefer_english_names=True` to the `[MapQuest]` section else you'll have cities named using the local language.
|
||||
|
||||
## Questions, comments or concerns?
|
||||
|
||||
The best ways to provide feedback is by opening a [GitHub issue](https://github.com/jmathai/elodie/issues) or emailing me at [jaisen@jmathai.com](mailto:jaisen@jmathai.com).
|
||||
|
|
|
@ -1,2 +1,3 @@
|
|||
[MapQuest]
|
||||
key=your-api-key-goes-here
|
||||
prefer_english_names=False
|
|
@ -20,6 +20,7 @@ from elodie.localstorage import Db
|
|||
|
||||
__KEY__ = None
|
||||
__DEFAULT_LOCATION__ = 'Unknown Location'
|
||||
__PREFER_ENGLISH_NAMES__ = None
|
||||
|
||||
|
||||
def coordinates_by_name(name):
|
||||
|
@ -114,6 +115,24 @@ def get_key():
|
|||
__KEY__ = config['MapQuest']['key']
|
||||
return __KEY__
|
||||
|
||||
def get_prefer_english_names():
|
||||
global __PREFER_ENGLISH_NAMES__
|
||||
if __PREFER_ENGLISH_NAMES__ is not None:
|
||||
return __PREFER_ENGLISH_NAMES__
|
||||
|
||||
config_file = '%s/config.ini' % constants.application_directory
|
||||
if not path.exists(config_file):
|
||||
return False
|
||||
|
||||
config = load_config()
|
||||
if('MapQuest' not in config):
|
||||
return False
|
||||
|
||||
if('prefer_english_names' not in config['MapQuest']):
|
||||
return False
|
||||
|
||||
__PREFER_ENGLISH_NAMES__ = bool(config['MapQuest']['prefer_english_names'])
|
||||
return __PREFER_ENGLISH_NAMES__
|
||||
|
||||
def place_name(lat, lon):
|
||||
lookup_place_name_default = {'default': __DEFAULT_LOCATION__}
|
||||
|
@ -167,6 +186,7 @@ def lookup(**kwargs):
|
|||
return None
|
||||
|
||||
key = get_key()
|
||||
prefer_english_names = get_prefer_english_names()
|
||||
|
||||
if(key is None):
|
||||
return None
|
||||
|
@ -181,7 +201,10 @@ def lookup(**kwargs):
|
|||
path,
|
||||
urllib.parse.urlencode(params)
|
||||
)
|
||||
r = requests.get(url)
|
||||
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:
|
||||
log.error(e)
|
||||
|
|
|
@ -112,6 +112,16 @@ def test_lookup_with_valid_key():
|
|||
assert latLng['lat'] == 37.36883, latLng
|
||||
assert latLng['lng'] == -122.03635, latLng
|
||||
|
||||
@mock.patch('elodie.geolocation.__PREFER_ENGLISH_NAMES__', True)
|
||||
def test_lookup_with_prefer_english_names_true():
|
||||
res = geolocation.lookup(lat=55.66333, lon=37.61583)
|
||||
assert res['address']['city'] == 'Nagorny District', res
|
||||
|
||||
@mock.patch('elodie.geolocation.__PREFER_ENGLISH_NAMES__', False)
|
||||
def test_lookup_with_prefer_english_names_false():
|
||||
res = geolocation.lookup(lat=55.66333, lon=37.61583)
|
||||
assert res['address']['city'] == u'\u041d\u0430\u0433\u043e\u0440\u043d\u044b\u0439 \u0440\u0430\u0439\u043e\u043d', res
|
||||
|
||||
@mock.patch('elodie.constants.location_db', '%s/location.json-cached' % gettempdir())
|
||||
def test_place_name_deprecated_string_cached():
|
||||
# See gh-160 for backwards compatability needed when a string is stored instead of a dict
|
||||
|
|
Loading…
Reference in New Issue