示例#1
0
def get_recording_by_id(mbid):
    recording = cache.get(mbid)
    if not recording:
        try:
            recording = musicbrainzngs.get_recording_by_id(mbid, includes=['artists', 'releases', 'media'])['recording']
        except ResponseError as e:
            raise DataUnavailable(e)
    cache.set(mbid, recording, time=CACHE_TIMEOUT)
    return recording
def get_recording_by_id(mbid):
    recording = cache.get(mbid)
    if not recording:
        try:
            recording = musicbrainzngs.get_recording_by_id(
                mbid, includes=['artists', 'releases', 'media'])['recording']
        except ResponseError as e:
            raise DataUnavailable(e)
    cache.set(mbid, recording, time=CACHE_TIMEOUT)
    return recording
示例#3
0
def get_stats():
    stats_keys = ["lowlevel-lossy", "lowlevel-lossy-unique", "lowlevel-lossless", "lowlevel-lossless-unique"]
    # TODO: Port this to new implementation:
    stats = cache._mc.get_multi(stats_keys, key_prefix="ac-num-")
    last_collected = cache.get('last-collected')

    # Recalculate everything together, always.
    if sorted(stats_keys) != sorted(stats.keys()) or last_collected is None:
        stats_parameters = dict([(a, 0) for a in stats_keys])

        with create_cursor() as cursor:
            cursor.execute("SELECT now() as now, collected FROM statistics ORDER BY collected DESC LIMIT 1")
            update_db = False
            if cursor.rowcount > 0:
                (now, last_collected) = cursor.fetchone()
            if cursor.rowcount == 0 or now - last_collected > datetime.timedelta(minutes=59):
                update_db = True

            cursor.execute("SELECT lossless, count(*) FROM lowlevel GROUP BY lossless")
            for row in cursor.fetchall():
                if row[0]: stats_parameters['lowlevel-lossless'] = row[1]
                if not row[0]: stats_parameters['lowlevel-lossy'] = row[1]

            cursor.execute("SELECT lossless, count(*) FROM (SELECT DISTINCT ON (mbid) mbid, lossless FROM lowlevel ORDER BY mbid, lossless DESC) q GROUP BY lossless;")
            for row in cursor.fetchall():
                if row[0]: stats_parameters['lowlevel-lossless-unique'] = row[1]
                if not row[0]: stats_parameters['lowlevel-lossy-unique'] = row[1]

            if update_db:
                for key, value in six.iteritems(stats_parameters):
                    cursor.execute("INSERT INTO statistics (collected, name, value) VALUES (now(), %s, %s) RETURNING collected", (key, value))
                commit()

            cursor.execute("SELECT now()")
            last_collected = cursor.fetchone()[0]

        value = stats_parameters

        # TODO: Port this to new implementation:
        cache._mc.set_multi(stats_parameters, key_prefix="ac-num-", time=STATS_CACHE_TIMEOUT)
        cache.set('last-collected', last_collected, time=STATS_CACHE_TIMEOUT)
    else:
        value = stats

    return value, last_collected
示例#4
0
def get_last_submitted_recordings():
    last_submitted_data = cache.get('last-submitted-data')
    if not last_submitted_data:
        with create_cursor() as cursor:
            cursor.execute("""SELECT mbid,
                                     data->'metadata'->'tags'->'artist'->>0,
                                     data->'metadata'->'tags'->'title'->>0
                                FROM lowlevel
                            ORDER BY id DESC
                               LIMIT 5
                              OFFSET 10""")
            last_submitted_data = cursor.fetchall()
        last_submitted_data = [
            (r[0], r[1], r[2]) for r in last_submitted_data if r[1] and r[2]
        ]
        cache.set('last-submitted-data', last_submitted_data, time=LAST_MBIDS_CACHE_TIMEOUT)

    return last_submitted_data