Skip to content

Commit

Permalink
handle google maps cache cold start
Browse files Browse the repository at this point in the history
  • Loading branch information
fabridamicelli committed Apr 4, 2024
1 parent 56d9223 commit 2da8b7a
Showing 1 changed file with 6 additions and 2 deletions.
8 changes: 6 additions & 2 deletions src/ficamp/classifier/google_apis.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import json
import os
import pathlib

import requests

Expand Down Expand Up @@ -89,7 +90,10 @@ def find_business_category_in_google(field, location=None):

def query_gmaps_category(concept):
"""Pycamp internet is slow. saving data locally to go faster"""
with open("gcache.json") as cache_file:
cache = pathlib.Path("gcache.json")
if not cache.exists(): # Create a valid json file if it doesn't yet exist
cache.write_text("{}")
with open(cache) as cache_file:
cached = json.load(cache_file)
cached_category = cached.get(concept)
if not cached_category:
Expand All @@ -99,7 +103,7 @@ def query_gmaps_category(concept):
except GoogleException as error:
print(f"error: {error}")
gmaps_category = ""
with open("gcache.json", "w") as cache_file:
with open(cache, "w") as cache_file:
cached[concept] = gmaps_category
json.dump(cached, cache_file)
else:
Expand Down

0 comments on commit 2da8b7a

Please sign in to comment.