Implemented compare in jinja

This commit is contained in:
Krateng 2020-08-30 18:53:02 +02:00
parent 44a2739a3b
commit 65861d4c41
6 changed files with 234 additions and 3 deletions

View File

@ -12,3 +12,4 @@ countas Shirley Manson Garbage
countas Lewis Brindley The Yogscast countas Lewis Brindley The Yogscast
countas Sips The Yogscast countas Sips The Yogscast
countas Sjin The Yogscast countas Sjin The Yogscast
countas Airi Suzuki ℃-ute

Can't render this file because it has a wrong number of fields in line 5.

View File

@ -681,6 +681,62 @@ def trackInfo(track):
@dbserver.get("compare")
def compare_external(**keys):
results = compare(keys["remote"])
return results
def compare(remoteurl):
import json
compareurl = remoteurl + "/api/info"
response = urllib.request.urlopen(compareurl)
strangerinfo = json.loads(response.read())
owninfo = info()
#add_known_server(compareto)
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((artists[a]["name"] 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":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)
for r in result:
result[r] = (result[r],result[r]/total)
return {
"result":result,
"info":{
"ownname":owninfo["name"],
"remotename":strangerinfo["name"]
},
"commonartist":best[0]
}

60
maloja/malojauri.py Normal file
View File

@ -0,0 +1,60 @@
from .malojatime import get_range_object
# this also sets defaults!
def uri_to_internal(keys,forceTrack=False,forceArtist=False):
# output:
# 1 keys that define the filtered object like artist or track
# 2 keys that define time limits of the whole thing
# 3 keys that define interal time ranges
# 4 keys that define amount limits
# 1
if "title" in keys and not forceArtist:
filterkeys = {"track":{"artists":keys.getall("artist"),"title":keys.get("title")}}
elif "artist" in keys and not forceTrack:
filterkeys = {"artist":keys.get("artist")}
if "associated" in keys: resultkeys1["associated"] = True
else:
filterkeys = {}
# 2
limitkeys = {}
since,to,within = None,None,None
if "since" in keys: since = keys.get("since")
elif "from" in keys: since = keys.get("from")
elif "start" in keys: since = keys.get("start")
if "to" in keys: to = keys.get("to")
elif "until" in keys: to = keys.get("until")
elif "end" in keys: to = keys.get("end")
if "in" in keys: within = keys.get("in")
elif "within" in keys: within = keys.get("within")
elif "during" in keys: within = keys.get("during")
limitkeys["timerange"] = get_range_object(since=since,to=to,within=within)
#3
delimitkeys = {"step":"month","stepn":1,"trail":1}
if "step" in keys: [delimitkeys["step"],delimitkeys["stepn"]] = (keys["step"].split("-") + [1])[:2]
if "stepn" in keys: delimitkeys["stepn"] = keys["stepn"] #overwrite if explicitly given
if "stepn" in delimitkeys: delimitkeys["stepn"] = int(delimitkeys["stepn"]) #in both cases, convert it here
if "trail" in keys: delimitkeys["trail"] = int(keys["trail"])
if "cumulative" in keys: delimitkeys["trail"] = math.inf
#4
amountkeys = {"page":0,"perpage":100}
if "max" in keys: amountkeys["page"],amountkeys["perpage"] = 0, int(keys["max"])
#different max than the internal one! the user doesn't get to disable pagination
if "page" in keys: amountkeys["page"] = int(keys["page"])
if "perpage" in keys: amountkeys["perpage"] = int(keys["perpage"])
#5
specialkeys = {}
if "remote" in keys: specialkeys["remote"] = keys["remote"]
return filterkeys, limitkeys, delimitkeys, amountkeys, specialkeys

View File

@ -17,7 +17,8 @@ from . import htmlgenerators
from . import malojatime from . import malojatime
from . import utilities from . import utilities
from .utilities import resolveImage from .utilities import resolveImage
from .urihandler import uri_to_internal, remove_identical from .urihandler import remove_identical
from .malojauri import uri_to_internal
from . import urihandler from . import urihandler
from . import globalconf from . import globalconf
from . import jinja_filters from . import jinja_filters
@ -114,7 +115,7 @@ def graceful_exit(sig=None,frame=None):
@webserver.route("/image") @webserver.route("/image")
def dynamic_image(): def dynamic_image():
keys = FormsDict.decode(request.query) keys = FormsDict.decode(request.query)
relevant, _, _, _ = uri_to_internal(keys) relevant, _, _, _, _ = uri_to_internal(keys)
result = resolveImage(**relevant) result = resolveImage(**relevant)
if result == "": return "" if result == "": return ""
redirect(result,307) redirect(result,307)
@ -265,7 +266,8 @@ def static_html(name):
"apikey":request.cookies.get("apikey") if adminmode else None, "apikey":request.cookies.get("apikey") if adminmode else None,
"_urikeys":keys, #temporary! "_urikeys":keys, #temporary!
} }
LOCAL_CONTEXT["filterkeys"], LOCAL_CONTEXT["limitkeys"], LOCAL_CONTEXT["delimitkeys"], LOCAL_CONTEXT["amountkeys"] = uri_to_internal(keys) lc = LOCAL_CONTEXT
lc["filterkeys"], lc["limitkeys"], lc["delimitkeys"], lc["amountkeys"], lc["specialkeys"] = uri_to_internal(keys)
template = jinjaenv.get_template(name + '.jinja') template = jinjaenv.get_template(name + '.jinja')

View File

@ -698,6 +698,8 @@ table.tiles_top td div {
table.tiles_top td span { table.tiles_top td span {
background-color:rgba(0,0,0,0.7); background-color:rgba(0,0,0,0.7);
display: inline-block;
padding: 3px;
} }
table.tiles_top td a:hover { table.tiles_top td a:hover {
text-decoration: none; text-decoration: none;

View File

@ -0,0 +1,110 @@
{% extends "abstracts/base.jinja" %}
{% block title %}Maloja - Compare{% endblock %}
{% block scripts %}
<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); */
}
table tr td:first-child {
text-align: left;
padding:10px;
width:33%;
}
table tr td {
text-align: center;
padding:10px;
}
table tr td:last-child {
text-align: right;
padding:10px;
width:33%;
}
</style>
{% endblock %}
{% set data = db.compare(specialkeys.remote) %}
{% set comparedata = data.result %}
{% set info = data.info %}
{% set bestartist = data.commonartist %}
<!--
{% set categories =
{
"unique_self":"rgba(255,255,255,0.2)",
"more_self":"rgba(255,255,255,0.5)",
"common":"white",
"more_other":"rgba(255,255,255,0.5)",
"unique_other":"rgba(255,255,255,0.2)"
}
%}-->
<!--
{% set css = [] %}
{% set cumulative = 0 %}
{% for cat in categories %}
{% set cumulative = cumulative + (comparedata[cat][1]*100) %}
{% set _ = css.append(categories[cat] + " " + cumulative.__str__() + "%") %}
{% endfor %}-->
{% set fullmatch = comparedata.common[1]*100 %}
{% set partialmatch = comparedata.more_self[1]*100 + comparedata.more_other[1]*100 %}
{% set match = fullmatch + (partialmatch)/2 %}
{% set pixel_fullmatch = fullmatch * 2.5 %}
{% set pixel_partialmatch = (fullmatch+partialmatch) * 2.5 %}
{% set match = [match,100] | min %}
{% set r = [255*match/50,255] | min %}
{% set g = [255*match/50,255] | min %}
{% set b = [255*(match/50-1),0] | max %}
{% block content %}
<table style="width:99%;">
<tr>
<td><h1>{{ info.ownname }}</h1></td>
<td>
<div class="comparecircle"
style="background-image: radial-gradient(rgb({{ r }},{{ g }}, {{ b }}) {{ pixel_fullmatch }}px, transparent {{ pixel_partialmatch }}px);">
{{ match | round(1) }}%
</div>
</td>
<td><h1>{{ info.remotename }}</h1></td>
</tr>
<tr>
<td></td>
<td style="font-size:70%;color:grey;">
The size of the circle shows matching music taste.
The fuzziness of its border indicates differences in quantity.
</td>
<td></td>
</tr>
<tr>
<td></td>
<td>
<span>Common Favorite</span>
<h2 style="margin:7px;">{{ htmlgenerators.artistLink(bestartist) }}</h2>
<img src="{{ utilities.getArtistImage(bestartist) }}" style="width:80px;" />
</td>
<td></td>
</tr>
</table>
{% endblock %}