Skip to content

Commit

Permalink
Handle empty albums (#143)
Browse files Browse the repository at this point in the history
Soemtimes an album may show up as empty and due to caching it will
never be queried again. This change forces a refresh every X minute
even if it was empty before

Fix #140
  • Loading branch information
mrworf authored Nov 16, 2019
1 parent 619df41 commit 760b875
Showing 1 changed file with 9 additions and 3 deletions.
12 changes: 9 additions & 3 deletions services/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
# Use the exposed functions as needed to get the data you want.
#
class BaseService:
REFRESH_DELAY = 60*60 # Number of seconds before we refresh the index in case no photos
SERVICE_DEPRECATED = False

STATE_ERROR = -1
Expand All @@ -64,12 +65,15 @@ def __init__(self, configDir, id, name, needConfig=False, needOAuth=False):
# NUM_IMAGES keeps track of how many images are being provided by each keyword
# As for now, unsupported images (mimetype, orientation) and already displayed images are NOT excluded due to simplicity,
# but it should still serve as a rough estimate to ensure that every image has a similar chance of being shown in "random_image_mode"!
# NEXT_SCAN is used to determine when a keyword should be re-indexed. This used in the case number of photos are zero to avoid hammering
# services.
self._STATE = {
'_OAUTH_CONFIG' : None,
'_OAUTH_CONTEXT' : None,
'_CONFIG' : None,
'_KEYWORDS' : [],
'_NUM_IMAGES' : {},
'_NEXT_SCAN' : {},
'_EXTRAS' : None
}
self._NEED_CONFIG = needConfig
Expand Down Expand Up @@ -181,8 +185,9 @@ def getNumImages(self, excludeUnsuported=True):
# return the total number of images provided by this service
if self.needKeywords():
for keyword in self.getKeywords():
if keyword not in self._STATE["_NUM_IMAGES"]:
if keyword not in self._STATE["_NUM_IMAGES"] or keyword not in self._STATE['_NEXT_SCAN'] or self._STATE['_NEXT_SCAN'][keyword] < time.time():
images = self.getImagesFor(keyword)
self._STATE['_NEXT_SCAN'][keyword] = time.time() + self.REFRESH_DELAY
if images is not None:
self._STATE["_NUM_IMAGES"][keyword] = len(images)
return sum([self._STATE["_NUM_IMAGES"][k] for k in self._STATE["_NUM_IMAGES"]])
Expand All @@ -192,7 +197,7 @@ def getMessages(self):
# the provider's instance. Return None to hide
# Format: [{'level' : 'INFO', 'message' : None, 'link' : None}]
msgs = []
if self._CURRENT_STATE in [self.STATE_NEED_KEYWORDS, self.STATE_NO_IMAGES]:
if self._CURRENT_STATE in [self.STATE_NEED_KEYWORDS]: # , self.STATE_NO_IMAGES]:
msgs.append(
{
'level': 'INFO',
Expand All @@ -209,7 +214,7 @@ def getMessages(self):
msgs.append(
{
'level': 'WARNING',
'message': 'At least one keyword does not appear to provide any images! Please remove keyword(s): %s' % ', '.join(removeme),
'message': 'The following keyword(s) do not yield any photos: %s' % ', '.join(map(u'"{0}"'.format, removeme)),
'link': None
}
)
Expand Down Expand Up @@ -483,6 +488,7 @@ def selectImageFromAlbum(self, destinationDir, supportedMimeTypes, displaySize,
if len(images) > 0 and images[0].error is not None:
return images[0]
self._STATE["_NUM_IMAGES"][keyword] = len(images)
self._STATE['_NEXT_SCAN'][keyword] = time.time() + self.REFRESH_DELAY
if len(images) == 0:
self.imageIndex = 0
continue
Expand Down

0 comments on commit 760b875

Please sign in to comment.