def module_trackcharts_tiles(**kwargs): kwargs_filter = pickKeys(kwargs,"artist","associated") kwargs_time = pickKeys(kwargs,"timerange","since","to","within") tracks = database.get_charts_tracks(**kwargs_filter,**kwargs_time)[:14] while len(tracks)<14: tracks.append(None) #{"track":{"title":"","artists":[]}} i = 1 bigpart = [0,1,2,6,15] smallpart = [0,1,2,4,6,9,12,15] #rnk = (0,0) #temporary store so entries with the same scrobble amount get the same rank html = """<table class="tiles_top"><tr>""" for e in tracks: if i in bigpart: n = bigpart.index(i) html += """<td><table class="tiles_""" + str(n) + """x""" + str(n) + """ tiles_sub">""" if i in smallpart: html += "<tr>" if e is not None: html += "<td onclick='window.location.href=\"" \ + link_address(e["track"]) \ + "\"' style='cursor:pointer;background-image:url(\"" + getTrackImage(e["track"]["artists"],e["track"]["title"],fast=True) + "\");'>" \ + "<span class='stats'>" + "#" + str(e["rank"]) + "</span> <span>" + html_link(e["track"]) + "</span></td>" else: html += "<td><span class='stats'></span> <span></span></td>" i += 1 if i in smallpart: html += "</tr>" if i in bigpart: html += "</table></td>" html += """</tr></table>""" return html
def update_medals(): from database import MEDALS, MEDALS_TRACKS, STAMPS, get_charts_artists, get_charts_tracks firstyear = datetime.datetime.utcfromtimestamp(STAMPS[0]).year currentyear = datetime.datetime.utcnow().year MEDALS.clear() for year in range(firstyear, currentyear): charts = get_charts_artists(within=[year]) for a in charts: artist = a["artist"] if a["rank"] == 1: MEDALS.setdefault(artist, {}).setdefault("gold", []).append(year) elif a["rank"] == 2: MEDALS.setdefault(artist, {}).setdefault("silver", []).append(year) elif a["rank"] == 3: MEDALS.setdefault(artist, {}).setdefault("bronze", []).append(year) else: break MEDALS_TRACKS.clear() for year in range(firstyear, currentyear): charts = get_charts_tracks(within=[year]) for t in charts: track = (frozenset(t["track"]["artists"]), t["track"]["title"]) if t["rank"] == 1: MEDALS_TRACKS.setdefault(track, {}).setdefault("gold", []).append(year) 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
def update_weekly(): from database import WEEKLY_TOPTRACKS, WEEKLY_TOPARTISTS, get_charts_artists, get_charts_tracks from malojatime import ranges, thisweek WEEKLY_TOPARTISTS.clear() WEEKLY_TOPTRACKS.clear() for week in ranges(step="week"): if week == thisweek(): break for a in get_charts_artists(timerange=week): artist = a["artist"] if a["rank"] == 1: WEEKLY_TOPARTISTS[artist] = WEEKLY_TOPARTISTS.setdefault( artist, 0) + 1 for t in get_charts_tracks(timerange=week): track = (frozenset(t["track"]["artists"]), t["track"]["title"]) if t["rank"] == 1: WEEKLY_TOPTRACKS[track] = WEEKLY_TOPTRACKS.setdefault( track, 0) + 1
def module_trackcharts(max_=None,**kwargs): kwargs_filter = pickKeys(kwargs,"artist","associated") kwargs_time = pickKeys(kwargs,"timerange","since","to","within") tracks = database.get_charts_tracks(**kwargs_filter,**kwargs_time) # last time range (to compare) try: trackslast = database.get_charts_tracks(**kwargs_filter,timerange=kwargs_time["timerange"].next(step=-1)) # create rank association lastrank = {} for tl in trackslast: lastrank[(*tl["track"]["artists"],tl["track"]["title"])] = tl["rank"] for t in tracks: try: t["delta"] = lastrank[(*t["track"]["artists"],t["track"]["title"])] - t["rank"] except: t["delta"] = math.inf except: pass if tracks != []: maxbar = tracks[0]["scrobbles"] representative = tracks[0]["track"] else: representative = None i = 0 html = "<table class='list'>" for e in tracks: i += 1 if max_ is not None and i>max_: break html += "<tr>" # rank if i == 1 or e["scrobbles"] < prev["scrobbles"]: html += "<td class='rank'>#" + str(i) + "</td>" else: html += "<td class='rank'></td>" # rank change if e.get("delta") is None: pass elif e["delta"] is math.inf: html += "<td class='rankup' title='New'>🆕</td>" elif e["delta"] > 0: html += "<td class='rankup' title='up from #" + str(e["rank"]+e["delta"]) + "'>↗</td>" elif e["delta"] < 0: html += "<td class='rankdown' title='down from #" + str(e["rank"]+e["delta"]) + "'>↘</td>" else: html += "<td class='ranksame' title='Unchanged'>➡</td>" # track html += entity_column(e["track"]) # scrobbles html += "<td class='amount'>" + scrobblesTrackLink(e["track"],internal_to_uri(kwargs_time),amount=e["scrobbles"]) + "</td>" html += "<td class='bar'>" + scrobblesTrackLink(e["track"],internal_to_uri(kwargs_time),percent=e["scrobbles"]*100/maxbar) + "</td>" html += "</tr>" prev = e html += "</table>" return (html,representative)