diff --git a/maloja/database/__init__.py b/maloja/database/__init__.py index ba37228..2bf9c4f 100644 --- a/maloja/database/__init__.py +++ b/maloja/database/__init__.py @@ -228,6 +228,7 @@ def get_top_tracks(**keys): @waitfordb def artist_info(artist): + artist = sqldb.get_artist(sqldb.get_artist_id(artist)) alltimecharts = get_charts_artists(timerange=alltime()) scrobbles = get_scrobbles_num(artist=artist,timerange=alltime()) #we cant take the scrobble number from the charts because that includes all countas scrobbles @@ -235,12 +236,19 @@ def artist_info(artist): c = [e for e in alltimecharts if e["artist"] == artist][0] others = sqldb.get_associated_artists(artist) position = c["rank"] - performance = get_performance(artist=artist,step="week") + performance_weekly = get_performance(artist=artist,step="week")[:-1] #current week doesn't count + performance_yearly = get_performance(artist=artist,step="year")[:-1] #current year doesn't count return { "artist":artist, "scrobbles":scrobbles, "position":position, - "associated":others + "associated":others, + "medals":{ + "gold":[e['range'] for e in performance_yearly if e['rank'] == 1], + "silver":[e['range'] for e in performance_yearly if e['rank'] == 2], + "bronze":[e['range'] for e in performance_yearly if e['rank'] == 3] + }, + "topweeks":len([e for e in performance_weekly if e['rank'] == 1]) } except: # if the artist isnt in the charts, they are not being credited and we @@ -254,12 +262,13 @@ def artist_info(artist): - def track_info(track): - charts = db_aggregate(by="TRACK") - #scrobbles = len(db_query(artists=artists,title=title)) #chart entry of track always has right scrobble number, no countas rules here - #c = [e for e in charts if set(e["track"]["artists"]) == set(artists) and e["track"]["title"] == title][0] - c = [e for e in charts if e["track"] == track][0] + + track = sqldb.get_track(sqldb.get_track_id(track)) + alltimecharts = get_charts_tracks(timerange=alltime()) + #scrobbles = get_scrobbles_num(track=track,timerange=alltime()) + + c = [e for e in alltimecharts if e["track"] == track][0] scrobbles = c["scrobbles"] position = c["rank"] cert = None @@ -268,17 +277,56 @@ def track_info(track): elif scrobbles >= threshold_platinum: cert = "platinum" elif scrobbles >= threshold_gold: cert = "gold" + performance_weekly = get_performance(track=track,step="week")[:-1] #current week doesn't count + performance_yearly = get_performance(track=track,step="year")[:-1] #current year doesn't count + return { "track":track, "scrobbles":scrobbles, "position":position, - "medals":{"gold":[],"silver":[],"bronze":[],**MEDALS_TRACKS.get((frozenset(track["artists"]),track["title"]),{})}, + "medals":{ + "gold":[e['range'] for e in performance_yearly if e['rank'] == 1], + "silver":[e['range'] for e in performance_yearly if e['rank'] == 2], + "bronze":[e['range'] for e in performance_yearly if e['rank'] == 3] + }, "certification":cert, - "topweeks":WEEKLY_TOPTRACKS.get(((frozenset(track["artists"]),track["title"])),0) + "topweeks":len([e for e in performance_weekly if e['rank'] == 1]) } +def tracks_info(tracks): + tracks = [sqldb.get_track(sqldb.get_track_id(track)) for track in tracks] + alltimecharts = get_charts_tracks(timerange=alltime()) + + result = [] + for track in tracks: + c = [e for e in alltimecharts if e["track"] == track][0] + scrobbles = c["scrobbles"] + position = c["rank"] + cert = None + threshold_gold, threshold_platinum, threshold_diamond = malojaconfig["SCROBBLES_GOLD","SCROBBLES_PLATINUM","SCROBBLES_DIAMOND"] + if scrobbles >= threshold_diamond: cert = "diamond" + elif scrobbles >= threshold_platinum: cert = "platinum" + elif scrobbles >= threshold_gold: cert = "gold" + + performance_weekly = get_performance(track=track,step="week")[:-1] #current week doesn't count + performance_yearly = get_performance(track=track,step="year")[:-1] #current year doesn't count + + result.append({ + "track":track, + "scrobbles":scrobbles, + "position":position, + "medals":{ + "gold":[e['range'] for e in performance_yearly if e['rank'] == 1], + "silver":[e['range'] for e in performance_yearly if e['rank'] == 2], + "bronze":[e['range'] for e in performance_yearly if e['rank'] == 3] + }, + "certification":cert, + "topweeks":len([e for e in performance_weekly if e['rank'] == 1]) + }) + + return result def compare(remoteurl): diff --git a/maloja/database/sqldb.py b/maloja/database/sqldb.py index f92dc9f..5a18c4e 100644 --- a/maloja/database/sqldb.py +++ b/maloja/database/sqldb.py @@ -88,7 +88,6 @@ meta.create_all(engine) ### DB -> DICT - def scrobbles_db_to_dict(rows): tracks = get_tracks_map(set(row.track_id for row in rows)) return [ @@ -100,10 +99,10 @@ def scrobbles_db_to_dict(rows): } for row in rows ] + def scrobble_db_to_dict(row): return scrobbles_db_to_dict([row])[0] - def tracks_db_to_dict(rows): artists = get_artists_of_tracks(set(row.id for row in rows)) return [ @@ -114,15 +113,16 @@ def tracks_db_to_dict(rows): } for row in rows ] + def track_db_to_dict(row): return tracks_db_to_dict([row])[0] - def artists_db_to_dict(rows): return [ row.name for row in rows ] + def artist_db_to_dict(row): return artists_db_to_dict([row])[0] @@ -131,6 +131,7 @@ def artist_db_to_dict(row): ### DICT -> DB # TODO + def scrobble_dict_to_db(info): return { "rawscrobble":json.dumps(info), @@ -182,7 +183,6 @@ def add_scrobbles(scrobbleslist): ### these will 'get' the ID of an entity, creating it if necessary - def get_track_id(trackdict): ntitle = normalize_name(trackdict['title']) artist_ids = [get_artist_id(a) for a in trackdict['artists']] @@ -260,7 +260,6 @@ def get_artist_id(artistname): ### Functions that get rows according to parameters - def get_scrobbles_of_artist(artist,since=None,to=None): if since is None: since=0 @@ -281,7 +280,6 @@ def get_scrobbles_of_artist(artist,since=None,to=None): #result = [scrobble_db_to_dict(row,resolve_references=resolve_references) for row in result] return result - def get_scrobbles_of_track(track,since=None,to=None): if since is None: since=0 @@ -301,7 +299,6 @@ def get_scrobbles_of_track(track,since=None,to=None): #result = [scrobble_db_to_dict(row) for row in result] return result - def get_scrobbles(since=None,to=None,resolve_references=True): if since is None: since=0 @@ -318,7 +315,6 @@ def get_scrobbles(since=None,to=None,resolve_references=True): #result = [scrobble_db_to_dict(row,resolve_references=resolve_references) for i,row in enumerate(result) if i -{% for track in db.get_tracks(artist=artist) -%} - {% set info = db.track_info(track) %} - {% if info.certification is not none -%} - + +{% set charts = db.get_charts_tracks(artist=artist,timerange=malojatime.alltime()) %} +{% for e in charts -%} + {%- if e.scrobbles >= settings.scrobbles_gold -%}{% set cert = 'gold' %}{%- endif -%} + {%- if e.scrobbles >= settings.scrobbles_platinum -%}{% set cert = 'platinum' %}{%- endif -%} + {%- if e.scrobbles >= settings.scrobbles_diamond -%}{% set cert = 'diamond' %}{%- endif -%} + + {%- if cert -%} + {%- endif %} + {%- endfor %} {%- endmacro %}