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
87
database.py
87
database.py
@ -205,50 +205,95 @@ def get_top_artists():
|
|||||||
since = request.query.get("since")
|
since = request.query.get("since")
|
||||||
to = request.query.get("to")
|
to = request.query.get("to")
|
||||||
(ts_start,ts_end) = getTimestamps(since,to)
|
(ts_start,ts_end) = getTimestamps(since,to)
|
||||||
date_start = datetime.datetime.utcfromtimestamp(ts_start)
|
#date_start = datetime.datetime.utcfromtimestamp(ts_start)
|
||||||
date_end = datetime.datetime.utcfromtimestamp(ts_end)
|
#date_end = datetime.datetime.utcfromtimestamp(ts_end)
|
||||||
d_end = [date_end.year,date_end.month,date_end.day]
|
#d_end = [date_end.year,date_end.month,date_end.day]
|
||||||
|
|
||||||
step = request.query.get("step")
|
# We use a trailing multiplier instead of a separate argument for time and step to avoid weirdness
|
||||||
time = request.query.get("time") # should not be lower than step
|
# e.g. if our steps are weeks, but the time is a month, should the value for 3/31 go back to 2/28?
|
||||||
if (step == None):
|
step = request.query.get("step","month")
|
||||||
step = "month"
|
trail = int(request.query.get("trail","3"))
|
||||||
if (time == None):
|
|
||||||
time = "month-3"
|
|
||||||
|
|
||||||
[step,stepn] = (step.split("-") + [1])[:2] # makes the multiplier 1 if not assigned
|
[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 = getStartOf(ts_start,step)
|
||||||
d_start = [date_start.year,1,1]
|
d_end = getStartOf(ts_end,step)
|
||||||
elif step == "month":
|
#if step == "year":
|
||||||
d_start = [date_start.year,date_start.month,1]
|
# 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]]
|
#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]]
|
#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,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,[-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 = []
|
results = []
|
||||||
|
|
||||||
d_current = d_start
|
d_current = d_start
|
||||||
while True:
|
while True:
|
||||||
d_current_end = addDate(d_current,ran)
|
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]
|
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"]})
|
results.append({"from":d_current,"to":d_current_end,"artist":res["artist"],"scrobbles":res["scrobbles"]})
|
||||||
d_current = addDate(d_current,inc)
|
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):
|
if isPast(d_current_end,d_end):
|
||||||
break
|
break
|
||||||
|
|
||||||
return {"list":results}
|
return {"list":results}
|
||||||
|
|
||||||
|
|
||||||
# only for months and years so far
|
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)
|
||||||
|
|
||||||
|
# DEPRECATED
|
||||||
def addDate(date,inc):
|
def addDate(date,inc):
|
||||||
newdate = [1,1,1]
|
newdate = [1,1,1]
|
||||||
newdate[0] = date[0] + inc[0]
|
newdate[0] = date[0] + inc[0]
|
||||||
newdate[1] = date[1] + inc[1]
|
newdate[1] = date[1] + inc[1]
|
||||||
|
newdate[2] = date[2] + inc[2]
|
||||||
while (newdate[1] > 12):
|
while (newdate[1] > 12):
|
||||||
newdate[1] -= 12
|
newdate[1] -= 12
|
||||||
newdate[0] += 1
|
newdate[0] += 1
|
||||||
|
Loading…
Reference in New Issue
Block a user