diff --git a/src/main/geocache.py b/src/main/geocache.py index 6c836b5..a121c92 100644 --- a/src/main/geocache.py +++ b/src/main/geocache.py @@ -22,29 +22,35 @@ class Geocache: if os.path.isfile(self.JSON_FILE): with open(self.JSON_FILE, 'r') as rf: self.__geo_cache.update(json.load(rf)) + print(f'Geocache loaded from {self.JSON_FILE}') def __save_geo_cache(self) -> None: with open(self.JSON_FILE, 'w') as wf: json.dump(self.__geo_cache, wf) - print('Geocache saved') + print(f'Geocache saved to {self.JSON_FILE}') def update_geo_cache(self, cities: List[str]) -> None: is_changed: bool = False - for city in cities: - if Utils.is_empty_str(city): - continue - result: () = self.__geo_cache.get(city) - if result is not None: - continue - print(f'{len(self.__geo_cache.keys())}/{len(cities)} - Try to load geocode for {city}') - location: Point = self.__geocode(city) - result: () = (location.latitude, location.longitude) - self.__geo_cache[city] = result - is_changed = True - if len(self.__geo_cache.keys()) % 50 == 0: + try: + for city in cities: + if Utils.is_empty_str(city): + continue + result: () = self.__geo_cache.get(city) + if result is not None: + continue + print(f'{len(self.__geo_cache.keys())} - Try to load geocode for {city}') + location: Point = self.__geocode(city) + if location is None: + self.__geo_cache[city] = '' + else: + result: [] = [location.latitude, location.longitude] + self.__geo_cache[city] = result + is_changed = True + if len(self.__geo_cache.keys()) % 50 == 0: + self.__save_geo_cache() + finally: + if is_changed: self.__save_geo_cache() - if is_changed: - self.__save_geo_cache() def get_location(self, city: str) -> (): return self.__geo_cache.get(city)