Created
April 3, 2020 17:00
-
-
Save rvause/f85afef078d91bdb826baba35ea1b09e to your computer and use it in GitHub Desktop.
Revisions
-
rvause created this gist
Apr 3, 2020 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,26 @@ import hashlib import io import json import os from django.core.files.storage import default_storage import googlemaps _client = googlemaps.Client(key=os.environ.get("GOOGLE_GEOCODING_API_KEY")) class Client: def geocode(self, address): filename = hashlib.sha1(address.encode("utf-8")).hexdigest() filepath = f".geocodecache/{filename}" if default_storage.exists(filepath): fp = default_storage.open(filepath) return json.load(fp) resp = _client.geocode(address) fp = io.StringIO() json.dump(resp, fp) default_storage.save(filepath, fp) return resp