mirror of
https://github.com/krateng/maloja.git
synced 2023-08-10 21:12:55 +03:00
Improved trailing top artists
This commit is contained in:
parent
95e80833cc
commit
159313ad0a
91
database.py
91
database.py
@ -205,50 +205,95 @@ def get_top_artists():
|
||||
since = request.query.get("since")
|
||||
to = request.query.get("to")
|
||||
(ts_start,ts_end) = getTimestamps(since,to)
|
||||
date_start = datetime.datetime.utcfromtimestamp(ts_start)
|
||||
date_end = datetime.datetime.utcfromtimestamp(ts_end)
|
||||
d_end = [date_end.year,date_end.month,date_end.day]
|
||||
#date_start = datetime.datetime.utcfromtimestamp(ts_start)
|
||||
#date_end = datetime.datetime.utcfromtimestamp(ts_end)
|
||||
#d_end = [date_end.year,date_end.month,date_end.day]
|
||||
|
||||
step = request.query.get("step")
|
||||
time = request.query.get("time") # should not be lower than step
|
||||
if (step == None):
|
||||
step = "month"
|
||||
if (time == None):
|
||||
time = "month-3"
|
||||
# We use a trailing multiplier instead of a separate argument for time and step to avoid weirdness
|
||||
# e.g. if our steps are weeks, but the time is a month, should the value for 3/31 go back to 2/28?
|
||||
step = request.query.get("step","month")
|
||||
trail = int(request.query.get("trail","3"))
|
||||
|
||||
[step,stepn] = (step.split("-") + [1])[:2] # makes the multiplier 1 if not assigned
|
||||
[time,timen] = (time.split("-") + [1])[:2]
|
||||
#[time,timen] = (time.split("-") + [1])[:2]
|
||||
stepn = int(stepn)
|
||||
|
||||
if step == "year":
|
||||
d_start = [date_start.year,1,1]
|
||||
elif step == "month":
|
||||
d_start = [date_start.year,date_start.month,1]
|
||||
d_start = getStartOf(ts_start,step)
|
||||
d_end = getStartOf(ts_end,step)
|
||||
#if step == "year":
|
||||
# d_start = [date_start.year,1,1]
|
||||
#elif step == "month":
|
||||
# d_start = [date_start.year,date_start.month,1]
|
||||
|
||||
inc = [i*int(stepn) for i in {"year":[1,0,0],"month":[0,1,0]}[step]]
|
||||
ran = [i*int(timen) for i in {"year":[1,0,0],"month":[0,1,0]}[time]]
|
||||
#inc = [i*int(stepn) for i in {"year":[1,0,0],"month":[0,1,0]}[step]]
|
||||
#ran = [i*int(timen) for i in {"year":[1,0,0],"month":[0,1,0]}[time]]
|
||||
|
||||
d_start = addDate(d_start,inc) # first range should end right after the first active scrobbling week / month / whatever relevant step
|
||||
d_start = addDate(d_start,[-i for i in ran]) # go one range back to begin
|
||||
#d_start = addDate(d_start,inc) # first range should end right after the first active scrobbling week / month / whatever relevant step
|
||||
#d_start = addDate(d_start,[-i for i in ran]) # go one range back to begin
|
||||
d_start = getNext(d_start,step,stepn)
|
||||
d_start = getNext(d_start,step,stepn * trail * -1)
|
||||
|
||||
results = []
|
||||
|
||||
d_current = d_start
|
||||
while True:
|
||||
d_current_end = addDate(d_current,ran)
|
||||
res = db_aggregate(since=d_current,to=d_current_end,by="ARTIST")[0]
|
||||
results.append({"from":d_current,"to":d_current_end,"artist":res["artist"],"scrobbles":res["scrobbles"]})
|
||||
d_current = addDate(d_current,inc)
|
||||
d_current_end = getNext(d_current,step,stepn * trail)
|
||||
#print("Checking from " + str(d_current[0]) + "-" + str(d_current[1]) + "-" + str(d_current[2]) + " to " + str(d_current_end[0]) + "-" + str(d_current_end[1]) + "-" + str(d_current_end[2]))
|
||||
#d_current_end = addDate(d_current,ran)
|
||||
try:
|
||||
res = db_aggregate(since=d_current,to=d_current_end,by="ARTIST")[0]
|
||||
results.append({"from":d_current,"to":d_current_end,"artist":res["artist"],"scrobbles":res["scrobbles"]})
|
||||
except:
|
||||
results.append({"from":d_current,"to":d_current_end,"artist":None,"scrobbles":0})
|
||||
d_current = getNext(d_current,step,stepn)
|
||||
#d_current = addDate(d_current,inc)
|
||||
if isPast(d_current_end,d_end):
|
||||
break
|
||||
|
||||
return {"list":results}
|
||||
|
||||
|
||||
def getStartOf(timestamp,unit):
|
||||
date = datetime.datetime.utcfromtimestamp(timestamp)
|
||||
if unit == "year":
|
||||
return [date.year,1,1]
|
||||
elif unit == "month":
|
||||
return [date.year,date.month,1]
|
||||
elif unit == "day":
|
||||
return [date.year,date.month,date.day]
|
||||
elif unit == "week":
|
||||
change = (date.weekday() + 1) % 7
|
||||
d = datetime.timedelta(days=change)
|
||||
newdate = date - d
|
||||
return [newdate.year,newdate.month,newdate.day]
|
||||
|
||||
def getNext(time,unit,step=1):
|
||||
if unit == "year":
|
||||
return [time[0] + step,time[1],time[2]]
|
||||
elif unit == "month":
|
||||
result = [time[0],time[1] + step,time[2]]
|
||||
while result[1] > 12:
|
||||
result[1] -= 12
|
||||
result[0] += 1
|
||||
while result[1] < 1:
|
||||
result[1] += 12
|
||||
result[0] -= 1
|
||||
return result
|
||||
elif unit == "day":
|
||||
dt = datetime.datetime(time[0],time[1],time[2])
|
||||
d = datetime.timedelta(days=step)
|
||||
newdate = dt + d
|
||||
return [newdate.year,newdate.month,newdate.day]
|
||||
#eugh
|
||||
elif unit == "week":
|
||||
return getNext(time,"day",step * 7)
|
||||
|
||||
# only for months and years so far
|
||||
# DEPRECATED
|
||||
def addDate(date,inc):
|
||||
newdate = [1,1,1]
|
||||
newdate[0] = date[0] + inc[0]
|
||||
newdate[1] = date[1] + inc[1]
|
||||
newdate[2] = date[2] + inc[2]
|
||||
while (newdate[1] > 12):
|
||||
newdate[1] -= 12
|
||||
newdate[0] += 1
|
||||
|
Loading…
Reference in New Issue
Block a user