From 46b553dee384b4db6fc7c1608372bd4302a18eb2 Mon Sep 17 00:00:00 2001 From: Steffen Rehberg Date: Tue, 15 Dec 2020 18:06:40 +0100 Subject: [PATCH 1/2] TOOLS: Fix loadGeoPossibilities Since mid 2018 you need an API key to use the Google maps API. If you have such a key, store it in a file googlemaps_api_key.txt in the same directory as add_library.py (this file is not checked into version control). If you don't have a Google API key, we use OSM Nominatim instead (no key required). --- .gitignore | 1 + tools/add_library.py | 62 +++++++++++++++++++++++++++++--------------- 2 files changed, 42 insertions(+), 21 deletions(-) diff --git a/.gitignore b/.gitignore index 392a7050e..14d5a2d5e 100644 --- a/.gitignore +++ b/.gitignore @@ -61,3 +61,4 @@ opacclient/opacapp/src/main/assets/bibs/*.json opacclient/opacapp/src/main/assets/last_library_config_update.txt opacclient/opacapp/src/main/resources/sentry.properties +tools/googlemaps_api_key.txt diff --git a/tools/add_library.py b/tools/add_library.py index 6f63818d8..87979d2ff 100755 --- a/tools/add_library.py +++ b/tools/add_library.py @@ -9,6 +9,7 @@ from bs4 import BeautifulSoup LIBDIR = 'opacclient/opacapp/src/main/assets/bibs/' +API_KEY_FILE = 'googlemaps_api_key.txt' TYPES = [ 'NONE', 'BOOK', 'CD', 'CD_SOFTWARE', 'CD_MUSIC', 'DVD', 'MOVIE', 'AUDIOBOOK', 'PACKAGE', 'GAME_CONSOLE', 'EBOOK', 'SCORE_MUSIC', 'PACKAGE_BOOKS', 'UNKNOWN', 'NEWSPAPER', @@ -33,28 +34,41 @@ def getInput(required=False, default=None): return inp -def loadGeoPossibilities(data): +def loadGeoPossibilities(data, api_key): possibilities = [] - for address in ('%s, %s, %s' % (data['title'], data['city'], data['state']), - '%s, %s, %s' % ('Bibliothek', data['city'], data['state']), - data['city']): - uri = 'https://maps.googleapis.com/maps/api/geocode/json?' + \ - urllib.parse.urlencode({'address': address, 'sensor': 'true'}) - jsoncontent = urllib.request.urlopen(uri).read().decode() - geocode = json.loads(jsoncontent) - - if geocode['status'] != 'OK': - print("ERROR!") - - for res in geocode['results']: - possibilities.append( - ( - ", ".join([a["long_name"] for a in res['address_components']]), - [float(res['geometry']['location']['lat']), float( - res['geometry']['location']['lng'])] + if api_key: + for address in ('%s, %s, %s' % (data['title'], data['city'], data['state']), + '%s, %s, %s' % ('Bibliothek', data['city'], data['state']), + data['city']): + uri = f'https://maps.googleapis.com/maps/api/geocode/json?key={api_key}&' + \ + urllib.parse.urlencode({'address': address, 'sensor': 'true'}) + jsoncontent = urllib.request.urlopen(uri).read().decode() + geocode = json.loads(jsoncontent) + + if geocode['status'] != 'OK': + print("ERROR!") + + for res in geocode['results']: + possibilities.append( + ( + ", ".join([a["long_name"] for a in res['address_components']]), + [float(res['geometry']['location']['lat']), float( + res['geometry']['location']['lng'])] + ) ) - ) + else: + api_url = 'https://nominatim.openstreetmap.org/search' + query = 'format=json&polygon=0&addressdetails=0&limit=40' + parameters1 = urllib.parse.quote(','.join((data['title'], data['city'].partition('(')[0], + data['state'],data['country'])), safe=',') + parameters2 = urllib.parse.urlencode({'country': data['country'], 'state': data['state'], + 'city': data['city'].partition('(')[0]}) + for uri in (f'{api_url}/{parameters1}?{query}', + f'{api_url}?{query}&amenity=library&{parameters2}'): + jsoncontent = urllib.request.urlopen(uri).read().decode() + for p in json.loads(jsoncontent): + possibilities.append((p["display_name"], [float(p['lat']), float(p['lon'])])) return possibilities @@ -399,8 +413,14 @@ def prompt(self, data): data['title'] = getInput(default="Stadtbibliothek") print("Lade Geodaten...") - - geo = loadGeoPossibilities(data) + try: + with open(os.path.join(os.path.dirname(__file__), API_KEY_FILE)) as f: + api_key = f.read().strip() + except: + print(f'Keine Google Maps API key Datei ({API_KEY_FILE}) gefunden, verwende OSM Nominatim.') + api_key = None + + geo = loadGeoPossibilities(data, api_key) for k, g in enumerate(geo): print("[%d] %s" % (k + 1, g[0])) From a99bc40eb7015575d2d61f98c77da1e244654cfb Mon Sep 17 00:00:00 2001 From: Steffen Rehberg Date: Wed, 16 Dec 2020 08:52:01 +0100 Subject: [PATCH 2/2] TOOLS: supply default for api_key of loadGeoPossibilities to make it work with add_auto_libraries.py and geoloc.py too where it is imported from add_library.py. --- tools/add_library.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tools/add_library.py b/tools/add_library.py index 87979d2ff..dba646092 100755 --- a/tools/add_library.py +++ b/tools/add_library.py @@ -34,7 +34,7 @@ def getInput(required=False, default=None): return inp -def loadGeoPossibilities(data, api_key): +def loadGeoPossibilities(data, api_key=None): possibilities = [] if api_key: