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

113 lines
3.7 KiB
Python
Raw Normal View History

2018-12-26 19:42:55 +03:00
def artistLink(name):
import urllib
return "<a href='/artist?artist=" + urllib.parse.quote(name) + "'>" + name + "</a>"
2018-12-26 21:20:26 +03:00
def artistLinks(artists):
return ", ".join([artistLink(a) for a in artists])
#def trackLink(artists,title):
def trackLink(track):
artists,title = track["artists"],track["title"]
import urllib
return "<a href='/track?title=" + urllib.parse.quote(title) + "&" + "&".join(["artist=" + urllib.parse.quote(a) for a in artists]) + "'>" + title + "</a>"
#def scrobblesTrackLink(artists,title,timekeys,amount=None,pixels=None):
2018-12-27 05:09:29 +03:00
def scrobblesTrackLink(track,timekeys,amount=None,percent=None):
2018-12-26 21:20:26 +03:00
artists,title = track["artists"],track["title"]
import urllib
2018-12-27 16:57:25 +03:00
inner = str(amount) if amount is not None else "<div style='width:" + str(percent) + "%;'></div>"
2018-12-26 21:20:26 +03:00
return "<a href='/scrobbles?" + "&".join(["artist=" + urllib.parse.quote(a) for a in artists]) + "&title=" + urllib.parse.quote(title) + "&" + keysToUrl(timekeys) + "'>" + inner + "</a>"
2018-12-27 05:09:29 +03:00
def scrobblesArtistLink(artist,timekeys,amount=None,percent=None,associated=False):
2018-12-26 21:20:26 +03:00
import urllib
2018-12-27 16:57:25 +03:00
inner = str(amount) if amount is not None else "<div style='width:" + str(percent) + "%;'></div>"
2018-12-26 21:20:26 +03:00
askey = "&associated" if associated else ""
return "<a href='/scrobbles?artist=" + urllib.parse.quote(artist) + "&" + keysToUrl(timekeys) + askey + "'>" + inner + "</a>"
2018-12-26 19:42:55 +03:00
# necessary because urllib.parse.urlencode doesnt handle multidicts
def keysToUrl(*dicts):
import urllib
st = ""
keys = removeIdentical(*dicts)
for k in keys:
values = keys.getall(k)
2018-12-26 21:20:26 +03:00
st += "&".join([urllib.parse.urlencode({k:v},safe="/") for v in values])
2018-12-26 19:42:55 +03:00
st += "&"
return st
def removeIdentical(*dicts):
from bottle import FormsDict
#combine multiple dicts
keys = FormsDict()
for d in dicts:
for k in d:
try: #multidicts
for v in d.getall(k):
keys.append(k,v)
except: #normaldicts
v = d.get(k)
keys.append(k,v)
new = FormsDict()
for k in keys:
values = set(keys.getall(k))
for v in values:
new.append(k,v)
return new
2019-02-03 18:52:37 +03:00
def getTimeDesc(timestamp,short=False):
2018-12-26 19:42:55 +03:00
import datetime
tim = datetime.datetime.utcfromtimestamp(timestamp)
2019-02-03 18:52:37 +03:00
if short:
2019-02-03 19:36:21 +03:00
now = datetime.datetime.now(tz=datetime.timezone.utc)
difference = int(now.timestamp() - timestamp)
if difference < 10: return "just now"
if difference < 60: return str(difference) + " seconds ago"
difference = int(difference/60)
if difference < 60: return str(difference) + " minutes ago" if difference>1 else str(difference) + " minute ago"
2019-02-03 19:36:21 +03:00
difference = int(difference/60)
if difference < 24: return str(difference) + " hours ago" if difference>1 else str(difference) + " hour ago"
2019-02-03 19:36:21 +03:00
difference = int(difference/24)
if difference < 5: return tim.strftime("%A")
if difference < 31: return str(difference) + " days ago" if difference>1 else str(difference) + " day ago"
2019-02-03 19:36:21 +03:00
#if difference < 300 and tim.year == now.year: return tim.strftime("%B")
#if difference < 300: return tim.strftime("%B %Y")
return tim.strftime("%d. %B %Y")
2019-02-03 18:52:37 +03:00
else:
return tim.strftime("%d. %b %Y %I:%M %p")
2018-12-26 19:42:55 +03:00
# limit a multidict to only the specified keys
# would be a simple constructor expression, but multidicts apparently don't let me do that
def pickKeys(d,*keys):
from bottle import FormsDict
2018-12-26 21:20:26 +03:00
if isinstance(d,dict):
2018-12-26 19:42:55 +03:00
return {k:d.get(k) for k in d if k in keys}
else:
# create a normal dictionary of lists
2018-12-26 21:20:26 +03:00
newd = {k:d.getall(k) for k in d if k in keys}
2018-12-26 19:42:55 +03:00
# one by one add the list entries to the formsdict
finald = FormsDict()
for k in newd:
for v in newd.get(k):
finald.append(k,v)
return finald
2018-12-26 21:20:26 +03:00
# removes all duplicate keys, except artists when a title is specified
def clean(d):
from bottle import FormsDict
if isinstance(d,dict):
return
else:
for k in d:
if (k != "artist") or "title" not in d:
d[k] = d.pop(k)