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-02-20 23:10:58 +03:00
import urllib
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
# artist=None,track=None,since=None,to=None,within=None,associated=False,max_=None,pictures=False
2019-03-12 14:56:53 +03:00
def module_scrobblelist ( 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 " )
kwargs_time = pickKeys ( kwargs , " since " , " to " , " within " )
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
maxkey = { " max_ " : max_ } if earlystop else { }
scrobbles = database . get_scrobbles ( * * kwargs_time , * * kwargs_filter , * * maxkey )
2019-02-20 20:22:45 +03:00
if pictures :
scrobbleswithpictures = scrobbles if max_ is None else scrobbles [ : max_ ]
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-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-03-14 13:07:20 +03:00
2019-02-20 20:22:45 +03:00
html + = " <tr> "
2019-03-03 00:55:22 +03:00
html + = " <td class= ' time ' > " + time_desc ( s [ " time " ] , short = shortTimeDesc ) + " </td> "
2019-02-20 20:22:45 +03:00
if pictures :
html + = """ <td class= ' icon ' ><div style= " background-image:url( ' """ + scrobbleimages [ i ] + """ ' ) " ></div></td> """
html + = " <td class= ' artists ' > " + artistLinks ( s [ " artists " ] ) + " </td> "
html + = " <td class= ' title ' > " + trackLink ( { " artists " : s [ " artists " ] , " title " : s [ " title " ] } ) + " </td> "
2019-03-10 19:38:33 +03:00
# Alternative way: Do it in one cell
#html += "<td class='title'><span>" + artistLinks(s["artists"]) + "</span> — " + trackLink({"artists":s["artists"],"title":s["title"]}) + "</td>"
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
if max_ is not None and i > = max_ :
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-02-20 20:22:45 +03:00
return ( html , len ( scrobbles ) , representative )
2019-03-14 13:07:20 +03:00
2019-02-20 20:22:45 +03:00
def module_pulse ( max_ = None , * * kwargs ) :
kwargs_filter = pickKeys ( kwargs , " artist " , " track " , " associated " )
kwargs_time = pickKeys ( kwargs , " since " , " to " , " within " , " step " , " stepn " , " trail " )
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-03-03 00:55:22 +03:00
if max_ is not None : ranges = ranges [ : max_ ]
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 :
fromstr = " / " . join ( [ str ( e ) for e in t [ " from " ] ] )
tostr = " / " . join ( [ str ( e ) for e in t [ " to " ] ] )
html + = " <tr> "
2019-03-03 00:55:22 +03:00
html + = " <td> " + range_desc ( t [ " from " ] , t [ " to " ] , short = True ) + " </td> "
2019-02-20 20:22:45 +03:00
html + = " <td class= ' amount ' > " + scrobblesLink ( { " since " : fromstr , " to " : tostr } , amount = t [ " scrobbles " ] , * * kwargs_filter ) + " </td> "
html + = " <td class= ' bar ' > " + scrobblesLink ( { " since " : fromstr , " to " : tostr } , percent = t [ " scrobbles " ] * 100 / maxbar , * * kwargs_filter ) + " </td> "
html + = " </tr> "
html + = " </table> "
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-02-20 20:22:45 +03:00
def module_trackcharts ( max_ = None , * * kwargs ) :
kwargs_filter = pickKeys ( kwargs , " artist " , " associated " )
kwargs_time = pickKeys ( kwargs , " since " , " to " , " within " )
2019-03-14 13:07:20 +03:00
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-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 :
i + = 1
if max_ is not None and i > max_ :
break
html + = " <tr> "
2019-03-14 13:07:20 +03:00
if i == 1 or e [ " scrobbles " ] < prev [ " scrobbles " ] :
html + = " <td class= ' rank ' ># " + str ( i ) + " </td> "
else :
html + = " <td class= ' rank ' ></td> "
2019-02-20 20:22:45 +03:00
html + = " <td class= ' artists ' > " + artistLinks ( e [ " track " ] [ " artists " ] ) + " </td> "
html + = " <td class= ' title ' > " + trackLink ( e [ " track " ] ) + " </td> "
html + = " <td class= ' amount ' > " + scrobblesTrackLink ( e [ " track " ] , kwargs_time , amount = e [ " scrobbles " ] ) + " </td> "
html + = " <td class= ' bar ' > " + scrobblesTrackLink ( e [ " track " ] , kwargs_time , percent = e [ " scrobbles " ] * 100 / maxbar ) + " </td> "
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-02-20 20:22:45 +03:00
return ( html , representative )
2019-03-14 13:07:20 +03:00
2019-02-20 20:22:45 +03:00
def module_artistcharts ( max_ = None , * * kwargs ) :
kwargs_filter = pickKeys ( kwargs , " associated " ) #not used right now
kwargs_time = pickKeys ( kwargs , " since " , " to " , " within " )
2019-03-14 13:07:20 +03:00
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-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 :
i + = 1
if max_ is not None and i > max_ :
break
html + = " <tr> "
2019-03-14 13:07:20 +03:00
if i == 1 or e [ " scrobbles " ] < prev [ " scrobbles " ] :
html + = " <td class= ' rank ' ># " + str ( i ) + " </td> "
else :
html + = " <td class= ' rank ' ></td> "
2019-02-20 20:22:45 +03:00
html + = " <td class= ' artist ' > " + artistLink ( e [ " artist " ] )
if ( e [ " counting " ] != [ ] ) :
html + = " <span class= ' extra ' >incl. " + " , " . join ( [ artistLink ( a ) for a in e [ " counting " ] ] ) + " </span> "
html + = " </td> "
html + = " <td class= ' amount ' > " + scrobblesArtistLink ( e [ " artist " ] , kwargs_time , amount = e [ " scrobbles " ] , associated = True ) + " </td> "
html + = " <td class= ' bar ' > " + scrobblesArtistLink ( e [ " artist " ] , kwargs_time , percent = e [ " scrobbles " ] * 100 / maxbar , associated = True ) + " </td> "
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-02-20 20:22:45 +03:00
return ( html , representative )
2019-03-14 13:07:20 +03:00
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
kwargs_time = pickKeys ( kwargs , " 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-03-03 23:55:35 +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 :
rank = i if e [ " scrobbles " ] != rnk [ 1 ] else rnk [ 0 ]
rnk = ( rank , e [ " scrobbles " ] )
rank = " # " + str ( rank )
2019-03-12 01:39:46 +03:00
#image = "/image?artist=" + urllib.parse.quote(e["artist"])
2019-03-12 13:39:36 +03:00
image = getArtistImage ( e [ " artist " ] , fast = True )
2019-03-03 23:55:35 +03:00
link = artistLink ( e [ " artist " ] )
else :
rank = " "
image = " "
link = " "
2019-03-14 13:07:20 +03:00
2019-03-03 23:22:42 +03:00
html + = """ <td style= " background-image:url( ' """ + image + """ ' ) " ><span class= " stats " > """ + rank + " </span> <span> " + link + " </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 " )
kwargs_time = pickKeys ( kwargs , " 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-03-03 23:55:35 +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 :
rank = i if e [ " scrobbles " ] != rnk [ 1 ] else rnk [ 0 ]
rnk = ( rank , e [ " scrobbles " ] )
rank = " # " + str ( rank )
2019-03-12 01:39:46 +03:00
#image = "/image?title=" + urllib.parse.quote(e["track"]["title"]) + "&" + "&".join(["artist=" + urllib.parse.quote(a) for a in e["track"]["artists"]])
2019-03-12 13:39:36 +03:00
image = getTrackImage ( e [ " track " ] [ " artists " ] , e [ " track " ] [ " title " ] , fast = True )
2019-03-03 23:55:35 +03:00
link = trackLink ( e [ " track " ] )
else :
rank = " "
image = " "
link = " "
2019-03-14 13:07:20 +03:00
2019-03-03 23:22:42 +03:00
html + = """ <td style= " background-image:url( ' """ + image + """ ' ) " ><span class= " stats " > """ + rank + " </span> <span> " + link + " </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