Skip to content

Instantly share code, notes, and snippets.

@rvause
Created April 3, 2020 17:00
Show Gist options
  • Select an option

  • Save rvause/f85afef078d91bdb826baba35ea1b09e to your computer and use it in GitHub Desktop.

Select an option

Save rvause/f85afef078d91bdb826baba35ea1b09e to your computer and use it in GitHub Desktop.

Revisions

  1. rvause created this gist Apr 3, 2020.
    26 changes: 26 additions & 0 deletions geocode_proxy.py
    Original 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