From c732e13c3be15b2628cf4a90d6ceda94679ffa6b Mon Sep 17 00:00:00 2001 From: Krateng Date: Tue, 19 Feb 2019 14:57:39 +0100 Subject: [PATCH] Provisionary fix for various time issues --- database.py | 65 +++++++++++++++++++++++++++++++++------------- htmlgenerators.py | 25 ++++++++++++++++++ utilities.py | 11 ++++++-- website/pulse.py | 7 ++--- website/start.html | 36 +++++++++---------------- website/start.py | 9 ++++--- 6 files changed, 102 insertions(+), 51 deletions(-) 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 += "" - html += "" + fromstr + "" - html += "" + tostr + "" + #html += "" + fromstr + "" + #html += "" + tostr + "" + html += "" + getRangeDesc(t["from"],t["to"]) + "" html += "" + scrobblesLink({"since":fromstr,"to":tostr},amount=t["scrobbles"],**limitkey) + "" html += "" + scrobblesLink({"since":fromstr,"to":tostr},percent=t["scrobbles"]*100/maxbar,**limitkey) + "" html += "" diff --git a/website/start.html b/website/start.html index f12ffc7..418e611 100644 --- a/website/start.html +++ b/website/start.html @@ -216,74 +216,62 @@ - KEY_PULSE_TERM_FROM - KEY_PULSE_TERM_TO + KEY_PULSE_TERM KEY_PULSE_AMOUNT KEY_PULSE_BAR - KEY_PULSE_TERM_FROM - KEY_PULSE_TERM_TO + KEY_PULSE_TERM KEY_PULSE_AMOUNT KEY_PULSE_BAR - KEY_PULSE_TERM_FROM - KEY_PULSE_TERM_TO + KEY_PULSE_TERM KEY_PULSE_AMOUNT KEY_PULSE_BAR - KEY_PULSE_TERM_FROM - KEY_PULSE_TERM_TO + KEY_PULSE_TERM KEY_PULSE_AMOUNT KEY_PULSE_BAR - KEY_PULSE_TERM_FROM - KEY_PULSE_TERM_TO + KEY_PULSE_TERM KEY_PULSE_AMOUNT KEY_PULSE_BAR - KEY_PULSE_TERM_FROM - KEY_PULSE_TERM_TO + KEY_PULSE_TERM KEY_PULSE_AMOUNT KEY_PULSE_BAR - KEY_PULSE_TERM_FROM - KEY_PULSE_TERM_TO + KEY_PULSE_TERM KEY_PULSE_AMOUNT KEY_PULSE_BAR - KEY_PULSE_TERM_FROM - KEY_PULSE_TERM_TO + KEY_PULSE_TERM KEY_PULSE_AMOUNT KEY_PULSE_BAR - KEY_PULSE_TERM_FROM - KEY_PULSE_TERM_TO + KEY_PULSE_TERM KEY_PULSE_AMOUNT KEY_PULSE_BAR - KEY_PULSE_TERM_FROM - KEY_PULSE_TERM_TO + KEY_PULSE_TERM KEY_PULSE_AMOUNT KEY_PULSE_BAR - KEY_PULSE_TERM_FROM - KEY_PULSE_TERM_TO + KEY_PULSE_TERM KEY_PULSE_AMOUNT KEY_PULSE_BAR - KEY_PULSE_TERM_FROM - KEY_PULSE_TERM_TO + KEY_PULSE_TERM KEY_PULSE_AMOUNT KEY_PULSE_BAR diff --git a/website/start.py b/website/start.py index d05648a..17fb521 100644 --- a/website/start.py +++ b/website/start.py @@ -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)