diff --git a/database.py b/database.py index 48d6eb6..ff490fc 100644 --- a/database.py +++ b/database.py @@ -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()) diff --git a/htmlgenerators.py b/htmlgenerators.py index 9ea124a..b8a8e23 100644 --- a/htmlgenerators.py +++ b/htmlgenerators.py @@ -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 diff --git a/utilities.py b/utilities.py index 8a46ac3..1a630e3 100644 --- a/utilities.py +++ b/utilities.py @@ -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 = [ { diff --git a/website/pulse.py b/website/pulse.py index c3536bc..6891d17 100644 --- a/website/pulse.py +++ b/website/pulse.py @@ -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 += "