Created
April 3, 2020 17:00
-
-
Save rvause/f85afef078d91bdb826baba35ea1b09e to your computer and use it in GitHub Desktop.
This file contains 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 characters
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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment