Skip to content

Instantly share code, notes, and snippets.

@rvause
Created April 3, 2020 17:00
Show Gist options
  • Save rvause/f85afef078d91bdb826baba35ea1b09e to your computer and use it in GitHub Desktop.
Save rvause/f85afef078d91bdb826baba35ea1b09e to your computer and use it in GitHub Desktop.
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