Exemplo n.º 1
0
def get_tags(store):
    lfm = liblfm.LastFM(CONFIG['lastfm']['key'])
    for track in store.find(Track, Eq(Track.tags, None)):
        print "Processing %s - %s ..." % (track.artist, track.title)
        try:
            track.tags = lfm.top_tags(track.artist, track.title)
        except LookupError as e:
            print "--- %s" % repr(e)
            continue
        except:
            print "WTF ??? %s" % repr(e)
        store.commit()
        time.sleep(1.0)
Exemplo n.º 2
0
def process(artist, title, db_conn):
    res = out_conn.execute(QUERY_TRACK_EXISTS, (artist, title)).fetchone()
    if res is not None:
        # Track is already in database.
        sys.stdout.write("-")
        sys.stdout.flush()
        return
    # Track not in database. We have to fetch the tags.
    time.sleep(1)
    lfm = liblfm.LastFM(CONFIG['lastfm']['key'])
    # Insert the metadata in the database.
    tags = json.dumps(lfm.top_tags(artist, title))
    db_conn.execute(QUERY_INSERT_TRACK, (artist, title, tags))
    db_conn.commit()
    sys.stdout.write(".")
    sys.stdout.flush()
Exemplo n.º 3
0
def process(user, max_pages, conn):
    lfm = liblfm.LastFM(CONFIG['lastfm']['key'])
    id = select_or_insert(user, conn)
    # Get the banned tracks.
    subset, pages = lfm.banned_tracks(user)
    banned = list(subset)
    for i in xrange(2, min(pages, max_pages) + 1):
        subset, pages = lfm.banned_tracks(user, page=i)
        banned.extend(subset)
    insert_tracks('banned', banned, id, conn)
    print " (banned: done)"
    # Get the loved tracks.
    subset, pages = lfm.loved_tracks(user)
    loved = list(subset)
    for i in xrange(2, min(pages, max_pages) + 1):
        subset, pages = lfm.loved_tracks(user, page=i)
        loved.extend(subset)
    insert_tracks('loved', loved, id, conn)
    print " (loved: done)"
Exemplo n.º 4
0
def process(file_path):
    # Get the ID3 tags, fail if not present.
    meta = mutagen.File(file_path, easy=True)
    try:
        artist = meta.get('artist', []).pop(0)
    except IndexError:
        raise LookupError("file has no ID3 'artist' tag")
    try:
        title = meta.get('title', []).pop(0)
    except IndexError:
        raise LookupError("file has no ID3 'title' tag")
    if len(artist) == 0 or len(title) == 0:
        raise LookupError("'title' or 'artist' tag is empty")
    # Call the last.fm API to get the associated tags.
    lfm = liblfm.LastFM(CONFIG['lastfm']['key'])
    return {
      'artist': artist,
      'title': title,
      'tags': lfm.top_tags(artist, title),
    }
Exemplo n.º 5
0
            track.listeners = info['listeners']
            self._store.commit()
            self._logger.info("fetched track info for: %r" % meta)


def _parse_args():
    parser = argparse.ArgumentParser()
    parser.add_argument('--rate', type=float, default=DEFAULT_RATE)
    return parser.parse_args()


def _get_logger():
    formatter = logging.Formatter('%(asctime)s: %(levelname)s - %(message)s')
    handler = logging.StreamHandler()  # Log to stderr.
    handler.setFormatter(formatter)
    logger = logging.getLogger('tagfetcher')
    logger.setLevel(logging.DEBUG)
    logger.addHandler(handler)
    return logger


if __name__ == '__main__':
    args = _parse_args()
    logger = _get_logger()
    for key in [CONFIG['lastfm']['key']] + CONFIG['lastfm']['addkeys']:
        lfm = liblfm.LastFM(key)
        store = uutils.get_store(CONFIG['database']['string'])
        fetcher = Fetcher(args.rate, lfm, store, logger)
        # Launch the fetcher !
        fetcher.start()