From 6be0dfd25bfdeaa4072065322be95e65e7e4a4dc Mon Sep 17 00:00:00 2001 From: Krateng Date: Sun, 16 Dec 2018 23:56:30 +0100 Subject: [PATCH] Improvement and bugfixing --- cleanup.py | 18 ++++++++++----- database.py | 31 +++++++++++++------------ server.py | 10 +++++++- website/artist.html | 5 ++-- website/artist.py | 56 ++++++++++++++++++++++++++++++--------------- website/maloja.css | 9 +++++++- 6 files changed, 87 insertions(+), 42 deletions(-) diff --git a/cleanup.py b/cleanup.py index a51024e..cf20b7f 100644 --- a/cleanup.py +++ b/cleanup.py @@ -49,6 +49,13 @@ class CleanerAgent: if re.match(r"(.*) \(" + d + " (.*)\)",a) is not None: return self.parseArtists(re.sub(r"(.*) \(" + d + " (.*)\)",r"\1",a)) + self.parseArtists(re.sub(r"(.*) \(" + d + " (.*)\)",r"\2",a)) + for d in self.delimiters_formal: + if (d in a): + ls = [] + for i in a.split(d): + ls += self.parseArtists(i) + return ls + for d in (self.delimiters_feat + self.delimiters): if ((" " + d + " ") in a): ls = [] @@ -56,12 +63,7 @@ class CleanerAgent: ls += self.parseArtists(i) return ls - for d in self.delimiters_formal: - if (d in a): - ls = [] - for i in a.split(d): - ls += self.parseArtists(i) - return ls + @@ -76,6 +78,7 @@ class CleanerAgent: t = re.sub(r" \(as made famous by .*?\)","",t) t = re.sub(r" \(originally by .*?\)","",t) + t = re.sub(r" \(.*?Remaster.*?\)","",t) return t.strip() @@ -132,6 +135,9 @@ class CollectorAgent: updatedArtists.append(self.getCredited(artist)) return list(set(updatedArtists)) + def getAllAssociated(self,artist): + return self.rules_include.get(artist,[]) + diff --git a/database.py b/database.py index 113e78b..dd86126 100644 --- a/database.py +++ b/database.py @@ -121,7 +121,8 @@ def get_scrobbles(): @route("/tracks") def get_tracks(): - artist = request.query.get("artist") + keys = FormsDict.decode(request.query) + artist = keys.get("artist") if artist is not None: artistid = ARTISTS.index(artist) @@ -297,20 +298,22 @@ def getNext(time,unit,step=1): elif unit == "week": return getNext(time,"day",step * 7) +@route("/artistinfo") +def artistInfo(): + keys = FormsDict.decode(request.query) + artist = keys.get("artist") -#def addDate(date,inc): -# newdate = [1,1,1] -# newdate[0] = date[0] + inc[0] -# newdate[1] = date[1] + inc[1] -# newdate[2] = date[2] + inc[2] -# while (newdate[1] > 12): -# newdate[1] -= 12 -# newdate[0] += 1 -# while (newdate[1] < 1): -# newdate[1] += 12 -# newdate[0] -= 1 -# -# return newdate + charts = db_aggregate(by="ARTIST") + scrobbles = len(db_query(artist=artist)) #we cant take the scrobble number from the charts because that includes all countas scrobbles + try: + c = [e for e in charts if e["artist"] == artist][0] + others = sovereign.getAllAssociated(artist) + return {"scrobbles":scrobbles,"position":charts.index(c) + 1,"associated":others} + except: + # if the artist isnt in the charts, they are not being credited and we need to show information about the credited one + artist = sovereign.getCredited(artist) + c = [e for e in charts if e["artist"] == artist][0] + return {"replace":artist,"scrobbles":scrobbles,"position":charts.index(c) + 1} def isPast(date,limit): if not date[0] == limit[0]: diff --git a/server.py b/server.py index 76e4943..4118399 100755 --- a/server.py +++ b/server.py @@ -81,8 +81,16 @@ def static(name): @route("/") def static_html(name): + keys = FormsDict.decode(request.query) if os.path.exists("website/" + name + ".py"): - return SourceFileLoader(name,"website/" + name + ".py").load_module().page(FormsDict.decode(request.query)) + txt_keys = SourceFileLoader(name,"website/" + name + ".py").load_module().replacedict(keys,DATABASE_PORT) + with open("website/" + name + ".html") as htmlfile: + html = htmlfile.read() + for k in txt_keys: + html = html.replace(k,txt_keys[k]) + return html + + return static_file("website/" + name + ".html",root="") diff --git a/website/artist.html b/website/artist.html index 270262b..910efad 100644 --- a/website/artist.html +++ b/website/artist.html @@ -14,8 +14,9 @@ -

KEY_ARTISTNAME

-

2342 Scrobbles / 23th all time / 12th this month

+

KEY_ARTISTNAME

KEY_POSITION
+ KEY_ASSOCIATED +

KEY_SCROBBLES Scrobbles

KEY_DESCRIPTION diff --git a/website/artist.py b/website/artist.py index e001046..1083181 100644 --- a/website/artist.py +++ b/website/artist.py @@ -3,37 +3,57 @@ import json -def page(keys): - - txt_keys = replace(keys) - - - with open("website/artist.html","r") as htmlfile: - html = htmlfile.read() - +#def page(keys): +# +# txt_keys = replace(keys) +# +# +# with open("website/artist.html","r") as htmlfile: +# html = htmlfile.read() +# +# +# +# for k in txt_keys: +# html = html.replace(k,txt_keys[k]) +# +# return html +def replacedict(keys,dbport): - for k in txt_keys: - html = html.replace(k,txt_keys[k]) - - return html - -def replace(keys): with open("website/apikey","r") as keyfile: apikey = keyfile.read().replace("\n","") - url = "https://ws.audioscrobbler.com/2.0/?method=artist.getinfo&artist=" + keys["artist"] + "&api_key=" + apikey + "&format=json" + + + url = "https://ws.audioscrobbler.com/2.0/?method=artist.getinfo&artist=" + urllib.parse.quote(keys["artist"]) + "&api_key=" + apikey + "&format=json" response = urllib.request.urlopen(url) lastfm_data = json.loads(response.read()) imgurl = lastfm_data["artist"]["image"][2]["#text"] desc = lastfm_data["artist"]["bio"]["summary"] - response = urllib.request.urlopen("http://localhost:42010/db/tracks?artist=" + urllib.parse.quote(keys["artist"])) + response = urllib.request.urlopen("http://localhost:" + str(dbport) + "/artistinfo?artist=" + urllib.parse.quote(keys["artist"])) + db_data = json.loads(response.read()) + scrobbles = str(db_data["scrobbles"]) + pos = "#" + str(db_data["position"]) + credited = db_data.get("replace") + includestr = " " + if credited is not None: + includestr = "Competing under " + credited + " (" + pos + ")" + pos = "" + included = db_data.get("associated") + if included is not None and included != []: + includestr = "associated: " + for a in included: + includestr += "" + a + ", " + includestr = includestr[:-2] + + response = urllib.request.urlopen("http://localhost:" + str(dbport) + "/tracks?artist=" + urllib.parse.quote(keys["artist"])) db_data = json.loads(response.read()) tracks = [] for e in db_data["list"]: html = "" for a in e["artists"]: - html += "" + a + " " + html += "" + a + ", " + html = html[:-2] html += "" + e["title"] + "" tracks.append(html) @@ -45,4 +65,4 @@ def replace(keys): trackshtml += "" - return {"KEY_ARTISTNAME":keys["artist"],"KEY_IMAGEURL":imgurl,"KEY_DESCRIPTION":desc,"KEY_TRACKLIST":trackshtml} + return {"KEY_ARTISTNAME":keys["artist"],"KEY_IMAGEURL":imgurl,"KEY_DESCRIPTION":desc,"KEY_TRACKLIST":trackshtml,"KEY_SCROBBLES":scrobbles,"KEY_POSITION":pos,"KEY_ASSOCIATED":includestr} diff --git a/website/maloja.css b/website/maloja.css index c32cb56..d4ceff2 100644 --- a/website/maloja.css +++ b/website/maloja.css @@ -18,6 +18,8 @@ a:hover { table.top_info td.image { padding:20px; + padding-left:0px; + padding-top:0px; } table.top_info td.text { @@ -25,6 +27,11 @@ table.top_info td.text { padding-left: 30px; } -table.top_info td.text p.stats { +table.top_info td.text h1 { + display:inline; + padding-right:5px; +} + +table.top_info td.text .stats { color:grey; }