From 1643eafc715804e2d5267155753deca459d972d6 Mon Sep 17 00:00:00 2001 From: Krateng Date: Sun, 16 Dec 2018 17:52:13 +0100 Subject: [PATCH] Basic artist overview webpage --- database.py | 113 ++++++++++++++++++++++++-------------------- server.py | 3 ++ website/.gitignore | 2 + website/artist.html | 29 ++++++++++++ website/artist.py | 48 +++++++++++++++++++ website/maloja.css | 24 +++++++++- 6 files changed, 166 insertions(+), 53 deletions(-) create mode 100644 website/.gitignore create mode 100644 website/artist.html create mode 100644 website/artist.py diff --git a/database.py b/database.py index e0e75af..113e78b 100644 --- a/database.py +++ b/database.py @@ -166,34 +166,27 @@ def get_pulse(): since = request.query.get("since") to = request.query.get("to") (ts_start,ts_end) = getTimestamps(since,to) - date_start = datetime.datetime.utcfromtimestamp(ts_start) - date_end = datetime.datetime.utcfromtimestamp(ts_end) - #date_start = datetime.datetime.utcfromtimestamp(min(timestamps)) - #date_end = datetime.datetime.utcnow() - d_end = [date_end.year,date_end.month,date_end.day] - - step = request.query.get("step") - if (step == None): - step = "month" + step = request.query.get("step","month") + trail = int(request.query.get("trail",3)) [step,stepn] = (step.split("-") + [1])[:2] # makes the multiplier 1 if not assigned + stepn = int(stepn) - if step == "year": - d_start = [date_start.year,1,1] - elif step == "month": - d_start = [date_start.year,date_start.month,1] - - inc = [i*int(stepn) for i in {"year":[1,0,0],"month":[0,1,0]}[step]] + d_start = getStartOf(ts_start,step) + d_end = getStartOf(ts_end,step) + d_start = getNext(d_start,step,stepn) # first range should end right after the first active scrobbling week / month / whatever relevant step + d_start = getNext(d_start,step,stepn * trail * -1) # go one range back to begin results = [] d_current = d_start while True: - d_current_end = addDate(d_current,inc) + d_current_end = getNext(d_current,step,stepn * trail) + #print("Checking from " + str(d_current[0]) + "-" + str(d_current[1]) + "-" + str(d_current[2]) + " to " + str(d_current_end[0]) + "-" + str(d_current_end[1]) + "-" + str(d_current_end[2])) res = db_aggregate(since=d_current,to=d_current_end) results.append({"from":d_current,"to":d_current_end,"scrobbles":res}) - d_current = d_current_end + d_current = getNext(d_current,step,stepn) if isPast(d_current_end,d_end): break @@ -205,33 +198,17 @@ def get_top_artists(): since = request.query.get("since") to = request.query.get("to") (ts_start,ts_end) = getTimestamps(since,to) - #date_start = datetime.datetime.utcfromtimestamp(ts_start) - #date_end = datetime.datetime.utcfromtimestamp(ts_end) - #d_end = [date_end.year,date_end.month,date_end.day] - - # We use a trailing multiplier instead of a separate argument for time and step to avoid weirdness - # e.g. if our steps are weeks, but the time is a month, should the value for 3/31 go back to 2/28? step = request.query.get("step","month") - trail = int(request.query.get("trail","3")) + trail = int(request.query.get("trail",3)) [step,stepn] = (step.split("-") + [1])[:2] # makes the multiplier 1 if not assigned - #[time,timen] = (time.split("-") + [1])[:2] stepn = int(stepn) d_start = getStartOf(ts_start,step) d_end = getStartOf(ts_end,step) - #if step == "year": - # d_start = [date_start.year,1,1] - #elif step == "month": - # d_start = [date_start.year,date_start.month,1] - - #inc = [i*int(stepn) for i in {"year":[1,0,0],"month":[0,1,0]}[step]] - #ran = [i*int(timen) for i in {"year":[1,0,0],"month":[0,1,0]}[time]] - #d_start = addDate(d_start,inc) # first range should end right after the first active scrobbling week / month / whatever relevant step - #d_start = addDate(d_start,[-i for i in ran]) # go one range back to begin - d_start = getNext(d_start,step,stepn) - d_start = getNext(d_start,step,stepn * trail * -1) + d_start = getNext(d_start,step,stepn) # first range should end right after the first active scrobbling week / month / whatever relevant step + d_start = getNext(d_start,step,stepn * trail * -1) # go one range back to begin results = [] @@ -239,14 +216,46 @@ def get_top_artists(): while True: d_current_end = getNext(d_current,step,stepn * trail) #print("Checking from " + str(d_current[0]) + "-" + str(d_current[1]) + "-" + str(d_current[2]) + " to " + str(d_current_end[0]) + "-" + str(d_current_end[1]) + "-" + str(d_current_end[2])) - #d_current_end = addDate(d_current,ran) try: res = db_aggregate(since=d_current,to=d_current_end,by="ARTIST")[0] results.append({"from":d_current,"to":d_current_end,"artist":res["artist"],"scrobbles":res["scrobbles"]}) except: results.append({"from":d_current,"to":d_current_end,"artist":None,"scrobbles":0}) d_current = getNext(d_current,step,stepn) - #d_current = addDate(d_current,inc) + if isPast(d_current_end,d_end): + break + + return {"list":results} + +@route("/top/tracks") +def get_top_tracks(): + since = request.query.get("since") + to = request.query.get("to") + (ts_start,ts_end) = getTimestamps(since,to) + step = request.query.get("step","month") + trail = int(request.query.get("trail",3)) + + [step,stepn] = (step.split("-") + [1])[:2] # makes the multiplier 1 if not assigned + stepn = int(stepn) + + d_start = getStartOf(ts_start,step) + d_end = getStartOf(ts_end,step) + + d_start = getNext(d_start,step,stepn) # first range should end right after the first active scrobbling week / month / whatever relevant step + d_start = getNext(d_start,step,stepn * trail * -1) # go one range back to begin + + results = [] + + d_current = d_start + while True: + d_current_end = getNext(d_current,step,stepn * trail) + #print("Checking from " + str(d_current[0]) + "-" + str(d_current[1]) + "-" + str(d_current[2]) + " to " + str(d_current_end[0]) + "-" + str(d_current_end[1]) + "-" + str(d_current_end[2])) + try: + res = db_aggregate(since=d_current,to=d_current_end,by="TRACK")[0] + results.append({"from":d_current,"to":d_current_end,"track":res["track"],"scrobbles":res["scrobbles"]}) + except: + results.append({"from":d_current,"to":d_current_end,"track":None,"scrobbles":0}) + d_current = getNext(d_current,step,stepn) if isPast(d_current_end,d_end): break @@ -287,21 +296,21 @@ def getNext(time,unit,step=1): #eugh elif unit == "week": return getNext(time,"day",step * 7) + -# DEPRECATED -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 +#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 def isPast(date,limit): if not date[0] == limit[0]: diff --git a/server.py b/server.py index 9ef3b29..76e4943 100755 --- a/server.py +++ b/server.py @@ -9,6 +9,7 @@ import urllib.parse from urllib.error import * import sys import signal +import os MAIN_PORT = 42010 @@ -80,6 +81,8 @@ def static(name): @route("/") def static_html(name): + if os.path.exists("website/" + name + ".py"): + return SourceFileLoader(name,"website/" + name + ".py").load_module().page(FormsDict.decode(request.query)) return static_file("website/" + name + ".html",root="") diff --git a/website/.gitignore b/website/.gitignore new file mode 100644 index 0000000..da54ded --- /dev/null +++ b/website/.gitignore @@ -0,0 +1,2 @@ +__pycache__ +apikey diff --git a/website/artist.html b/website/artist.html new file mode 100644 index 0000000..270262b --- /dev/null +++ b/website/artist.html @@ -0,0 +1,29 @@ + + + + + + Maloja + + + + + + + + + +
+ + +

KEY_ARTISTNAME

+

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

+ + KEY_DESCRIPTION +
+ +

Tracks

+ KEY_TRACKLIST + + + diff --git a/website/artist.py b/website/artist.py new file mode 100644 index 0000000..e001046 --- /dev/null +++ b/website/artist.py @@ -0,0 +1,48 @@ +import urllib +import json + + + +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 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" + 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"])) + db_data = json.loads(response.read()) + tracks = [] + for e in db_data["list"]: + html = "" + for a in e["artists"]: + html += "" + a + " " + html += "" + e["title"] + "" + tracks.append(html) + + trackshtml = "" + for t in tracks: + trackshtml += "" + trackshtml += t + trackshtml += "" + trackshtml += "
" + + + return {"KEY_ARTISTNAME":keys["artist"],"KEY_IMAGEURL":imgurl,"KEY_DESCRIPTION":desc,"KEY_TRACKLIST":trackshtml} diff --git a/website/maloja.css b/website/maloja.css index 8428de3..c32cb56 100644 --- a/website/maloja.css +++ b/website/maloja.css @@ -1,8 +1,30 @@ @import url('https://fonts.googleapis.com/css?family=Ubuntu'); body { - background-color:#111114; + background-color:#333337; color:beige; font-family:"Ubuntu"; padding:15px; } + +a { + color:lightgray; + text-decoration:none; +} + +a:hover { + text-decoration:underline; +} + +table.top_info td.image { + padding:20px; +} + +table.top_info td.text { + vertical-align: top; + padding-left: 30px; +} + +table.top_info td.text p.stats { + color:grey; +}