Beispiel #1
0
def get_podcast_logo(podcast, feed):
    cover_art = podcast.logo_url
    image = feed.feed.get('image', None)
    if image is not None:
        for key in ('href', 'url'):
            cover_art = getattr(image, key, None)
            if cover_art:
                break

    if podcast.link:
        yturl = youtube.get_real_cover(podcast.link)
        if yturl:
            cover_art = yturl

    if cover_art:
        try:
            image_sha1 = hashlib.sha1(cover_art).hexdigest()
            prefix = CoverArt.get_prefix(image_sha1)

            filename = CoverArt.get_original(prefix, image_sha1)
            dirname = CoverArt.get_dir(filename)

            # get hash of existing file
            if os.path.exists(filename):
                with open(filename) as f:
                    old_hash = file_hash(f).digest()
            else:
                old_hash = ''

            print 'LOGO @', cover_art

            # save new cover art
            with open(filename, 'w') as fp:
                fp.write(urllib2.urlopen(cover_art).read())

            # get hash of new file
            with open(filename) as f:
                new_hash = file_hash(f).digest()

            # remove thumbnails if cover changed
            if old_hash != new_hash:
                thumbnails = CoverArt.get_existing_thumbnails(prefix, filename)
                print 'Removing %d thumbnails' % len(thumbnails)
                for f in thumbnails:
                    os.unlink(f)

            return  cover_art

        except Exception, e:
            if str(e).strip():
                try:
                    print >> sys.stderr, \
                        unicode('cannot save image for podcast %s: %s'
                        % (podcast.get_id(), str(e)), errors='ignore')
                except:
                    print >> sys.stderr, 'cannot save podcast logo'

            return None
Beispiel #2
0
    def save_podcast_logo(cls, cover_art_url):
        if not cover_art_url:
            return

        try:
            image_sha1 = hashlib.sha1(
                cover_art_url.encode('utf-8')).hexdigest()
            prefix = get_prefix(image_sha1)

            filename = cls.get_original_path(prefix, image_sha1)
            dirname = cls.get_dir(filename)

            # get hash of existing file
            if LOGO_STORAGE.exists(filename):
                with LOGO_STORAGE.open(filename, 'rb') as f:
                    old_hash = file_hash(f).digest()
            else:
                old_hash = ''

            logger.info('Logo {}, saving to {}'.format(cover_art_url,
                                                       filename))

            # save new cover art
            LOGO_STORAGE.delete(filename)
            source = io.BytesIO(requests.get(cover_art_url).content)
            LOGO_STORAGE.save(filename, source)

            # get hash of new file
            with LOGO_STORAGE.open(filename, 'rb') as f:
                new_hash = file_hash(f).digest()

            # remove thumbnails if cover changed
            if old_hash != new_hash:
                logger.info('Removing thumbnails')
                thumbnails = cls.remove_existing_thumbnails(prefix, filename)

            return cover_art_url

        except (
                ValueError,
                requests.exceptions.RequestException,
                socket.error,
                IOError,
        ) as e:
            logger.warning('Exception while updating podcast logo: %s', str(e))
Beispiel #3
0
    def save_podcast_logo(cls, cover_art_url):
        if not cover_art_url:
            return

        try:
            image_sha1 = hashlib.sha1(cover_art_url.encode('utf-8')).hexdigest()
            prefix = get_prefix(image_sha1)

            filename = cls.get_original_path(prefix, image_sha1)
            dirname = cls.get_dir(filename)

            # get hash of existing file
            if LOGO_STORAGE.exists(filename):
                with LOGO_STORAGE.open(filename, 'rb') as f:
                    old_hash = file_hash(f).digest()
            else:
                old_hash = ''

            logger.info('Logo {}, saving to {}'.format(cover_art_url, filename))

            # save new cover art
            LOGO_STORAGE.delete(filename)
            source = io.BytesIO(requests.get(cover_art_url).content)
            LOGO_STORAGE.save(filename, source)

            # get hash of new file
            with LOGO_STORAGE.open(filename, 'rb') as f:
                new_hash = file_hash(f).digest()

            # remove thumbnails if cover changed
            if old_hash != new_hash:
                logger.info('Removing thumbnails')
                thumbnails = cls.remove_existing_thumbnails(prefix, filename)

            return cover_art_url

        except (
            ValueError,
            requests.exceptions.RequestException,
            socket.error,
            IOError,
        ) as e:
            logger.warning('Exception while updating podcast logo: %s', str(e))
Beispiel #4
0
def _save_podcast_logo(cover_art):
    if not cover_art:
        return

    try:
        image_sha1 = hashlib.sha1(cover_art.encode('utf-8')).hexdigest()
        prefix = CoverArt.get_prefix(image_sha1)

        filename = CoverArt.get_original(prefix, image_sha1)
        dirname = CoverArt.get_dir(filename)

        # get hash of existing file
        if os.path.exists(filename):
            with open(filename) as f:
                old_hash = file_hash(f).digest()
        else:
            old_hash = ''

        logger.info('Logo %s', cover_art)

        # save new cover art
        with open(filename, 'wb') as fp:
            fp.write(urllib.request.urlopen(cover_art).read())

        # get hash of new file
        with open(filename) as f:
            new_hash = file_hash(f).digest()

        # remove thumbnails if cover changed
        if old_hash != new_hash:
            thumbnails = CoverArt.get_existing_thumbnails(prefix, filename)
            logger.info('Removing %d thumbnails', len(thumbnails))
            for f in thumbnails:
                os.unlink(f)

        return cover_art

    except (urllib.error.HTTPError, urllib.error.URLError, ValueError,
            http.client.BadStatusLine, socket.error, IOError) as e:
        logger.warn('Exception while updating podcast logo: %s', str(e))