Example #1
0
 def _get_cached_artwork(self):
     record = db.AlbumArtRecord.find(artist=util.clean_name(self.artist),
                                     album=util.clean_name(self.album))
     if record is not None:
         return base64.b64decode(record.data)
     else:
         raise ArtNotFoundError(self.album, self.artist)
Example #2
0
def cache_path(path):
    print 'Caching %s...' % path
    done = set()
    for root, dirs, files in os.walk(path):
        for f in [os.path.join(root, x) for x in files]:
            ext = os.path.splitext(f)[1].lower()
            if ext in filetypes:
                try:
                    (artist, album, data) = filetypes[ext](f)
                    if (artist, album) not in done:
                        print u'Adding \'%s\' by \'%s\'...' % (album, artist)
                        db.AlbumArtRecord.add(artist=util.clean_name(artist),
                                              album=util.clean_name(album),
                                              data=base64.b64encode(data))
                        done.add((artist, album))
                except TypeError:
                    print u'No art for \'%s\'!' % f
Example #3
0
def _build_index(repo):
    index = {}
    for name, digest in repo.list_file_names():
        i = 1
        name = util.clean_name(name)
        path = name
        while path in index:
            path = '%s.%d' % (name, i)
            i += 1
        index[path] = digest
    return index
Example #4
0
def get_albumart_url(artist, album):
    url = '%s?%s' % (ALBUMART_ROOT, urllib.urlencode({
        'itempage': 1,
        'newsearch': 1,
        'searchindex': 'Music',
        'srchkey': '%s %s' % (artist, album),
    }))
    logging.info('Fetching from albumart.org: %s' % url)
    req = urllib2.Request(url, headers=HEADERS)
    try:
        data = urllib2.urlopen(req).read()
        images = re.findall(r'title="(.+?)".*src=.*href="(.+?)".*zoom-icon\.jpg', data)
        for (title, url) in images:
            if util.clean_name(title) == util.clean_name(album):
                return url
        # If there's no exact match
        try:
            return images[0][1]
        except IndexError:
            pass
    except urllib2.URLError:
        pass

    return None
Example #5
0
    def parse(cls, raw, instance=None):
        """
        Given a possibly-multiline string representing an SSE message, parse it
        and return a Event object.
        """
        msg = cls()
        for line in raw.split('\n'):
            m = cls.sse_line_pattern.match(line)
            if m is None:
                # Malformed line.  Discard but warn.
                log.warning('Invalid SSE line: "%s"' % line)
                continue

            name = m.groupdict()['name']
            value = m.groupdict()['value']
            if name == '':
                if instance is not None:
                    now = time.time()
                    last_heartbeat = instance._last_heartbeat or instance._connect_start
                    duration = int((now - last_heartbeat) * 1000)
                    request_success.fire(request_type='sse:heartbeat',
                                         name=clean_name('GET', instance.url),
                                         response_time=duration,
                                         response_length=0)
                    instance._last_heartbeat = now
                # line began with a ":", so is a comment.  Ignore
                continue

            if name == 'data':
                # If we already have some data, then join to it with a newline.
                # Else this is it.
                if msg.data:
                    msg.data = '%s\n%s' % (msg.data, value)
                else:
                    msg.data = value
            elif name == 'event':
                msg.event = value
            elif name == 'id':
                msg.id = value
            elif name == 'retry':
                msg.retry = int(value)

        return msg
Example #6
0
 def _cache_artwork(self, image_data):
     logging.info('Caching artwork for \'%s\' by \'%s\'' % (self.album, self.artist))
     db.AlbumArtRecord.add(artist=util.clean_name(self.artist),
                           album=util.clean_name(self.album),
                           data=base64.b64encode(image_data))