RATP: Use cache when API is not reachable

This commit is contained in:
nemunaire 2022-08-14 20:27:54 +02:00
parent 5acdf32ae7
commit d7d9fe8a6d

View File

@ -83,9 +83,12 @@ class IDFMAPI:
if statinfo is None or datetime.fromtimestamp(statinfo.st_mtime, tz=timezone.utc) + timedelta(minutes=self.cache_time) < datetime.now(tz=timezone.utc): if statinfo is None or datetime.fromtimestamp(statinfo.st_mtime, tz=timezone.utc) + timedelta(minutes=self.cache_time) < datetime.now(tz=timezone.utc):
# Do the request and save it # Do the request and save it
req = urllib.request.Request(self.baseurl + "/general-message?LineRef=STIF:Line::" + IDFMAPI.lines[mode][line] + ":", headers={'apikey': self.apikey}) req = urllib.request.Request(self.baseurl + "/general-message?LineRef=STIF:Line::" + IDFMAPI.lines[mode][line] + ":", headers={'apikey': self.apikey})
with urllib.request.urlopen(req) as f: try:
with open(cache_file, 'wb') as fd: with urllib.request.urlopen(req) as f:
fd.write(f.read()) with open(cache_file, 'wb') as fd:
fd.write(f.read())
except ConnectionResetError:
pass
# Retrieve cached data # Retrieve cached data
res = {} res = {}