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 2, 2024
1 parent 56d9223 commit b6dd18b
Showing 1 changed file with 5 additions and 2 deletions.
7 changes: 5 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,9 @@ 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")
cache.touch(exist_ok=True) # Create file if it doesn't yet exist
with open(cache) as cache_file:
cached = json.load(cache_file)
cached_category = cached.get(concept)
if not cached_category:
Expand All @@ -99,7 +102,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 b6dd18b

Please sign in to comment.