From e5af0dfb4e4b5a7a17771793329c39a35e266e96 Mon Sep 17 00:00:00 2001 From: Denis Date: Tue, 22 Jan 2019 07:27:38 +0300 Subject: [PATCH] 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 --- Readme.md | 2 ++ config.ini-sample | 1 + elodie/geolocation.py | 25 ++++++++++++++++++++++++- elodie/tests/geolocation_test.py | 10 ++++++++++ 4 files changed, 37 insertions(+), 1 deletion(-) diff --git a/Readme.md b/Readme.md index 9054a15..0ecc000 100644 --- a/Readme.md +++ b/Readme.md @@ -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). diff --git a/config.ini-sample b/config.ini-sample index 30098af..bf729c3 100644 --- a/config.ini-sample +++ b/config.ini-sample @@ -1,2 +1,3 @@ [MapQuest] key=your-api-key-goes-here +prefer_english_names=False \ No newline at end of file diff --git a/elodie/geolocation.py b/elodie/geolocation.py index 2af9188..31bbf59 100644 --- a/elodie/geolocation.py +++ b/elodie/geolocation.py @@ -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) diff --git a/elodie/tests/geolocation_test.py b/elodie/tests/geolocation_test.py index c97e757..5b75436 100644 --- a/elodie/tests/geolocation_test.py +++ b/elodie/tests/geolocation_test.py @@ -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