Skip to content

Commit 5d4612a

Browse files
committed
allow to set caching time for pictures seperately
1 parent a5aff69 commit 5d4612a

File tree

3 files changed

+25
-8
lines changed

3 files changed

+25
-8
lines changed

CHANGELOG.md

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@ All notable changes to this project will be documented in this file.
44
## [Unreleased]
55
- Currently no unreleased changes
66

7+
## [0.19.0] - 2021-08-15
8+
### Added
9+
- Possibility to set caching time for picture downloads seperately
10+
711
## [0.18.3] - 2021-08-14
812
### Fixed
913
- Bug with failing picture download
@@ -217,7 +221,8 @@ Minor fix in observer interface
217221
## [0.1.0] - 2021-05-26
218222
Initial release
219223

220-
[unreleased]: https://github.com/tillsteinbach/WeConnect-python/compare/v0.18.3...HEAD
224+
[unreleased]: https://github.com/tillsteinbach/WeConnect-python/compare/v0.19.0...HEAD
225+
[0.19.0]: https://github.com/tillsteinbach/WeConnect-python/releases/tag/v0.19.0
221226
[0.18.3]: https://github.com/tillsteinbach/WeConnect-python/releases/tag/v0.18.3
222227
[0.18.2]: https://github.com/tillsteinbach/WeConnect-python/releases/tag/v0.18.2
223228
[0.18.1]: https://github.com/tillsteinbach/WeConnect-python/releases/tag/v0.18.1

weconnect/elements/vehicle.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -351,12 +351,12 @@ def updatePictures(self): # noqa: C901
351351
img = None
352352
cacheDate = None
353353
url = image['url']
354-
if self.weConnect.maxAge is not None and self.weConnect.cache is not None and url in self.weConnect.cache:
354+
if self.weConnect.maxAgePictures is not None and self.weConnect.cache is not None and url in self.weConnect.cache:
355355
img, cacheDateString = self.weConnect.cache[url]
356356
img = base64.b64decode(img)
357357
img = Image.open(io.BytesIO(img))
358358
cacheDate = datetime.fromisoformat(cacheDateString)
359-
if img is None or self.weConnect.maxAge is None \
359+
if img is None or self.weConnect.maxAgePictures is None \
360360
or (cacheDate is not None and cacheDate < (datetime.utcnow() - timedelta(days=1))):
361361
imageDownloadResponse = self.weConnect.session.get(url, stream=True)
362362
if imageDownloadResponse.status_code == requests.codes['ok']:
@@ -382,7 +382,8 @@ def updatePictures(self): # noqa: C901
382382
f' Status Code was: {imageDownloadResponse.status_code}')
383383
raise RetrievalError(f'Could not retrieve data. Status Code was: {imageDownloadResponse.status_code}')
384384
else:
385-
LOG.warning('Failed downloading picture %s with status code %d', image['id'], imageDownloadResponse.status_code)
385+
LOG.warning('Failed downloading picture %s with status code %d will try again in next update', image['id'],
386+
imageDownloadResponse.status_code)
386387

387388
if img is not None:
388389
self.__carImages[image['id']] = img

weconnect/weconnect.py

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ def __init__( # noqa: C901 # pylint: disable=too-many-arguments
6868
refreshTokens=True,
6969
fixAPI=True,
7070
maxAge=None,
71+
maxAgePictures=None,
7172
updateCapabilities=True,
7273
updatePictures=True
7374
):
@@ -85,6 +86,7 @@ def __init__( # noqa: C901 # pylint: disable=too-many-arguments
8586
self.__cache = dict()
8687
self.fixAPI = fixAPI
8788
self.maxAge = maxAge
89+
self.maxAgePictures = maxAgePictures
8890
self.latitude = None
8991
self.longitude = None
9092
self.searchRadius = None
@@ -164,19 +166,28 @@ def persistCacheAsJson(self, filename):
164166
json.dump(self.__cache, file, cls=DateTimeEncoder)
165167
LOG.info('Writing cachefile %s', filename)
166168

167-
def fillCacheFromJson(self, filename, maxAge):
169+
def fillCacheFromJson(self, filename, maxAge, maxAgePictures=None):
168170
self.maxAge = maxAge
171+
if maxAgePictures is None:
172+
self.maxAgePictures = maxAge
173+
else:
174+
self.maxAgePictures = maxAgePictures
175+
169176
with open(filename, 'r') as file:
170177
self.__cache = json.load(file)
171178
LOG.info('Reading cachefile %s', filename)
172179

173-
def fillCacheFromJsonString(self, jsonString, maxAge):
180+
def fillCacheFromJsonString(self, jsonString, maxAge, maxAgePictures=None):
174181
self.maxAge = maxAge
182+
if maxAgePictures is None:
183+
self.maxAgePictures = maxAge
184+
else:
185+
self.maxAgePictures = maxAgePictures
186+
175187
self.__cache = json.loads(jsonString)
176188
LOG.info('Reading cache from string')
177189

178-
def clearCache(self, maxAge):
179-
self.maxAge = maxAge
190+
def clearCache(self):
180191
self.__cache.clear()
181192
LOG.info('Clearing cache')
182193

0 commit comments

Comments
 (0)