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

Experimental use of pyhp for artist page

This commit is contained in:
Krateng 2019-05-21 17:29:07 +02:00
parent 2ae78bcec2
commit ab648db336
3 changed files with 221 additions and 33 deletions

View File

@ -7,6 +7,9 @@ import waitress
import monkey import monkey
# rest of the project # rest of the project
import database import database
import utilities
import htmlmodules
import malojatime
from utilities import * from utilities import *
from urihandler import uri_to_internal, remove_identical from urihandler import uri_to_internal, remove_identical
# doreah toolkit # doreah toolkit
@ -20,6 +23,7 @@ import signal
import os import os
import setproctitle import setproctitle
# url handling # url handling
import urllib
import urllib.request import urllib.request
import urllib.parse import urllib.parse
from urllib.error import * from urllib.error import *
@ -128,46 +132,74 @@ def static_html(name):
linkheaders = ["</css/maloja.css>; rel=preload; as=style"] linkheaders = ["</css/maloja.css>; rel=preload; as=style"]
keys = remove_identical(FormsDict.decode(request.query)) keys = remove_identical(FormsDict.decode(request.query))
with open("website/" + name + ".html") as htmlfile: # if a pyhp file exists, use this
html = htmlfile.read() if os.path.exists("website/" + name + ".pyhp"):
from doreah.pyhp import file
#try:
# d = SourceFileLoader(name,"website/" + name + ".py").load_module().pyhp(keys)
# print("loaded dict")
#except:
# d = {}
# print("error")
# raise
environ = {} #things we expose to the pyhp pages
# apply global substitutions # maloja
with open("website/common/footer.html") as footerfile: environ["db"] = database
footerhtml = footerfile.read() environ["htmlmodules"] = htmlmodules
with open("website/common/header.html") as headerfile: environ["malojatime"] = malojatime
headerhtml = headerfile.read() environ["utilities"] = utilities
html = html.replace("</body>",footerhtml + "</body>").replace("</head>",headerhtml + "</head>") # external
environ["urllib"] = urllib
# request
environ["filterkeys"], environ["limitkeys"], environ["delimitkeys"], environ["amountkeys"] = uri_to_internal(keys)
#response.set_header("Content-Type","application/xhtml+xml")
return file("website/" + name + ".pyhp",environ)
# if not, use the old way
else:
with open("website/" + name + ".html") as htmlfile:
html = htmlfile.read()
# apply global substitutions
with open("website/common/footer.html") as footerfile:
footerhtml = footerfile.read()
with open("website/common/header.html") as headerfile:
headerhtml = headerfile.read()
html = html.replace("</body>",footerhtml + "</body>").replace("</head>",headerhtml + "</head>")
# If a python file exists, it provides the replacement dict for the html file # If a python file exists, it provides the replacement dict for the html file
if os.path.exists("website/" + name + ".py"): if os.path.exists("website/" + name + ".py"):
#txt_keys = SourceFileLoader(name,"website/" + name + ".py").load_module().replacedict(keys,DATABASE_PORT) #txt_keys = SourceFileLoader(name,"website/" + name + ".py").load_module().replacedict(keys,DATABASE_PORT)
try: try:
txt_keys,resources = SourceFileLoader(name,"website/" + name + ".py").load_module().instructions(keys) txt_keys,resources = SourceFileLoader(name,"website/" + name + ".py").load_module().instructions(keys)
except Exception as e: except Exception as e:
log("Error in website generation: " + str(sys.exc_info()),module="error") log("Error in website generation: " + str(sys.exc_info()),module="error")
raise raise
# add headers for server push # add headers for server push
for resource in resources: for resource in resources:
if all(ord(c) < 128 for c in resource["file"]): if all(ord(c) < 128 for c in resource["file"]):
# we can only put ascii stuff in the http header # we can only put ascii stuff in the http header
linkheaders.append("<" + resource["file"] + ">; rel=preload; as=" + resource["type"]) linkheaders.append("<" + resource["file"] + ">; rel=preload; as=" + resource["type"])
# apply key substitutions # apply key substitutions
for k in txt_keys: for k in txt_keys:
if isinstance(txt_keys[k],list): if isinstance(txt_keys[k],list):
# if list, we replace each occurence with the next item # if list, we replace each occurence with the next item
for element in txt_keys[k]: for element in txt_keys[k]:
html = html.replace(k,element,1) html = html.replace(k,element,1)
else: else:
html = html.replace(k,txt_keys[k]) html = html.replace(k,txt_keys[k])
response.set_header("Link",",".join(linkheaders)) response.set_header("Link",",".join(linkheaders))
return html return html
#return static_file("website/" + name + ".html",root="") #return static_file("website/" + name + ".html",root="")
#set graceful shutdown #set graceful shutdown
signal.signal(signal.SIGINT, graceful_exit) signal.signal(signal.SIGINT, graceful_exit)

156
website/artist.pyhp Normal file
View File

@ -0,0 +1,156 @@
<!DOCTYPE html>
<html>
<!-- query keys -->
<pyhp save="filterkeys.get('artist')" as="artist" />
<!-- DB calls -->
<pyhp save="db.artistInfo(artist)" as="info" />
<!-- preprocessing -->
<pyhp save="info.get('replace')" as="credited" />
<pyhp save="info.get('associated')" as="included" />
<pyhp save="'#' + str(info.get('position'))" as="positionstring" />
<pyhp save="urllib.parse.quote(artist)" as="encodedartist" />
<pyhp save="'months'" as="initialrange" />
<!-- includestring -->
<pyhp save="''" as="includestring" />
<pyhp if="credited is not None">
<pyhp save="'Competing under ' + credited + ' (' + positionstring + ')'" as="includestring" />
<pyhp save="''" as="positionstring" /> <!-- clear positionstring so it's not shown next to the artist -->
</pyhp>
<pyhp if="credited is None">
<pyhp save="artist" as="credited" />
</pyhp>
<pyhp if="included is not None and included != []">
<pyhp save="'associated: ' + ', '.join(included)" as="includestring" />
</pyhp>
<head>
<meta charset="UTF-8" />
<title>Maloja - <pyhp echo="artist" /></title>
<pyhp include="common/header.html" />
<script src="javascript/rangeselect.js"></script>
</head>
<body>
<table class="top_info">
<tr>
<td class="image">
<div style="background-image:url('{utilities.getArtistImage(artist=artist,fast=True)}');"></div>
</td>
<td class="text">
<h1><pyhp echo="artist" /></h1>
<span class="rank"><a href="/charts_artists?max=100"><pyhp echo="positionstring" /></a></span>
<br/>
<span><pyhp echo="includestring" /></span>
<p class="stats"><a href="/scrobbles?artist={encodedartist}"><pyhp echo="info['scrobbles']" /> Scrobbles</a></p>
<pyhp if="info.get('medals') is not None and 'gold' in info['medals']">
<pyhp for="y" in="info['medals']['gold']">
<a title="Best Artist in {y}" class="hidelink medal shiny gold" href='/charts_artists?max=50&amp;in=" + str(y) + "'><span><pyhp echo="y" /></span></a>
</pyhp>
</pyhp>
<pyhp if="info.get('medals') is not None and 'silver' in info['medals']">
<pyhp for="y" in="info['medals']['silver']">
<a title="Second Best Artist in {y}" class="hidelink medal shiny silver" href='/charts_artists?max=50&amp;in=" + str(y) + "'><span><pyhp echo="y" /></span></a>
</pyhp>
</pyhp>
<pyhp if="info.get('medals') is not None and 'bronze' in info['medals']">
<pyhp for="y" in="info['medals']['bronze']">
<a title="Third Best Artist in {y}" class="hidelink medal shiny bronze" href='/charts_artists?max=50&amp;in=" + str(y) + "'><span><pyhp echo="y" /></span></a>
</pyhp>
</pyhp>
</td>
</tr>
</table>
<h2><a href='/charts_tracks?artist={encodedartist}'>Top Tracks</a></h2>
<pyhp echo="htmlmodules.module_trackcharts(**filterkeys,max_=15)[0]" />
<br/>
<table class="twopart">
<pyhp save="[
('days','7 days',malojatime.today().next(-6),'day',7),
('weeks','12 weeks',malojatime.thisweek().next(-11),'week',12),
('months','12 months',malojatime.thismonth().next(-11),'month',12),
('years','10 years',malojatime.thisyear().next(-9),'year',12)
]" as="ranges" />
<tr>
<td>
<h2><a href='/pulse?artist={encodedartist}&amp;step=year&amp;trail=1'>Pulse</a></h2>
<pyhp for="range" in="ranges" separator = " | ">
<pyhp if="initialrange==range[0]">
<span onclick="showRange('pulse','{range[0]}')" class="stat_selector_pulse selector_pulse_{range[0]}" style="opacity:0.5;"><pyhp echo="range[1]" /></span>
</pyhp>
<pyhp if="initialrange!=range[0]">
<span onclick="showRange('pulse','{range[0]}')" class="stat_selector_pulse selector_pulse_{range[0]}"><pyhp echo="range[1]" /></span>
</pyhp>
</pyhp>
<br/><br/>
<pyhp for="range" in="ranges">
<pyhp if="initialrange==range[0]">
<span class="stat_module_pulse pulse_{range[0]}"><pyhp echo="htmlmodules.module_pulse(artist=artist,max_=range[4],since=range[2],step=range[3],trail=1)" /></span>
</pyhp>
<pyhp if="initialrange!=range[0]">
<span class="stat_module_pulse pulse_{range[0]}" style="display:none;"><pyhp echo="htmlmodules.module_pulse(artist=artist,max_=range[4],since=range[2],step=range[3],trail=1)" /></span>
</pyhp>
</pyhp>
</td>
<td>
<!-- We use the same classes / function calls here because we want it to switch together with pulse -->
<h2><a href='/performance?artist={encodedartist}&amp;step=year&amp;trail=1'>Performance</a></h2>
<pyhp for="range" in="ranges" separator = " | ">
<pyhp if="initialrange==range[0]">
<span onclick="showRange('pulse','{range[0]}')" class="stat_selector_pulse selector_pulse_{range[0]}" style="opacity:0.5;"><pyhp echo="range[1]" /></span>
</pyhp>
<pyhp if="initialrange!=range[0]">
<span onclick="showRange('pulse','{range[0]}')" class="stat_selector_pulse selector_pulse_{range[0]}"><pyhp echo="range[1]" /></span>
</pyhp>
</pyhp>
<br/><br/>
<pyhp for="range" in="ranges">
<pyhp if="initialrange==range[0]">
<span class="stat_module_pulse pulse_{range[0]}"><pyhp echo="htmlmodules.module_performance(artist=credited,max_=range[4],since=range[2],step=range[3],trail=1)" /></span>
</pyhp>
<pyhp if="initialrange!=range[0]">
<span class="stat_module_pulse pulse_{range[0]}" style="display:none;"><pyhp echo="htmlmodules.module_performance(artist=credited,max_=range[4],since=range[2],step=range[3],trail=1)" /></span>
</pyhp>
</pyhp>
</td>
</tr>
</table>
<h2><a href='/scrobbles?artist={encodedartist}'>Last Scrobbles</a></h2>
<pyhp echo="htmlmodules.module_scrobblelist(**filterkeys,max_=10,earlystop=True)[0]" />
<pyhp include="common/footer.html" />
</body>
</html>

View File

@ -1,3 +1,3 @@
<meta name="description" content='Maloja is a self-hosted music scrobble server.' /> <meta name="description" content='Maloja is a self-hosted music scrobble server.' />
<link rel="stylesheet" href="/css/maloja.css" /> <link rel="stylesheet" href="/css/maloja.css" />
<script src="/javascript/search.js" async></script> <script src="/javascript/search.js" async="yes"></script>