示例#1
0
文件: base.py 项目: mrworf/photoframe
    def fetchImage(self, image, destinationDir, supportedMimeTypes,
                   displaySize):
        filename = os.path.join(destinationDir, self.generateFilename())

        if image.cacheAllow:
            # Look it up in the cache mgr
            if self._CACHEMGR is None:
                logging.error('CacheManager is not available')
            else:
                cacheFile = self._CACHEMGR.getCachedImage(
                    image.getCacheId(), filename)
                if cacheFile:
                    image.setFilename(cacheFile)
                    image.cacheUsed = True

        if not image.cacheUsed:
            recommendedSize = self.calcRecommendedSize(image.dimensions,
                                                       displaySize)
            if recommendedSize is None:
                recommendedSize = displaySize
            url = self.getContentUrl(image, {
                'size': recommendedSize,
                'display': displaySize
            })
            if url is None:
                return ImageHolder().setError(
                    'Unable to download image, no URL')

            try:
                result = self.requestUrl(url, destination=filename)
            except (RequestResult.RequestExpiredToken, RequestInvalidToken):
                logging.exception('Cannot fetch due to token issues')
                result = RequestResult().setResult(RequestResult.OAUTH_INVALID)
                self._OAUTH = None
            except requests.exceptions.RequestException:
                logging.exception('request to download image failed')
                result = RequestResult().setResult(RequestResult.NO_NETWORK)

            if not result.isSuccess():
                return ImageHolder().setError('%d: Unable to download image!' %
                                              result.httpcode)
            else:
                image.setFilename(filename)
        if image.filename is not None:
            image.setMimetype(helper.getMimetype(image.filename))
        return image
示例#2
0
    def selectImageFromAlbum(self, destinationDir, supportedMimeTypes,
                             displaySize, randomize):
        # chooses an album and selects an image from that album --> return {'id':, 'mimetype':, 'error':, 'source':}
        # if no (new) images can be found --> return None

        keywordList = list(self.getKeywords())
        keywordCount = len(keywordList)
        if keywordCount == 0:
            return ImageHolder().setError('No albums have been specified')

        if randomize:
            index = self.getRandomKeywordIndex()
        else:
            index = self.keywordIndex

        # if current keywordList[index] does not contain any new images --> just run through all albums
        for i in range(0, keywordCount):
            if not randomize and (index + i) >= keywordCount:
                # (non-random image order): return if the last album is exceeded --> serviceManager should use next service
                break
            self.keywordIndex = (index + i) % keywordCount
            keyword = keywordList[self.keywordIndex]

            # a provider-specific implementation for 'getImagesFor' is obligatory!
            images = self.getImagesFor(keyword)
            if images is None:
                logging.warning(
                    'Function returned None, this is used sometimes when a temporary error happens. Still logged'
                )
                self.imageIndex = 0
                continue
            if len(images) > 0 and images[0].error is not None:
                return images[0]
            self._STATE["_NUM_IMAGES"][keyword] = len(images)
            if len(images) == 0:
                self.imageIndex = 0
                continue
            self.saveState()

            image = self.selectImage(images, supportedMimeTypes, displaySize,
                                     randomize)
            if image is None:
                self.imageIndex = 0
                continue

            filename = os.path.join(destinationDir, image.id)

            # you should implement 'addUrlParams' if the provider allows 'width' / 'height' parameters!
            recommendedSize = self.calcRecommendedSize(image.dimensions,
                                                       displaySize)
            url = self.addUrlParams(image.url, recommendedSize, displaySize)

            if image.cacheAllow:
                # Look it up in the cache mgr
                if self._CACHEMGR is None:
                    logging.error('CacheManager is not available')
                else:
                    cacheFile = self._CACHEMGR.getCachedImage(
                        image.getCacheId(), filename)
                    if cacheFile:
                        image.setFilename(cacheFile)
                        image.cacheUsed = True

            if not image.cacheUsed:
                try:
                    result = self.requestUrl(url, destination=filename)
                except requests.exceptions.RequestException:
                    logging.exception('request to download image failed')
                    result = RequestResult().setResult(
                        RequestResult.NO_NETWORK)

                if not result.isSuccess():
                    return ImageHolder().setError(
                        '%d: Unable to download image!' % result.httpcode)
                else:
                    image.setFilename(filename)
            image.setMimetype(helper.getMimetype(image.filename))
            return image

        self.resetIndices()
        return None