From cfa0734c0af7788da736c05985f2a3462ee3b1d6 Mon Sep 17 00:00:00 2001 From: Krateng Date: Thu, 27 Jun 2019 11:04:45 +0200 Subject: [PATCH] Enabled caching of weekly #1 --- database.py | 10 +++++++--- utilities.py | 16 ++++++++++++++++ 2 files changed, 23 insertions(+), 3 deletions(-) diff --git a/database.py b/database.py index afbc1e1..6b62651 100644 --- a/database.py +++ b/database.py @@ -50,8 +50,11 @@ TRACKS_LOWER = [] ARTISTS_LOWER = [] ARTIST_SET = set() TRACK_SET = set() + MEDALS = {} #literally only changes once per year, no need to calculate that on the fly MEDALS_TRACKS = {} +WEEKLY_TOPTRACKS = [] +WEEKLY_TOPARTISTS = [] cla = CleanerAgent() coa = CollectorAgent() @@ -541,7 +544,7 @@ def artistInfo(artist): "position":position, "associated":others, "medals":MEDALS.get(artist), - "topweeks":len([p for p in performance if p["rank"] == 1]) + "topweeks":len([a for a in WEEKLY_TOPARTISTS if a == artist]) } except: # if the artist isnt in the charts, they are not being credited and we @@ -574,19 +577,19 @@ def trackInfo(track): c = [e for e in charts if e["track"] == track][0] scrobbles = c["scrobbles"] position = c["rank"] - performance = get_performance(track=track,step="week") cert = None threshold_gold, threshold_platinum, threshold_diamond = settings.get_settings("SCROBBLES_GOLD","SCROBBLES_PLATINUM","SCROBBLES_DIAMOND") if scrobbles >= threshold_diamond: cert = "diamond" elif scrobbles >= threshold_platinum: cert = "platinum" elif scrobbles >= threshold_gold: cert = "gold" + return { "scrobbles":scrobbles, "position":position, "medals":MEDALS_TRACKS.get((frozenset(track["artists"]),track["title"])), "certification":cert, - "topweeks":len([p for p in performance if p["rank"] == 1]) + "topweeks":len([t for t in WEEKLY_TOPTRACKS if t == track]) } @@ -913,6 +916,7 @@ def build_db(): #start regular tasks utilities.update_medals() + utilities.update_weekly() global db_rulestate db_rulestate = utilities.consistentRulestate("scrobbles",cla.checksums) diff --git a/utilities.py b/utilities.py index 2b1b7e5..e0c6c6c 100644 --- a/utilities.py +++ b/utilities.py @@ -468,3 +468,19 @@ def update_medals(): elif t["rank"] == 2: MEDALS_TRACKS.setdefault(track,{}).setdefault("silver",[]).append(year) elif t["rank"] == 3: MEDALS_TRACKS.setdefault(track,{}).setdefault("bronze",[]).append(year) else: break + +@daily +def update_weekly(): + + from database import WEEKLY_TOPTRACKS, WEEKLY_TOPARTISTS, get_top_artists, get_top_tracks + + topartists = get_top_artists(step="week") + toptracks = get_top_tracks(step="week") + + WEEKLY_TOPTRACKS.clear() + WEEKLY_TOPTRACKS += [t["track"] for t in toptracks][:-1] + + WEEKLY_TOPARTISTS.clear() + WEEKLY_TOPARTISTS += [t["artist"] for t in topartists][:-1] + + #print(WEEKLY_TOPTRACKS)