示例#1
0
 def test_coverart_uuid(self):
     info_l = [
         {'artist': 'Antony & the Johnsons', 'album': 'The crying light'},
         {'artist': 'Antony and the Johnsons', 'album': 'The crying light'},
     ]
     for info in info_l:
         ret = MediaManager.cover_art_uuid(info)
         print "coverid:", ret
 def test_coverart_uuid(self):
     info_l = [
         {
             'artist': 'Antony & the Johnsons',
             'album': 'The crying light'
         },
         {
             'artist': 'Antony and the Johnsons',
             'album': 'The crying light'
         },
     ]
     for info in info_l:
         ret = MediaManager.cover_art_uuid(info)
         print "coverid:", ret
示例#3
0
def cover_art_worker(cache_dir, cover_search=cover_search):
    log.info("starting downloader thread with tmp_dir: %s" % cache_dir)
    info = True
    while info:
        info = q.get()
        try:
            cover_art_path = os.path.join("/", cache_dir,
                                          MediaManager.cover_art_uuid(info))
            log.info("coverart %s: searching album: %s " %
                     (info.get('id'), info.get('album')))
            covers = cover_search(info.get('album'))
            for cover in covers:
                # TODO consider multiple authors in info
                #  ex. Actually "U2 & Frank Sinatra" != "U2"
                #      leads to a false negative
                # TODO con
                print "confronting info: %s with: %s" % (info, cover)
                normalize_info, normalize_cover = map(
                    MediaManager.normalize_artist, [info, cover])
                full_match = len(set([normalize_info, normalize_cover])) == 1
                stopwords_match = len(
                    set([
                        MediaManager.normalize_artist(x, stopwords=True)
                        for x in [info, cover]
                    ])) == 1

                partial_match = len([
                    x for x in normalize_info if x not in normalize_cover
                ]) == 0
                if full_match or stopwords_match or partial_match:
                    log.warn("Saving image %s -> %s" %
                             (cover.get('cover_small'), cover_art_path))
                    fd = open(cover_art_path, "w")
                    fd.write(urlopen(cover.get('cover_small')).read())
                    fd.close()

                else:
                    log.info("Artist mismatch: %s, %s" % tuple([
                        x.get('artist', x.get('name')) for x in [info, cover]
                    ]))
        except Exception as e:
            log.error("Error while downloading albumart.", e)

        q.task_done()
    log.warn("finish download thread")
示例#4
0
def cover_art_worker(cache_dir, cover_search=cover_search):
    log.info("starting downloader thread with tmp_dir: %s" % cache_dir)
    info = True
    while info:
        info = q.get()
        try:
            cover_art_path = os.path.join("/",
                                          cache_dir,
                                          MediaManager.cover_art_uuid(info)
                                          )
            log.info("coverart %s: searching album: %s " % (
                info.get('id'), info.get('album')))
            covers = cover_search(info.get('album'))
            for cover in covers:
                # TODO consider multiple authors in info
                #  ex. Actually "U2 & Frank Sinatra" != "U2"
                #      leads to a false negative
                # TODO con
                print "confronting info: %s with: %s" % (info, cover)
                normalize_info, normalize_cover = map(
                    MediaManager.normalize_artist, [info, cover])
                full_match = len(set([normalize_info, normalize_cover])) == 1
                stopwords_match = len(set([MediaManager.normalize_artist(
                    x, stopwords=True) for x in [info, cover]])) == 1

                partial_match = len(
                    [x for x in normalize_info if x not in normalize_cover]) == 0
                if full_match or stopwords_match or partial_match:
                    log.warn("Saving image %s -> %s" % (
                        cover.get('cover_small'), cover_art_path)
                    )
                    fd = open(cover_art_path, "w")
                    fd.write(urlopen(cover.get('cover_small')).read())
                    fd.close()

                else:
                    log.info("Artist mismatch: %s, %s" % tuple(
                        [x.get('artist', x.get('name')) for x in [info, cover]])
                    )
        except Exception as e:
            log.error("Error while downloading albumart.", e)

        q.task_done()
    log.warn("finish download thread")
示例#5
0
 def add_path(self, path, album=False):
     """Create an entry from path and add it to the DB."""
     if os.path.isdir(path):
         self.log.warn(
             "Adding %s: %s " % ("album" if album else "artist", stringutils.to_unicode(path)))
         eid = MediaManager.uuid(path)
         if album:
             self.albums[eid] = IposonicDB.Album(path)
         else:
             self.artists[eid] = IposonicDB.Artist(path)
         self.log.info(u"adding directory: %s, %s " % (eid, stringutils.to_unicode(path)))
         return eid
     elif MediaManager.is_allowed_extension(path):
         try:
             info = MediaManager.get_info(path)
             info.update({
                 'coverArt': MediaManager.cover_art_uuid(info)
             })
             self.songs[info['id']] = info
             self.log.info("adding file: %s, %s " % (info['id'], path))
             return info['id']
         except UnsupportedMediaError as e:
             raise IposonicException(e)
     raise IposonicException("Path not found or bad extension: %s " % path)
示例#6
0
 def add_path(self, path, album=False):
     """Create an entry from path and add it to the DB."""
     if os.path.isdir(path):
         self.log.warn(
             "Adding %s: %s " %
             ("album" if album else "artist", stringutils.to_unicode(path)))
         eid = MediaManager.uuid(path)
         if album:
             self.albums[eid] = IposonicDB.Album(path)
         else:
             self.artists[eid] = IposonicDB.Artist(path)
         self.log.info(u"adding directory: %s, %s " %
                       (eid, stringutils.to_unicode(path)))
         return eid
     elif MediaManager.is_allowed_extension(path):
         try:
             info = MediaManager.get_info(path)
             info.update({'coverArt': MediaManager.cover_art_uuid(info)})
             self.songs[info['id']] = info
             self.log.info("adding file: %s, %s " % (info['id'], path))
             return info['id']
         except UnsupportedMediaError as e:
             raise IposonicException(e)
     raise IposonicException("Path not found or bad extension: %s " % path)
示例#7
0
def get_cover_art_file(eid, nocache=False):
    from mediamanager.cover_art import q

    """Return coverArt file, eventually downloading it.

        Successful download requires both Artist and Album, so
        store the filename as uuid(Artist/Album)
        1- if parent directory...
        2- if song, download as Artist/Album
        3- if album, download as Artist/Album
        3- if albumId...

        We should manage some border cases:
        a- featured artists
            ex. album: Antonella Ruggiero
                song: Antonella Ruggiero & Subsonica
    """

    # if everything is fine, just get the file
    cover_art_path = os.path.join("/", app.iposonic.cache_dir, "%s" % eid)
    if os.path.exists(cover_art_path):
        return cover_art_path

    # otherwise we need to guess from item info,
    # and hit the database
    info = app.iposonic.get_entry_by_id(eid)
    log.info("search cover_art requires media info from db: %s" % info)

    # search cover_art using id3 tag
    if not info.get('artist') or not info.get('album'):
        return None

    # if we're a CD collection, use parent
    #   TODO add other patterns to "cd" eg. "disk", "volume"
    if info.get('isDir') and info.get('album').lower().startswith("cd"):
        info = app.iposonic.get_entry_by_id(info.get('parent'))
        log.info("album is a cd, getting parent info: %s" % info)

    cover_art_path = os.path.join(
        "/", app.iposonic.cache_dir, MediaManager.cover_art_uuid(info))
    log.info("checking cover_art_uuid: %s" % cover_art_path)
    if os.path.exists(cover_art_path):
        return cover_art_path

    # if we're a file, let's use parent or albumId
    if info.get('isDir') in [False, 'false', 'False']:
        cover_art_path = join(
            "/", app.iposonic.cache_dir, "%s" % info.get('parent'))
        if os.path.exists(cover_art_path):
            log.info("successfully get cover_art from parent directory")
            return cover_art_path

    cover_art_path = join("/", app.iposonic.cache_dir,
                          MediaManager.cover_art_uuid(info))
    if os.path.exists(cover_art_path):
        return cover_art_path

    # if it's not present
    # use the background thread to download
    # and return 503
    q.put(info)
    abort(503)