2019-02-20 20:22:45 +03:00
from htmlgenerators import *
import database
2019-03-12 13:39:36 +03:00
from utilities import getArtistImage , getTrackImage
2019-03-03 00:55:22 +03:00
from malojatime import *
2019-04-08 15:43:25 +03:00
from urihandler import compose_querystring , internal_to_uri , uri_to_internal
2019-02-20 23:10:58 +03:00
import urllib
2019-04-08 15:43:25 +03:00
import datetime
2019-04-11 11:49:56 +03:00
import math
2019-02-20 20:22:45 +03:00
2019-03-12 01:39:46 +03:00
#def getpictures(ls,result,tracks=False):
# from utilities import getArtistsInfo, getTracksInfo
# if tracks:
# for element in getTracksInfo(ls):
# result.append(element.get("image"))
# else:
# for element in getArtistsInfo(ls):
# result.append(element.get("image"))
2019-02-20 20:22:45 +03:00
2019-06-17 15:59:30 +03:00
#max_ indicates that no pagination should occur (because this is not the primary module)
def module_scrobblelist ( page = 0 , perpage = 100 , max_ = None , pictures = False , shortTimeDesc = False , earlystop = False , * * kwargs ) :
2019-03-14 13:07:20 +03:00
2019-02-20 20:22:45 +03:00
kwargs_filter = pickKeys ( kwargs , " artist " , " track " , " associated " )
2019-04-11 13:07:57 +03:00
kwargs_time = pickKeys ( kwargs , " timerange " , " since " , " to " , " within " )
2019-03-14 13:07:20 +03:00
2019-06-17 15:59:30 +03:00
if max_ is not None : perpage , page = max_ , 0
2019-06-17 15:28:16 +03:00
firstindex = page * perpage
lastindex = firstindex + perpage
2019-03-14 13:07:20 +03:00
2019-03-12 14:56:53 +03:00
# if earlystop, we don't care about the actual amount and only request as many from the db
# without, we request everything and filter on site
2019-06-17 15:28:16 +03:00
maxkey = { " max_ " : lastindex } if earlystop else { }
2019-03-12 14:56:53 +03:00
scrobbles = database . get_scrobbles ( * * kwargs_time , * * kwargs_filter , * * maxkey )
2019-02-20 20:22:45 +03:00
if pictures :
2019-06-17 15:28:16 +03:00
scrobbleswithpictures = [ " " ] * firstindex + scrobbles [ firstindex : lastindex ]
2019-02-20 23:10:58 +03:00
#scrobbleimages = [e.get("image") for e in getTracksInfo(scrobbleswithpictures)] #will still work with scrobble objects as they are a technically a subset of track objects
2019-03-12 01:39:46 +03:00
#scrobbleimages = ["/image?title=" + urllib.parse.quote(t["title"]) + "&" + "&".join(["artist=" + urllib.parse.quote(a) for a in t["artists"]]) for t in scrobbleswithpictures]
2019-03-12 13:39:36 +03:00
scrobbleimages = [ getTrackImage ( t [ " artists " ] , t [ " title " ] , fast = True ) for t in scrobbleswithpictures ]
2019-03-14 13:07:20 +03:00
2019-06-17 15:59:30 +03:00
pages = math . ceil ( len ( scrobbles ) / perpage )
2019-03-03 00:55:22 +03:00
representative = scrobbles [ 0 ] if len ( scrobbles ) is not 0 else None
2019-03-14 13:07:20 +03:00
2019-02-20 20:22:45 +03:00
# build list
i = 0
html = " <table class= ' list ' > "
for s in scrobbles :
2019-06-17 15:28:16 +03:00
if i < firstindex :
i + = 1
continue
2019-03-14 13:07:20 +03:00
2019-02-20 20:22:45 +03:00
html + = " <tr> "
2019-04-10 16:45:50 +03:00
html + = " <td class= ' time ' > " + timestamp_desc ( s [ " time " ] , short = shortTimeDesc ) + " </td> "
2019-02-20 20:22:45 +03:00
if pictures :
2019-04-07 15:07:50 +03:00
img = scrobbleimages [ i ]
else : img = None
html + = entity_column ( s , image = img )
2019-02-20 20:22:45 +03:00
html + = " </tr> "
2019-03-14 13:07:20 +03:00
2019-02-21 02:13:18 +03:00
i + = 1
2019-06-17 15:28:16 +03:00
if i > = lastindex :
2019-02-21 02:13:18 +03:00
break
2019-03-14 13:07:20 +03:00
2019-02-20 20:22:45 +03:00
html + = " </table> "
2019-03-14 13:07:20 +03:00
2019-06-17 16:45:00 +03:00
if max_ is None : html + = module_paginate ( page = page , pages = pages , perpage = perpage , * * kwargs )
2019-06-17 15:59:30 +03:00
2019-02-20 20:22:45 +03:00
return ( html , len ( scrobbles ) , representative )
2019-03-14 13:07:20 +03:00
2019-06-17 15:59:30 +03:00
def module_pulse ( page = 0 , perpage = 100 , max_ = None , * * kwargs ) :
2019-02-20 20:22:45 +03:00
2019-04-10 19:50:56 +03:00
from doreah . timing import clock , clockp
2019-02-20 20:22:45 +03:00
kwargs_filter = pickKeys ( kwargs , " artist " , " track " , " associated " )
2019-04-10 19:50:56 +03:00
kwargs_time = pickKeys ( kwargs , " since " , " to " , " within " , " timerange " , " step " , " stepn " , " trail " )
2019-06-17 15:59:30 +03:00
if max_ is not None : perpage , page = max_ , 0
2019-06-17 15:28:16 +03:00
firstindex = page * perpage
lastindex = firstindex + perpage
2019-03-14 13:07:20 +03:00
2019-02-20 20:22:45 +03:00
ranges = database . get_pulse ( * * kwargs_time , * * kwargs_filter )
2019-03-14 13:07:20 +03:00
2019-06-17 15:59:30 +03:00
pages = math . ceil ( len ( ranges ) / perpage )
2019-04-10 19:50:56 +03:00
2019-06-17 15:28:16 +03:00
ranges = ranges [ firstindex : lastindex ]
2019-03-14 13:07:20 +03:00
2019-03-03 00:55:22 +03:00
# if time range not explicitly specified, only show from first appearance
# if "since" not in kwargs:
# while ranges[0]["scrobbles"] == 0:
# del ranges[0]
2019-03-14 13:07:20 +03:00
2019-02-20 20:22:45 +03:00
maxbar = max ( [ t [ " scrobbles " ] for t in ranges ] )
maxbar = max ( maxbar , 1 )
2019-03-14 13:07:20 +03:00
2019-02-20 20:22:45 +03:00
#build list
html = " <table class= ' list ' > "
for t in ranges :
2019-04-10 16:45:50 +03:00
range = t [ " range " ]
2019-02-20 20:22:45 +03:00
html + = " <tr> "
2019-04-10 16:45:50 +03:00
html + = " <td> " + range . desc ( ) + " </td> "
2019-04-11 13:07:57 +03:00
html + = " <td class= ' amount ' > " + scrobblesLink ( range . urikeys ( ) , amount = t [ " scrobbles " ] , * * kwargs_filter ) + " </td> "
html + = " <td class= ' bar ' > " + scrobblesLink ( range . urikeys ( ) , percent = t [ " scrobbles " ] * 100 / maxbar , * * kwargs_filter ) + " </td> "
2019-02-20 20:22:45 +03:00
html + = " </tr> "
html + = " </table> "
2019-03-14 13:07:20 +03:00
2019-06-17 16:45:00 +03:00
if max_ is None : html + = module_paginate ( page = page , pages = pages , perpage = perpage , * * kwargs )
2019-03-14 13:07:20 +03:00
2019-02-20 20:22:45 +03:00
return html
2019-03-14 13:07:20 +03:00
2019-04-09 14:34:06 +03:00
2019-06-17 15:59:30 +03:00
def module_performance ( page = 0 , perpage = 100 , max_ = None , * * kwargs ) :
2019-04-09 14:34:06 +03:00
kwargs_filter = pickKeys ( kwargs , " artist " , " track " )
2019-04-11 13:07:57 +03:00
kwargs_time = pickKeys ( kwargs , " since " , " to " , " within " , " timerange " , " step " , " stepn " , " trail " )
2019-04-09 14:34:06 +03:00
2019-06-17 15:59:30 +03:00
if max_ is not None : perpage , page = max_ , 0
2019-06-17 15:28:16 +03:00
firstindex = page * perpage
lastindex = firstindex + perpage
2019-04-09 14:34:06 +03:00
ranges = database . get_performance ( * * kwargs_time , * * kwargs_filter )
2019-06-17 15:59:30 +03:00
pages = math . ceil ( len ( ranges ) / perpage )
2019-06-17 15:28:16 +03:00
ranges = ranges [ firstindex : lastindex ]
2019-04-09 14:34:06 +03:00
# if time range not explicitly specified, only show from first appearance
# if "since" not in kwargs:
# while ranges[0]["scrobbles"] == 0:
# del ranges[0]
minrank = 80
for t in ranges :
2019-05-01 12:29:28 +03:00
if t [ " rank " ] is not None and t [ " rank " ] + 20 > minrank : minrank = t [ " rank " ] + 20
2019-04-09 14:34:06 +03:00
#build list
html = " <table class= ' list ' > "
for t in ranges :
2019-04-10 16:45:50 +03:00
range = t [ " range " ]
2019-04-09 14:34:06 +03:00
html + = " <tr> "
2019-04-11 13:07:57 +03:00
html + = " <td> " + range . desc ( ) + " </td> "
2019-04-09 14:34:06 +03:00
html + = " <td class= ' rank ' > " + ( " # " + str ( t [ " rank " ] ) if t [ " rank " ] is not None else " No scrobbles " ) + " </td> "
prct = ( minrank + 1 - t [ " rank " ] ) * 100 / minrank if t [ " rank " ] is not None else 0
2019-04-11 13:07:57 +03:00
html + = " <td class= ' chart ' > " + rankLink ( range . urikeys ( ) , percent = prct , * * kwargs_filter , medal = t [ " rank " ] ) + " </td> "
2019-04-09 14:34:06 +03:00
html + = " </tr> "
html + = " </table> "
2019-06-17 16:45:00 +03:00
if max_ is None : html + = module_paginate ( page = page , pages = pages , perpage = perpage , * * kwargs )
2019-04-09 14:34:06 +03:00
return html
2019-06-17 15:59:30 +03:00
def module_trackcharts ( page = 0 , perpage = 100 , max_ = None , * * kwargs ) :
2019-02-20 20:22:45 +03:00
kwargs_filter = pickKeys ( kwargs , " artist " , " associated " )
2019-04-11 13:07:57 +03:00
kwargs_time = pickKeys ( kwargs , " timerange " , " since " , " to " , " within " )
2019-03-14 13:07:20 +03:00
2019-06-17 15:59:30 +03:00
if max_ is not None : perpage , page = max_ , 0
2019-06-17 15:28:16 +03:00
firstindex = page * perpage
lastindex = firstindex + perpage
2019-02-20 20:22:45 +03:00
tracks = database . get_charts_tracks ( * * kwargs_filter , * * kwargs_time )
2019-03-14 13:07:20 +03:00
2019-06-17 15:59:30 +03:00
pages = math . ceil ( len ( tracks ) / perpage )
2019-04-04 21:01:28 +03:00
# last time range (to compare)
2019-04-11 13:07:57 +03:00
try :
trackslast = database . get_charts_tracks ( * * kwargs_filter , timerange = kwargs_time [ " timerange " ] . next ( step = - 1 ) )
2019-04-04 21:01:28 +03:00
# create rank association
lastrank = { }
for tl in trackslast :
lastrank [ ( * tl [ " track " ] [ " artists " ] , tl [ " track " ] [ " title " ] ) ] = tl [ " rank " ]
for t in tracks :
try :
t [ " delta " ] = lastrank [ ( * t [ " track " ] [ " artists " ] , t [ " track " ] [ " title " ] ) ] - t [ " rank " ]
except :
2019-04-11 13:07:57 +03:00
t [ " delta " ] = math . inf
except :
pass
2019-04-04 21:01:28 +03:00
2019-02-20 20:22:45 +03:00
if tracks != [ ] :
maxbar = tracks [ 0 ] [ " scrobbles " ]
representative = tracks [ 0 ] [ " track " ]
else :
representative = None
2019-03-14 13:07:20 +03:00
2019-02-20 20:22:45 +03:00
i = 0
html = " <table class= ' list ' > "
for e in tracks :
2019-06-17 15:28:16 +03:00
if i < firstindex :
i + = 1
continue
2019-02-20 20:22:45 +03:00
i + = 1
2019-06-17 15:28:16 +03:00
if i > lastindex :
2019-02-20 20:22:45 +03:00
break
html + = " <tr> "
2019-04-04 21:01:28 +03:00
# rank
2019-06-17 15:28:16 +03:00
if i == firstindex + 1 or e [ " scrobbles " ] < prev [ " scrobbles " ] :
html + = " <td class= ' rank ' ># " + str ( e [ " rank " ] ) + " </td> "
2019-03-14 13:07:20 +03:00
else :
html + = " <td class= ' rank ' ></td> "
2019-04-04 21:01:28 +03:00
# rank change
2019-04-11 13:07:57 +03:00
if e . get ( " delta " ) is None :
pass
elif e [ " delta " ] is math . inf :
2019-04-04 21:01:28 +03:00
html + = " <td class= ' rankup ' title= ' New ' >🆕</td> "
elif e [ " delta " ] > 0 :
html + = " <td class= ' rankup ' title= ' up from # " + str ( e [ " rank " ] + e [ " delta " ] ) + " ' >↗</td> "
elif e [ " delta " ] < 0 :
html + = " <td class= ' rankdown ' title= ' down from # " + str ( e [ " rank " ] + e [ " delta " ] ) + " ' >↘</td> "
else :
html + = " <td class= ' ranksame ' title= ' Unchanged ' >➡</td> "
# track
2019-04-07 15:07:50 +03:00
html + = entity_column ( e [ " track " ] )
2019-04-04 21:01:28 +03:00
# scrobbles
2019-04-08 14:04:31 +03:00
html + = " <td class= ' amount ' > " + scrobblesTrackLink ( e [ " track " ] , internal_to_uri ( kwargs_time ) , amount = e [ " scrobbles " ] ) + " </td> "
html + = " <td class= ' bar ' > " + scrobblesTrackLink ( e [ " track " ] , internal_to_uri ( kwargs_time ) , percent = e [ " scrobbles " ] * 100 / maxbar ) + " </td> "
2019-02-20 20:22:45 +03:00
html + = " </tr> "
2019-03-14 13:07:20 +03:00
prev = e
2019-02-20 20:22:45 +03:00
html + = " </table> "
2019-03-14 13:07:20 +03:00
2019-06-17 16:45:00 +03:00
if max_ is None : html + = module_paginate ( page = page , pages = pages , perpage = perpage , * * kwargs )
2019-06-17 15:59:30 +03:00
2019-02-20 20:22:45 +03:00
return ( html , representative )
2019-03-14 13:07:20 +03:00
2019-06-17 15:59:30 +03:00
def module_artistcharts ( page = 0 , perpage = 100 , max_ = None , * * kwargs ) :
2019-02-20 20:22:45 +03:00
kwargs_filter = pickKeys ( kwargs , " associated " ) #not used right now
2019-04-11 13:07:57 +03:00
kwargs_time = pickKeys ( kwargs , " timerange " , " since " , " to " , " within " )
2019-03-14 13:07:20 +03:00
2019-06-17 15:59:30 +03:00
if max_ is not None : perpage , page = max_ , 0
2019-06-17 15:28:16 +03:00
firstindex = page * perpage
lastindex = firstindex + perpage
2019-02-20 20:22:45 +03:00
artists = database . get_charts_artists ( * * kwargs_filter , * * kwargs_time )
2019-03-14 13:07:20 +03:00
2019-06-17 15:59:30 +03:00
pages = math . ceil ( len ( artists ) / perpage )
2019-06-17 16:24:11 +03:00
2019-04-04 21:01:28 +03:00
# last time range (to compare)
2019-04-11 13:07:57 +03:00
try :
#from malojatime import _get_next
artistslast = database . get_charts_artists ( * * kwargs_filter , timerange = kwargs_time [ " timerange " ] . next ( step = - 1 ) )
2019-04-04 21:01:28 +03:00
# create rank association
lastrank = { }
for al in artistslast :
lastrank [ al [ " artist " ] ] = al [ " rank " ]
for a in artists :
try :
a [ " delta " ] = lastrank [ a [ " artist " ] ] - a [ " rank " ]
except :
2019-04-11 13:07:57 +03:00
a [ " delta " ] = math . inf
except :
pass
2019-03-14 13:07:20 +03:00
2019-02-20 20:22:45 +03:00
if artists != [ ] :
maxbar = artists [ 0 ] [ " scrobbles " ]
representative = artists [ 0 ] [ " artist " ]
else :
representative = None
2019-03-14 13:07:20 +03:00
2019-02-20 20:22:45 +03:00
i = 0
html = " <table class= ' list ' > "
for e in artists :
2019-06-17 15:28:16 +03:00
if i < firstindex :
i + = 1
continue
2019-02-20 20:22:45 +03:00
i + = 1
2019-06-17 15:28:16 +03:00
if i > lastindex :
2019-02-20 20:22:45 +03:00
break
html + = " <tr> "
2019-04-04 21:01:28 +03:00
# rank
2019-06-17 15:28:16 +03:00
if i == firstindex + 1 or e [ " scrobbles " ] < prev [ " scrobbles " ] :
html + = " <td class= ' rank ' ># " + str ( e [ " rank " ] ) + " </td> "
2019-03-14 13:07:20 +03:00
else :
html + = " <td class= ' rank ' ></td> "
2019-04-04 21:01:28 +03:00
# rank change
2019-04-11 13:07:57 +03:00
#if "within" not in kwargs_time: pass
if e . get ( " delta " ) is None :
pass
elif e [ " delta " ] is math . inf :
2019-04-04 21:01:28 +03:00
html + = " <td class= ' rankup ' title= ' New ' >🆕</td> "
elif e [ " delta " ] > 0 :
html + = " <td class= ' rankup ' title= ' up from # " + str ( e [ " rank " ] + e [ " delta " ] ) + " ' >↗</td> "
elif e [ " delta " ] < 0 :
html + = " <td class= ' rankdown ' title= ' down from # " + str ( e [ " rank " ] + e [ " delta " ] ) + " ' >↘</td> "
else :
html + = " <td class= ' ranksame ' title= ' Unchanged ' >➡</td> "
# artist
2019-04-07 15:07:50 +03:00
html + = entity_column ( e [ " artist " ] , counting = e [ " counting " ] )
2019-04-04 21:01:28 +03:00
# scrobbles
2019-04-08 14:04:31 +03:00
html + = " <td class= ' amount ' > " + scrobblesArtistLink ( e [ " artist " ] , internal_to_uri ( kwargs_time ) , amount = e [ " scrobbles " ] , associated = True ) + " </td> "
html + = " <td class= ' bar ' > " + scrobblesArtistLink ( e [ " artist " ] , internal_to_uri ( kwargs_time ) , percent = e [ " scrobbles " ] * 100 / maxbar , associated = True ) + " </td> "
2019-02-20 20:22:45 +03:00
html + = " </tr> "
2019-03-14 13:07:20 +03:00
prev = e
2019-02-20 20:22:45 +03:00
html + = " </table> "
2019-03-14 13:07:20 +03:00
2019-06-17 16:45:00 +03:00
if max_ is None : html + = module_paginate ( page = page , pages = pages , perpage = perpage , * * kwargs )
2019-06-17 15:59:30 +03:00
2019-02-20 20:22:45 +03:00
return ( html , representative )
2019-03-14 13:07:20 +03:00
2019-04-02 15:17:19 +03:00
2019-04-02 15:41:18 +03:00
def module_toptracks ( pictures = True , * * kwargs ) :
2019-04-02 15:17:19 +03:00
kwargs_filter = pickKeys ( kwargs , " artist " , " associated " )
2019-04-11 13:07:57 +03:00
kwargs_time = pickKeys ( kwargs , " timerange " , " since " , " to " , " within " , " step " , " stepn " , " trail " )
2019-04-02 15:17:19 +03:00
tracks = database . get_top_tracks ( * * kwargs_filter , * * kwargs_time )
if tracks != [ ] :
maxbar = max ( t [ " scrobbles " ] for t in tracks )
2019-04-02 16:27:22 +03:00
# track with most #1 positions
max_appear = 0
representatives = list ( t [ " track " ] for t in tracks if t [ " track " ] is not None )
for t in representatives :
max_appear = max ( max_appear , representatives . count ( t ) )
#representatives.sort(key=lambda reftrack:len([t for t in tracks if t["track"] == reftrack["track"] and t["track"] is not None]))
representatives = [ t for t in tracks if representatives . count ( t [ " track " ] ) == max_appear ]
# of these, track with highest scrobbles in its #1 range
representatives . sort ( key = lambda t : t [ " scrobbles " ] )
representative = representatives [ - 1 ] [ " track " ]
2019-04-02 15:17:19 +03:00
else :
representative = None
i = 0
html = " <table class= ' list ' > "
for e in tracks :
2019-04-08 14:04:31 +03:00
#fromstr = "/".join([str(p) for p in e["from"]])
#tostr = "/".join([str(p) for p in e["to"]])
2019-04-10 16:45:50 +03:00
range = e [ " range " ]
2019-04-02 15:17:19 +03:00
i + = 1
html + = " <tr> "
2019-04-10 16:45:50 +03:00
html + = " <td> " + range . desc ( ) + " </td> "
2019-04-02 15:17:19 +03:00
if e [ " track " ] is None :
2019-04-02 15:41:18 +03:00
if pictures :
html + = " <td><div></div></td> "
2019-04-02 15:17:19 +03:00
html + = " <td class= ' stats ' > " + " No scrobbles " + " </td> "
2019-06-13 18:41:42 +03:00
#html += "<td>" + "" + "</td>"
2019-04-02 15:17:19 +03:00
html + = " <td class= ' amount ' > " + " 0 " + " </td> "
html + = " <td class= ' bar ' > " + " " + " </td> "
else :
2019-04-02 15:41:18 +03:00
if pictures :
2019-04-07 15:07:50 +03:00
img = getTrackImage ( e [ " track " ] [ " artists " ] , e [ " track " ] [ " title " ] , fast = True )
else : img = None
html + = entity_column ( e [ " track " ] , image = img )
2019-04-11 13:07:57 +03:00
html + = " <td class= ' amount ' > " + scrobblesTrackLink ( e [ " track " ] , range . urikeys ( ) , amount = e [ " scrobbles " ] ) + " </td> "
html + = " <td class= ' bar ' > " + scrobblesTrackLink ( e [ " track " ] , range . urikeys ( ) , percent = e [ " scrobbles " ] * 100 / maxbar ) + " </td> "
2019-04-02 15:17:19 +03:00
html + = " </tr> "
prev = e
html + = " </table> "
return ( html , representative )
2019-04-02 15:41:18 +03:00
def module_topartists ( pictures = True , * * kwargs ) :
2019-04-02 15:17:19 +03:00
2019-04-11 13:07:57 +03:00
kwargs_time = pickKeys ( kwargs , " timerange " , " since " , " to " , " within " , " step " , " stepn " , " trail " )
2019-04-02 15:17:19 +03:00
artists = database . get_top_artists ( * * kwargs_time )
if artists != [ ] :
maxbar = max ( a [ " scrobbles " ] for a in artists )
2019-04-02 16:27:22 +03:00
# artists with most #1 positions
max_appear = 0
representatives = list ( a [ " artist " ] for a in artists if a [ " artist " ] is not None )
for a in representatives :
max_appear = max ( max_appear , representatives . count ( a ) )
representatives = [ a for a in artists if representatives . count ( a [ " artist " ] ) == max_appear ]
# of these, artist with highest scrobbles in their #1 range
representatives . sort ( key = lambda a : a [ " scrobbles " ] )
representative = representatives [ - 1 ] [ " artist " ]
2019-04-02 15:17:19 +03:00
else :
representative = None
i = 0
html = " <table class= ' list ' > "
for e in artists :
2019-04-08 14:04:31 +03:00
#fromstr = "/".join([str(p) for p in e["from"]])
#tostr = "/".join([str(p) for p in e["to"]])
2019-04-10 16:45:50 +03:00
range = e [ " range " ]
2019-04-02 15:17:19 +03:00
i + = 1
html + = " <tr> "
2019-04-10 16:45:50 +03:00
html + = " <td> " + range . desc ( ) + " </td> "
2019-04-02 15:41:18 +03:00
2019-04-02 15:17:19 +03:00
if e [ " artist " ] is None :
2019-04-02 15:41:18 +03:00
if pictures :
html + = " <td><div></div></td> "
2019-04-02 15:17:19 +03:00
html + = " <td class= ' stats ' > " + " No scrobbles " + " </td> "
html + = " <td class= ' amount ' > " + " 0 " + " </td> "
html + = " <td class= ' bar ' > " + " " + " </td> "
else :
2019-04-02 15:41:18 +03:00
if pictures :
2019-04-07 15:07:50 +03:00
img = getArtistImage ( e [ " artist " ] , fast = True )
else : img = None
html + = entity_column ( e [ " artist " ] , image = img )
2019-04-11 13:07:57 +03:00
html + = " <td class= ' amount ' > " + scrobblesArtistLink ( e [ " artist " ] , range . urikeys ( ) , amount = e [ " scrobbles " ] , associated = True ) + " </td> "
html + = " <td class= ' bar ' > " + scrobblesArtistLink ( e [ " artist " ] , range . urikeys ( ) , percent = e [ " scrobbles " ] * 100 / maxbar , associated = True ) + " </td> "
2019-04-02 15:17:19 +03:00
html + = " </tr> "
prev = e
html + = " </table> "
return ( html , representative )
2019-03-12 01:39:46 +03:00
def module_artistcharts_tiles ( * * kwargs ) :
2019-03-03 23:22:42 +03:00
kwargs_filter = pickKeys ( kwargs , " associated " ) #not used right now
2019-04-11 13:07:57 +03:00
kwargs_time = pickKeys ( kwargs , " timerange " , " since " , " to " , " within " )
2019-03-14 13:07:20 +03:00
2019-03-03 23:22:42 +03:00
artists = database . get_charts_artists ( * * kwargs_filter , * * kwargs_time ) [ : 14 ]
while len ( artists ) < 14 : artists . append ( None )
2019-03-14 13:07:20 +03:00
2019-03-03 23:22:42 +03:00
i = 1
2019-03-14 13:07:20 +03:00
2019-03-03 23:22:42 +03:00
bigpart = [ 0 , 1 , 2 , 6 , 15 ]
smallpart = [ 0 , 1 , 2 , 4 , 6 , 9 , 12 , 15 ]
2019-04-04 21:15:48 +03:00
#rnk = (0,0) #temporary store so entries with the same scrobble amount get the same rank
2019-03-14 13:07:20 +03:00
2019-03-03 23:22:42 +03:00
html = """ <table class= " tiles_top " ><tr> """
2019-03-14 13:07:20 +03:00
2019-03-03 23:22:42 +03:00
for e in artists :
2019-03-14 13:07:20 +03:00
2019-03-03 23:22:42 +03:00
if i in bigpart :
n = bigpart . index ( i )
html + = """ <td><table class= " tiles_ """ + str ( n ) + """ x """ + str ( n ) + """ tiles_sub " > """
2019-03-14 13:07:20 +03:00
2019-03-03 23:22:42 +03:00
if i in smallpart :
html + = " <tr> "
2019-03-14 13:07:20 +03:00
2019-03-03 23:55:35 +03:00
if e is not None :
2019-04-09 12:35:02 +03:00
html + = " <td onclick= ' window.location.href= \" " \
+ link_address ( e [ " artist " ] ) \
+ " \" ' style= ' cursor:pointer;background-image:url( \" " + getArtistImage ( e [ " artist " ] , fast = True ) + " \" ); ' > " \
+ " <span class= ' stats ' > " + " # " + str ( e [ " rank " ] ) + " </span> <span> " + html_link ( e [ " artist " ] ) + " </span></td> "
2019-03-03 23:55:35 +03:00
else :
2019-04-09 12:35:02 +03:00
html + = " <td><span class= ' stats ' ></span> <span></span></td> "
2019-03-14 13:07:20 +03:00
2019-03-03 23:22:42 +03:00
i + = 1
2019-03-14 13:07:20 +03:00
2019-03-03 23:22:42 +03:00
if i in smallpart :
html + = " </tr> "
2019-03-14 13:07:20 +03:00
2019-03-03 23:22:42 +03:00
if i in bigpart :
html + = " </table></td> "
2019-03-14 13:07:20 +03:00
2019-03-03 23:22:42 +03:00
html + = """ </tr></table> """
2019-03-14 13:07:20 +03:00
2019-03-03 23:22:42 +03:00
return html
2019-03-14 13:07:20 +03:00
2019-03-12 01:39:46 +03:00
def module_trackcharts_tiles ( * * kwargs ) :
2019-03-03 23:22:42 +03:00
kwargs_filter = pickKeys ( kwargs , " artist " , " associated " )
2019-04-11 13:07:57 +03:00
kwargs_time = pickKeys ( kwargs , " timerange " , " since " , " to " , " within " )
2019-03-14 13:07:20 +03:00
2019-03-03 23:22:42 +03:00
tracks = database . get_charts_tracks ( * * kwargs_filter , * * kwargs_time ) [ : 14 ]
while len ( tracks ) < 14 : tracks . append ( None ) #{"track":{"title":"","artists":[]}}
2019-03-14 13:07:20 +03:00
2019-03-03 23:22:42 +03:00
i = 1
2019-03-14 13:07:20 +03:00
2019-03-03 23:22:42 +03:00
bigpart = [ 0 , 1 , 2 , 6 , 15 ]
smallpart = [ 0 , 1 , 2 , 4 , 6 , 9 , 12 , 15 ]
2019-04-04 21:15:48 +03:00
#rnk = (0,0) #temporary store so entries with the same scrobble amount get the same rank
2019-03-14 13:07:20 +03:00
2019-03-03 23:22:42 +03:00
html = """ <table class= " tiles_top " ><tr> """
2019-03-14 13:07:20 +03:00
2019-03-03 23:22:42 +03:00
for e in tracks :
2019-03-14 13:07:20 +03:00
2019-03-03 23:22:42 +03:00
if i in bigpart :
n = bigpart . index ( i )
html + = """ <td><table class= " tiles_ """ + str ( n ) + """ x """ + str ( n ) + """ tiles_sub " > """
2019-03-14 13:07:20 +03:00
2019-03-03 23:22:42 +03:00
if i in smallpart :
html + = " <tr> "
2019-03-14 13:07:20 +03:00
2019-03-03 23:55:35 +03:00
if e is not None :
2019-04-09 12:35:02 +03:00
html + = " <td onclick= ' window.location.href= \" " \
+ link_address ( e [ " track " ] ) \
+ " \" ' style= ' cursor:pointer;background-image:url( \" " + getTrackImage ( e [ " track " ] [ " artists " ] , e [ " track " ] [ " title " ] , fast = True ) + " \" ); ' > " \
+ " <span class= ' stats ' > " + " # " + str ( e [ " rank " ] ) + " </span> <span> " + html_link ( e [ " track " ] ) + " </span></td> "
2019-03-03 23:55:35 +03:00
else :
2019-04-09 12:35:02 +03:00
html + = " <td><span class= ' stats ' ></span> <span></span></td> "
2019-03-14 13:07:20 +03:00
2019-03-03 23:22:42 +03:00
i + = 1
2019-03-14 13:07:20 +03:00
2019-03-03 23:22:42 +03:00
if i in smallpart :
html + = " </tr> "
2019-03-14 13:07:20 +03:00
2019-03-03 23:22:42 +03:00
if i in bigpart :
html + = " </table></td> "
2019-03-14 13:07:20 +03:00
2019-03-03 23:22:42 +03:00
html + = """ </tr></table> """
2019-03-14 13:07:20 +03:00
return html
2019-04-01 19:15:08 +03:00
2019-06-17 15:59:30 +03:00
2019-06-17 16:45:00 +03:00
def module_paginate ( page , pages , perpage , * * keys ) :
2019-06-17 15:59:30 +03:00
2019-06-17 16:45:00 +03:00
unchangedkeys = internal_to_uri ( { * * keys , " perpage " : perpage } )
2019-06-17 15:59:30 +03:00
html = " <div class= ' paginate ' > "
if page > 1 :
html + = " <a href= ' ? " + compose_querystring ( unchangedkeys , internal_to_uri ( { " page " : 0 } ) ) + " ' ><span class= ' stat_selector ' > " + " 1 " + " </span></a> "
html + = " | "
if page > 2 :
html + = " ... | "
if page > 0 :
html + = " <a href= ' ? " + compose_querystring ( unchangedkeys , internal_to_uri ( { " page " : page - 1 } ) ) + " ' ><span class= ' stat_selector ' > " + str ( page ) + " </span></a> "
html + = " « "
html + = " <span style= ' opacity:0.5; ' class= ' stat_selector ' > " + str ( page + 1 ) + " </span> "
if page < pages - 1 :
html + = " » "
html + = " <a href= ' ? " + compose_querystring ( unchangedkeys , internal_to_uri ( { " page " : page + 1 } ) ) + " ' ><span class= ' stat_selector ' > " + str ( page + 2 ) + " </span></a> "
if page < pages - 3 :
html + = " | ... "
if page < pages - 2 :
html + = " | "
html + = " <a href= ' ? " + compose_querystring ( unchangedkeys , internal_to_uri ( { " page " : pages - 1 } ) ) + " ' ><span class= ' stat_selector ' > " + str ( pages ) + " </span></a> "
html + = " </div> "
return html
2019-04-08 14:04:31 +03:00
# THIS FUNCTION USES THE ORIGINAL URI KEYS!!!
2019-04-01 19:43:08 +03:00
def module_filterselection ( keys , time = True , delimit = False ) :
2019-04-01 19:15:08 +03:00
2019-06-26 20:13:02 +03:00
from malojatime import today , thisweek , thismonth , thisyear , alltime
2019-04-08 15:43:25 +03:00
2019-06-26 20:13:02 +03:00
filterkeys , timekeys , delimitkeys , extrakeys = uri_to_internal ( keys )
2019-05-14 19:06:34 +03:00
# drop keys that are not relevant so they don't clutter the URI
if not time : timekeys = { }
if not delimit : delimitkeys = { }
2019-07-25 18:35:03 +03:00
if " page " in extrakeys : del extrakeys [ " page " ]
2019-06-26 20:13:02 +03:00
internalkeys = { * * filterkeys , * * timekeys , * * delimitkeys , * * extrakeys }
2019-04-08 15:43:25 +03:00
2019-04-01 19:43:08 +03:00
html = " "
2019-04-01 19:15:08 +03:00
2019-06-17 15:59:30 +03:00
2019-04-01 19:43:08 +03:00
if time :
2019-04-02 12:40:02 +03:00
# wonky selector for precise date range
# fromdate = start_of_scrobbling()
# todate = end_of_scrobbling()
# if keys.get("since") is not None: fromdate = keys.get("since")
# if keys.get("to") is not None: todate = keys.get("to")
# if keys.get("in") is not None: fromdate, todate = keys.get("in"), keys.get("in")
# fromdate = time_fix(fromdate)
# todate = time_fix(todate)
# fromdate, todate = time_pad(fromdate,todate,full=True)
# fromdate = [str(e) if e>9 else "0" + str(e) for e in fromdate]
# todate = [str(e) if e>9 else "0" + str(e) for e in todate]
#
# html += "<div>"
# html += "from <input id='dateselect_from' onchange='datechange()' type='date' value='" + "-".join(fromdate) + "'/> "
# html += "to <input id='dateselect_to' onchange='datechange()' type='date' value='" + "-".join(todate) + "'/>"
# html += "</div>"
2019-04-26 18:05:29 +03:00
# relative to current range
html + = " <div> "
2019-06-26 20:13:02 +03:00
thisrange = timekeys . get ( " timerange " )
prevrange = thisrange . next ( - 1 )
nextrange = thisrange . next ( 1 )
if prevrange is not None :
link = compose_querystring ( internal_to_uri ( { * * internalkeys , " timerange " : prevrange } ) )
html + = " <a href= ' ? " + link + " ' ><span class= ' stat_selector ' > " + prevrange . desc ( ) + " </span></a> "
2019-04-26 18:07:14 +03:00
html + = " « "
2019-06-26 20:13:02 +03:00
if prevrange is not None or nextrange is not None :
html + = " <span class= ' stat_selector ' style= ' opacity:0.5; ' > " + thisrange . desc ( ) + " </span> "
if nextrange is not None :
2019-04-26 18:07:14 +03:00
html + = " » "
2019-06-26 20:13:02 +03:00
link = compose_querystring ( internal_to_uri ( { * * internalkeys , " timerange " : nextrange } ) )
html + = " <a href= ' ? " + link + " ' ><span class= ' stat_selector ' > " + nextrange . desc ( ) + " </span></a> "
html + = " </div> "
2019-04-26 18:05:29 +03:00
2019-06-26 18:59:23 +03:00
categories = [
{
" active " : time ,
" options " : {
" Today " : { " timerange " : today ( ) } ,
" This Week " : { " timerange " : thisweek ( ) } ,
" This Month " : { " timerange " : thismonth ( ) } ,
" This Year " : { " timerange " : thisyear ( ) } ,
" All Time " : { " timerange " : alltime ( ) }
}
} ,
{
" active " : delimit ,
" options " : {
" Daily " : { " step " : " day " , " stepn " : 1 } ,
" Weekly " : { " step " : " week " , " stepn " : 1 } ,
" Fortnightly " : { " step " : " week " , " stepn " : 2 } ,
" Monthly " : { " step " : " month " , " stepn " : 1 } ,
" Quarterly " : { " step " : " month " , " stepn " : 3 } ,
" Yearly " : { " step " : " year " , " stepn " : 1 }
}
} ,
{
" active " : delimit ,
" options " : {
" Standard " : { " trail " : 1 } ,
" Trailing " : { " trail " : 2 } ,
" Long Trailing " : { " trail " : 3 } ,
" Inert " : { " trail " : 10 } ,
" Cumulative " : { " trail " : math . inf }
}
2019-06-26 18:11:46 +03:00
}
2019-04-26 18:05:29 +03:00
2019-06-26 18:59:23 +03:00
]
2019-04-01 19:43:08 +03:00
2019-06-26 18:59:23 +03:00
for c in categories :
2019-04-01 19:43:08 +03:00
2019-06-26 18:59:23 +03:00
if c [ " active " ] :
2019-06-26 18:11:46 +03:00
2019-06-26 18:59:23 +03:00
optionlist = [ ]
for option in c [ " options " ] :
values = c [ " options " ] [ option ]
link = " ? " + compose_querystring ( internal_to_uri ( { * * internalkeys , * * values } ) )
2019-06-26 18:11:46 +03:00
2019-06-26 18:59:23 +03:00
if all ( internalkeys . get ( k ) == values [ k ] for k in values ) :
optionlist . append ( " <span class= ' stat_selector ' style= ' opacity:0.5; ' > " + option + " </span> " )
else :
optionlist . append ( " <a href= ' " + link + " ' ><span class= ' stat_selector ' > " + option + " </span></a> " )
2019-06-26 18:11:46 +03:00
2019-06-26 18:59:23 +03:00
html + = " <div> " + " | " . join ( optionlist ) + " </div> "
2019-04-01 19:15:08 +03:00
return html