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

Provisionary fix for various time issues

This commit is contained in:
Krateng 2019-02-19 14:57:39 +01:00
parent f29c5ca8b9
commit c732e13c3b
6 changed files with 102 additions and 51 deletions

View File

@ -324,7 +324,8 @@ def get_pulse(step="month",stepn=1,trail=3,**keys):
d_current = d_start
while True:
d_current_end = getNext(d_current,step,stepn * trail)
#d_current_end = getNext(d_current,step,stepn * trail)
d_current_end = getEnd(d_current,step,stepn * trail)
#res = db_aggregate(since=d_current,to=d_current_end)
res = len(db_query(since=d_current,to=d_current_end,**{k:keys[k] for k in keys if k in ["artists","title","associated"]}))
results.append({"from":d_current,"to":d_current_end,"scrobbles":res})
@ -441,9 +442,11 @@ def get_top_tracks(step="month",stepn=1,trail=3,**keys):
def getStartOf(timestamp,unit):
date = datetime.datetime.utcfromtimestamp(timestamp)
if unit == "year":
return [date.year,1,1]
#return [date.year,1,1]
return [date.year]
elif unit == "month":
return [date.year,date.month,1]
#return [date.year,date.month,1]
return [date.year,date.month]
elif unit == "day":
return [date.year,date.month,date.day]
elif unit == "week":
@ -453,16 +456,20 @@ def getStartOf(timestamp,unit):
return [newdate.year,newdate.month,newdate.day]
def getNext(time,unit="auto",step=1):
result = time[:]
if unit == "auto":
# see how long the list is, increment by the last specified unit
unit = [None,"year","month","day"][len(time)]
while len(time) < 3:
time.append(1)
#while len(time) < 3:
# time.append(1)
if unit == "year":
return [time[0] + step,time[1],time[2]]
#return [time[0] + step,time[1],time[2]]
result[0] += step
return result
elif unit == "month":
result = [time[0],time[1] + step,time[2]]
#result = [time[0],time[1] + step,time[2]]
result[1] += step
while result[1] > 12:
result[1] -= 12
result[0] += 1
@ -479,6 +486,22 @@ def getNext(time,unit="auto",step=1):
elif unit == "week":
return getNext(time,"day",step * 7)
# like getNext(), but gets the last INCLUDED day / month whatever
def getEnd(time,unit="auto",step=1):
if step == 1:
if unit == "auto": return time[:]
if unit == "year" and len(time) == 1: return time[:]
if unit == "month" and len(time) == 2: return time[:]
if unit == "day" and len(time) == 3: return time[:]
exc = getNext(time,unit,step)
inc = getNext(exc,"auto",-1)
return inc
@dbserver.route("/artistinfo")
def artistInfo_external():
keys = FormsDict.decode(request.query)
@ -525,11 +548,14 @@ def trackInfo(artists,title):
def isPast(date,limit):
if not date[0] == limit[0]:
return date[0] > limit[0]
if not date[1] == limit[1]:
return date[1] > limit[1]
return (date[2] > limit[2])
date_, limit_ = date[:], limit[:]
while len(date_) != 3: date_.append(1)
while len(limit_) != 3: limit_.append(1)
if not date_[0] == limit_[0]:
return date_[0] > limit_[0]
if not date_[1] == limit_[1]:
return date_[1] > limit_[1]
return (date_[2] > limit_[2])
@ -951,16 +977,19 @@ def getTimestamps(since=None,to=None,within=None):
# this step is done if either the input is a list or the first step was done (which creates a list)
if isinstance(f, list):
#date = [1970,1,1,0,0]
#date[:len(f)] = f
while len(f) < 3: f.append(1) # padding month and day
date = [1970,1,1]
date[:len(f)] = f
#while len(f) < 3: f.append(1) # padding month and day
f = date
#f = int(datetime.datetime(date[0],date[1],date[2],date[3],date[4],tzinfo=datetime.timezone.utc).timestamp())
f = int(datetime.datetime(f[0],f[1],f[2],tzinfo=datetime.timezone.utc).timestamp())
if isinstance(t, list):
t = getNext(t) # going on step forward automatically pads month and day
#date = [1970,1,1,0,0]
#date[:len(t)] = t
t = getNext(t)
#while len(t) < 3: t.append(1)
date = [1970,1,1]
date[:len(t)] = t
t = date
#t = int(datetime.datetime(date[0],date[1],date[2],date[3],date[4],tzinfo=datetime.timezone.utc).timestamp())
t = int(datetime.datetime(t[0],t[1],t[2],tzinfo=datetime.timezone.utc).timestamp())

View File

@ -82,6 +82,31 @@ def getTimeDesc(timestamp,short=False):
return tim.strftime("%d. %B %Y")
else:
return tim.strftime("%d. %b %Y %I:%M %p")
def getRangeDesc(timeA,timeB,inclusiveB=True):
# string to list
if isinstance(timeA,str): timeA = timeA.split("/")
if isinstance(timeB,str): timeB = timeB.split("/")
# if lists, we have it potentially much easier:
if isinstance(timeA,list) and isinstance(timeB,list):
if timeA == timeB:
date = [1970,1,1]
date[:len(timeA)] = timeA
dto = datetime.datetime(date[0],date[1],date[2],tzinfo=datetime.timezone.utc)
if len(timeA) == 3:
return dto.strftime("%d. %b %Y")
if len(timeA) == 2:
return dto.strftime("%B %Y")
if len(timeA) == 1:
return dto.strftime("%Y")
from database import getTimestamps
(timeA, timeB) = getTimestamps(since=timeA, to=timeB)
return getTimeDesc(timeA) + " to " + getTimeDesc(timeB)
# limit a multidict to only the specified keys

View File

@ -166,6 +166,7 @@ def log(msg):
logfile.write(msg + "\n")
### Media info
def apirequest(artists=None,artist=None,title=None):
@ -173,8 +174,14 @@ def apirequest(artists=None,artist=None,title=None):
import urllib.parse, urllib.request
import json
with open("apikey","r") as keyfile:
apikey = keyfile.read().replace("\n","")
try:
with open("apikey","r") as keyfile:
apikey = keyfile.read().replace("\n","")
if apikey == "NONE": return {"image":None}
except:
return {"image":None}
sites = [
{

View File

@ -4,7 +4,7 @@ import json
def instructions(keys,dbport):
from utilities import getArtistInfo, getTrackInfo
from htmlgenerators import getTimeDesc, artistLink, artistLinks, trackLink, scrobblesLink, keysToUrl, pickKeys, clean
from htmlgenerators import getTimeDesc, artistLink, artistLinks, trackLink, scrobblesLink, keysToUrl, pickKeys, clean, getRangeDesc
clean(keys)
timekeys = pickKeys(keys,"since","to","in","step","trail")
@ -58,8 +58,9 @@ def instructions(keys,dbport):
fromstr = "/".join([str(e) for e in t["from"]])
tostr = "/".join([str(e) for e in t["to"]])
html += "<tr>"
html += "<td>" + fromstr + "</td>"
html += "<td>" + tostr + "</td>"
#html += "<td>" + fromstr + "</td>"
#html += "<td>" + tostr + "</td>"
html += "<td>" + getRangeDesc(t["from"],t["to"]) + "</td>"
html += "<td class='amount'>" + scrobblesLink({"since":fromstr,"to":tostr},amount=t["scrobbles"],**limitkey) + "</td>"
html += "<td class='bar'>" + scrobblesLink({"since":fromstr,"to":tostr},percent=t["scrobbles"]*100/maxbar,**limitkey) + "</td>"
html += "</tr>"

View File

@ -216,74 +216,62 @@
<tr>
<td>KEY_PULSE_TERM_FROM</td>
<td>KEY_PULSE_TERM_TO</td>
<td>KEY_PULSE_TERM</td>
<td class='amount'>KEY_PULSE_AMOUNT</td>
<td class='bar'>KEY_PULSE_BAR</td>
</tr>
<tr>
<td>KEY_PULSE_TERM_FROM</td>
<td>KEY_PULSE_TERM_TO</td>
<td>KEY_PULSE_TERM</td>
<td class='amount'>KEY_PULSE_AMOUNT</td>
<td class='bar'>KEY_PULSE_BAR</td>
</tr>
<tr>
<td>KEY_PULSE_TERM_FROM</td>
<td>KEY_PULSE_TERM_TO</td>
<td>KEY_PULSE_TERM</td>
<td class='amount'>KEY_PULSE_AMOUNT</td>
<td class='bar'>KEY_PULSE_BAR</td>
</tr>
<tr>
<td>KEY_PULSE_TERM_FROM</td>
<td>KEY_PULSE_TERM_TO</td>
<td>KEY_PULSE_TERM</td>
<td class='amount'>KEY_PULSE_AMOUNT</td>
<td class='bar'>KEY_PULSE_BAR</td>
</tr>
<tr>
<td>KEY_PULSE_TERM_FROM</td>
<td>KEY_PULSE_TERM_TO</td>
<td>KEY_PULSE_TERM</td>
<td class='amount'>KEY_PULSE_AMOUNT</td>
<td class='bar'>KEY_PULSE_BAR</td>
</tr>
<tr>
<td>KEY_PULSE_TERM_FROM</td>
<td>KEY_PULSE_TERM_TO</td>
<td>KEY_PULSE_TERM</td>
<td class='amount'>KEY_PULSE_AMOUNT</td>
<td class='bar'>KEY_PULSE_BAR</td>
</tr>
<tr>
<td>KEY_PULSE_TERM_FROM</td>
<td>KEY_PULSE_TERM_TO</td>
<td>KEY_PULSE_TERM</td>
<td class='amount'>KEY_PULSE_AMOUNT</td>
<td class='bar'>KEY_PULSE_BAR</td>
</tr>
<tr>
<td>KEY_PULSE_TERM_FROM</td>
<td>KEY_PULSE_TERM_TO</td>
<td>KEY_PULSE_TERM</td>
<td class='amount'>KEY_PULSE_AMOUNT</td>
<td class='bar'>KEY_PULSE_BAR</td>
</tr>
<tr>
<td>KEY_PULSE_TERM_FROM</td>
<td>KEY_PULSE_TERM_TO</td>
<td>KEY_PULSE_TERM</td>
<td class='amount'>KEY_PULSE_AMOUNT</td>
<td class='bar'>KEY_PULSE_BAR</td>
</tr>
<tr>
<td>KEY_PULSE_TERM_FROM</td>
<td>KEY_PULSE_TERM_TO</td>
<td>KEY_PULSE_TERM</td>
<td class='amount'>KEY_PULSE_AMOUNT</td>
<td class='bar'>KEY_PULSE_BAR</td>
</tr>
<tr>
<td>KEY_PULSE_TERM_FROM</td>
<td>KEY_PULSE_TERM_TO</td>
<td>KEY_PULSE_TERM</td>
<td class='amount'>KEY_PULSE_AMOUNT</td>
<td class='bar'>KEY_PULSE_BAR</td>
</tr>
<tr>
<td>KEY_PULSE_TERM_FROM</td>
<td>KEY_PULSE_TERM_TO</td>
<td>KEY_PULSE_TERM</td>
<td class='amount'>KEY_PULSE_AMOUNT</td>
<td class='bar'>KEY_PULSE_BAR</td>
</tr>

View File

@ -16,7 +16,7 @@ def getpictures(ls,result,tracks=False):
def instructions(keys,dbport):
from utilities import getArtistsInfo, getTracksInfo
from htmlgenerators import artistLink, artistLinks, trackLink, scrobblesArtistLink, scrobblesLink, keysToUrl, pickKeys, clean, getTimeDesc
from htmlgenerators import artistLink, artistLinks, trackLink, scrobblesArtistLink, scrobblesLink, keysToUrl, pickKeys, clean, getTimeDesc, getRangeDesc
max_show = 15
posrange = ["#" + str(i) for i in range(1,max_show)]
@ -106,8 +106,9 @@ def instructions(keys,dbport):
terms = db_data["list"]
maxbar = max([t["scrobbles"] for t in terms])
pulse_fromdates = ["/".join([str(e) for e in t["from"]]) for t in terms]
pulse_todates = ["/".join([str(e) for e in t["to"]]) for t in terms]
#pulse_fromdates = ["/".join([str(e) for e in t["from"]]) for t in terms]
#pulse_todates = ["/".join([str(e) for e in t["to"]]) for t in terms]
pulse_rangedescs = [getRangeDesc(t["from"],t["to"]) for t in terms]
pulse_amounts = [scrobblesLink({"since":"/".join([str(e) for e in t["from"]]),"to":"/".join([str(e) for e in t["to"]])},amount=t["scrobbles"]) for t in terms]
pulse_bars = [scrobblesLink({"since":"/".join([str(e) for e in t["from"]]),"to":"/".join([str(e) for e in t["to"]])},percent=t["scrobbles"]*100/maxbar) for t in terms]
@ -126,7 +127,7 @@ def instructions(keys,dbport):
"KEY_TRACKIMAGE":trackimages,"KEY_TRACKNAME":tracktitles,"KEY_TRACKLINK":tracklinks,"KEY_POSITION_TRACK":posrange,
"KEY_SCROBBLES_TODAY":scrobbles_today,"KEY_SCROBBLES_MONTH":scrobbles_month,"KEY_SCROBBLES_YEAR":scrobbles_year,"KEY_SCROBBLES_TOTAL":scrobbles_total,
"KEY_SCROBBLE_TIME":scrobbletimes,"KEY_SCROBBLE_ARTISTS":scrobbleartists,"KEY_SCROBBLE_TITLE":scrobbletracklinks,"KEY_SCROBBLE_IMAGE":scrobbleimages,
"KEY_PULSE_TERM_FROM":pulse_fromdates,"KEY_PULSE_TERM_TO":pulse_todates,"KEY_PULSE_AMOUNT":pulse_amounts,"KEY_PULSE_BAR":pulse_bars}
"KEY_PULSE_TERM":pulse_rangedescs,"KEY_PULSE_AMOUNT":pulse_amounts,"KEY_PULSE_BAR":pulse_bars}
return (replace,pushresources)