1
0
mirror of https://github.com/krateng/maloja.git synced 2023-08-10 21:12:55 +03:00
maloja/website/artist.py

74 lines
2.8 KiB
Python
Raw Normal View History

2018-12-16 19:52:13 +03:00
import urllib
import json
def instructions(keys,dbport):
2018-12-26 21:20:26 +03:00
from utilities import getArtistInfo
2019-02-20 01:18:46 +03:00
from htmlgenerators import clean, artistLink, artistLinks, trackLink, scrobblesTrackLink, getRangeDesc, scrobblesLink
2018-12-16 19:52:13 +03:00
2018-12-27 05:09:29 +03:00
clean(keys)
2018-12-17 17:10:10 +03:00
info = getArtistInfo(keys["artist"])
imgurl = info.get("image")
2018-12-28 20:06:09 +03:00
#desc = info.get("info")
pushresources = [{"file":imgurl,"type":"image"}] if imgurl.startswith("/") else []
2018-12-16 19:52:13 +03:00
response = urllib.request.urlopen("http://[::1]:" + str(dbport) + "/artistinfo?artist=" + urllib.parse.quote(keys["artist"]))
2018-12-17 01:56:30 +03:00
db_data = json.loads(response.read())
scrobbles = str(db_data["scrobbles"])
pos = "#" + str(db_data["position"])
2018-12-17 18:33:26 +03:00
2018-12-17 01:56:30 +03:00
credited = db_data.get("replace")
includestr = " "
if credited is not None:
includestr = "Competing under " + artistLink(credited) + " (" + pos + ")"
2018-12-17 01:56:30 +03:00
pos = ""
included = db_data.get("associated")
if included is not None and included != []:
includestr = "associated: "
2018-12-27 05:09:29 +03:00
includestr += artistLinks(included)
response = urllib.request.urlopen("http://[::1]:" + str(dbport) + "/charts/tracks?artist=" + urllib.parse.quote(keys["artist"]))
2018-12-16 19:52:13 +03:00
db_data = json.loads(response.read())
if db_data["list"] != []: maxbar = db_data["list"][0]["scrobbles"]
html = "<table class='list'>"
2018-12-16 19:52:13 +03:00
for e in db_data["list"]:
html += "<tr>"
2018-12-27 05:09:29 +03:00
html += "<td class='artists'>" + artistLinks(e["track"]["artists"]) + "</td>"
html += "<td>" + trackLink(e["track"]) + "</td>"
html += "<td class='amount'>" + scrobblesTrackLink(e["track"],{},amount=e["scrobbles"]) + "</td>"
2018-12-28 20:06:09 +03:00
html += "<td class='bar'>" + scrobblesTrackLink(e["track"],{},percent=e["scrobbles"]*100/maxbar) + "</td>"
html += "</tr>"
html += "</table>"
2018-12-16 19:52:13 +03:00
2019-02-20 01:18:46 +03:00
# pulse
response = urllib.request.urlopen("http://[::1]:" + str(dbport) + "/pulse?step=year&trail=1&artist=" + urllib.parse.quote(keys["artist"]))
db_data = json.loads(response.read())
terms = db_data["list"]
# build list
maxbar = max([t["scrobbles"] for t in terms])
html_pulse = "<table class='list'>"
for t in terms:
fromstr = "/".join([str(e) for e in t["from"]])
tostr = "/".join([str(e) for e in t["to"]])
html_pulse += "<tr>"
#html += "<td>" + fromstr + "</td>"
#html += "<td>" + tostr + "</td>"
html_pulse += "<td>" + getRangeDesc(t["from"],t["to"]) + "</td>"
html_pulse += "<td class='amount'>" + scrobblesLink({"since":fromstr,"to":tostr},amount=t["scrobbles"],artist=keys["artist"]) + "</td>"
html_pulse += "<td class='bar'>" + scrobblesLink({"since":fromstr,"to":tostr},percent=t["scrobbles"]*100/maxbar,artist=keys["artist"]) + "</td>"
html_pulse += "</tr>"
html_pulse += "</table>"
2018-12-16 19:52:13 +03:00
2019-02-20 01:18:46 +03:00
replace = {"KEY_ARTISTNAME":keys["artist"],"KEY_ENC_ARTISTNAME":urllib.parse.quote(keys["artist"]),"KEY_IMAGEURL":imgurl, "KEY_DESCRIPTION":"","KEY_TRACKLIST":html,"KEY_SCROBBLES":scrobbles,"KEY_POSITION":pos,"KEY_ASSOCIATED":includestr,"KEY_PULSE":html_pulse}
return (replace,pushresources)