Exemplo n.º 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
Exemplo n.º 2
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))