From ca90adc842fb0f26027a69a58bcd0346e2ee822e Mon Sep 17 00:00:00 2001 From: Krateng Date: Sun, 3 Mar 2019 21:55:35 +0100 Subject: [PATCH] Now correctly showing same rank for equally-ranked entries --- htmlmodules.py | 32 +++++++++++++++++++++++++------- utilities.py | 22 ++++++++++++++++------ website/start.py | 4 ++-- 3 files changed, 43 insertions(+), 15 deletions(-) diff --git a/htmlmodules.py b/htmlmodules.py index 90076e3..f914288 100644 --- a/htmlmodules.py +++ b/htmlmodules.py @@ -163,7 +163,7 @@ def module_artistcharts_tiles(**kwargs) : 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 = """""" @@ -177,9 +177,18 @@ def module_artistcharts_tiles(**kwargs) : if i in smallpart: html += "" - rank = "#" + str(i) if e is not None else "" - image = "/image?artist=" + urllib.parse.quote(e["artist"]) if e is not None else "" - link = artistLink(e["artist"]) if e is not None else "" + + if e is not None: + rank = i if e["scrobbles"] != rnk[1] else rnk[0] + rnk = (rank,e["scrobbles"]) + rank = "#" + str(rank) + image = "/image?artist=" + urllib.parse.quote(e["artist"]) + link = artistLink(e["artist"]) + else: + rank = "" + image = "" + link = "" + html += """" @@ -208,6 +217,7 @@ def module_trackcharts_tiles(**kwargs) : 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 = """
""" + rank + " " + link + "
""" @@ -222,9 +232,17 @@ def module_trackcharts_tiles(**kwargs) : if i in smallpart: html += "" - rank = "#" + str(i) if e is not None else "" - image = "/image?title=" + urllib.parse.quote(e["track"]["title"]) + "&" + "&".join(["artist=" + urllib.parse.quote(a) for a in e["track"]["artists"]]) if e is not None else "" - link = trackLink(e["track"]) if e is not None else "" + + if e is not None: + rank = i if e["scrobbles"] != rnk[1] else rnk[0] + rnk = (rank,e["scrobbles"]) + rank = "#" + str(rank) + image = "/image?title=" + urllib.parse.quote(e["track"]["title"]) + "&" + "&".join(["artist=" + urllib.parse.quote(a) for a in e["track"]["artists"]]) + link = trackLink(e["track"]) + else: + rank = "" + image = "" + link = "" html += """" diff --git a/utilities.py b/utilities.py index ebec4f1..7e0042e 100644 --- a/utilities.py +++ b/utilities.py @@ -3,6 +3,7 @@ import os import hashlib from threading import Thread import pickle +import urllib ### TSV files @@ -257,7 +258,7 @@ def loadCache(): finally: fl.close() -def getTrackInfo(artists,title): +def getTrackInfo(artists,title,fast=False): obj = (frozenset(artists),title) filename = "-".join([re.sub("[^a-zA-Z0-9]","",artist) for artist in artists]) + "_" + re.sub("[^a-zA-Z0-9]","",title) @@ -279,6 +280,10 @@ def getTrackInfo(artists,title): return {"image":cachedTracks[(frozenset(artists),title)]} except: pass + + # fast request only retuns cached and local results, generates redirect link for rest + if fast: + return "/image?title=" + urllib.parse.quote(title) + "&" + "&".join(["artist=" + urllib.parse.quote(a) for a in artists]) result = apirequest(artists=artists,title=title) if result.get("image") is not None: @@ -289,7 +294,7 @@ def getTrackInfo(artists,title): #cachedTracks[(frozenset(artists),title)] = result["image"] return result -def getArtistInfo(artist): +def getArtistInfo(artist,fast=False): obj = artist filename = re.sub("[^a-zA-Z0-9]","",artist) @@ -314,6 +319,11 @@ def getArtistInfo(artist): except: pass + + # fast request only retuns cached and local results, generates redirect link for rest + if fast: + return "/image?artist=" + urllib.parse.quote(artist) + result = apirequest(artist=artist) if result.get("image") is not None: cachedArtists[artist] = result["image"] @@ -321,12 +331,12 @@ def getArtistInfo(artist): else: return {"image":""} -def getTracksInfo(trackobjectlist): +def getTracksInfo(trackobjectlist,fast=False): threads = [] for track in trackobjectlist: - t = Thread(target=getTrackInfo,args=(track["artists"],track["title"],)) + t = Thread(target=getTrackInfo,args=(track["artists"],track["title"],),kwargs={"fast":fast}) t.start() threads.append(t) @@ -336,12 +346,12 @@ def getTracksInfo(trackobjectlist): return [getTrackInfo(t["artists"],t["title"]) for t in trackobjectlist] -def getArtistsInfo(artistlist): +def getArtistsInfo(artistlist,fast=False): threads = [] for artist in artistlist: - t = Thread(target=getArtistInfo,args=(artist,)) + t = Thread(target=getArtistInfo,args=(artist,),kwargs={"fast":fast}) t.start() threads.append(t) diff --git a/website/start.py b/website/start.py index 58ec5cc..484078f 100644 --- a/website/start.py +++ b/website/start.py @@ -9,8 +9,8 @@ def instructions(keys): from utilities import getArtistsInfo, getTracksInfo from htmlgenerators import artistLink, trackLink - max_show = 14 - posrange = ["#" + str(i) for i in range(1,max_show+1)] +# max_show = 14 +# posrange = ["#" + str(i) for i in range(1,max_show+1)] # get chart data
""" + rank + " " + link + "