Added bare-bones experimental compare feature

This commit is contained in:
Krateng 2019-06-18 12:34:10 +02:00
parent aeaaf3a8e9
commit 8a80c265cd
3 changed files with 146 additions and 1 deletions

View File

@ -239,7 +239,7 @@ def info():
"name":settings.get_settings("NAME"),
"artists":{
chartentry["artist"]:round(chartentry["scrobbles"] * 100 / totalscrobbles,3)
for chartentry in get_charts_artists() if chartentry["scrobbles"]/totalscrobbles >= 0.001}
for chartentry in get_charts_artists() if chartentry["scrobbles"]/totalscrobbles >= 0}
}

65
website/compare.html Normal file
View File

@ -0,0 +1,65 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8" />
<title>Maloja - Compare</title>
<style>
.comparecircle {
height:500px;
width:500px;
border-radius:250px;
border: 1px solid rgba(245,245,220,0.3);
margin:auto;
margin-top:100px;
text-align:center;
line-height:500px;
font-size:60px;
color:black;
/* background-image: linear-gradient(to right,KEY_CIRCLE_CSS); */
background-image: radial-gradient(#KEY_CICLE_COLOR KEY_FULLMATCHpx, transparent KEY_PARTIALMATCHpx);
}
table tr td:first-child {
text-align: left;
padding:20px;
}
table tr td {
text-align: center;
padding:20px;
}
table tr td:last-child {
text-align: right;
padding:20px;
}
</style>
</head>
<body>
<table style="width:99%;">
<tr>
<td><h1>KEY_NAME_SELF</h1></td>
<td>
<div class="comparecircle">
KEY_MATCH%
</div>
</td>
<td><h1>KEY_NAME_OTHER</h1></td>
</tr>
<tr>
<td></td>
<td style="font-size:70%">
The size of the circle shows matching music taste.
The fuzziness of its border indicates differences in quantity.
</td>
<td></td>
</tr>
</table>
</body>
</html>

80
website/compare.py Normal file
View File

@ -0,0 +1,80 @@
import urllib
import database
import json
def instructions(keys):
compareto = keys.get("to")
compareurl = compareto + "/api/info"
response = urllib.request.urlopen(compareurl)
strangerinfo = json.loads(response.read())
owninfo = database.info()
artists = {}
for a in owninfo["artists"]:
artists[a.lower()] = {"name":a,"self":int(owninfo["artists"][a]*1000),"other":0}
for a in strangerinfo["artists"]:
artists[a.lower()] = artists.setdefault(a.lower(),{"name":a,"self":0})
artists[a.lower()]["other"] = int(strangerinfo["artists"][a]*1000)
for a in artists:
common = min(artists[a]["self"],artists[a]["other"])
artists[a]["self"] -= common
artists[a]["other"] -= common
artists[a]["common"] = common
best = sorted((a for a in artists),key=lambda x: artists[x.lower()]["common"],reverse=True)
result = {
"unique_self":sum(artists[a]["self"] for a in artists if artists[a]["common"] == 0),
"more_self":sum(artists[a]["self"] for a in artists if artists[a]["common"] != 0),
# "common":{
# **{
# artists[a]["name"]:artists[a]["common"]
# for a in best[:3]},
# None: sum(artists[a]["common"] for a in artists if a not in best[:3])
# },
"common":sum(artists[a]["common"] for a in artists),
"more_other":sum(artists[a]["other"] for a in artists if artists[a]["common"] != 0),
"unique_other":sum(artists[a]["other"] for a in artists if artists[a]["common"] == 0)
}
total = sum(result[c] for c in result)
percentages = {c:result[c]*100/total for c in result}
css = []
cumulative = 0
for color,category in [
("rgba(255,255,255,0.2)","unique_self"),
("rgba(255,255,255,0.5)","more_self"),
("white","common"),
("rgba(255,255,255,0.5)","more_other"),
("rgba(255,255,255,0.2)","unique_other")]:
cumulative += percentages[category]
css.append(color + " " + str(cumulative) + "%")
fullmatch = percentages["common"]
partialmatch = percentages["more_self"] + percentages["more_other"]
match = fullmatch + (partialmatch)/2
pixel_fullmatch = fullmatch * 2.5
pixel_partialmatch = (fullmatch+partialmatch) * 2.5
matchcolor = format(int(min(1,match/50)*255),"x") * 2 + format(int(max(0,match/50-1)*255),"x")
return {
"KEY_CIRCLE_CSS":",".join(css),
"KEY_CICLE_COLOR":matchcolor,
"KEY_MATCH":str(round(match,2)),
"KEY_FULLMATCH":str(int(pixel_fullmatch)),
"KEY_PARTIALMATCH":str(int(pixel_partialmatch)),
"KEY_NAME_SELF":owninfo["name"],
"KEY_NAME_OTHER":strangerinfo["name"],
},[]