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

Refactoring

This commit is contained in:
Krateng 2019-04-07 15:01:04 +02:00
parent 4701228c3f
commit 194bc6c742
2 changed files with 33 additions and 24 deletions

View File

@ -70,15 +70,16 @@ def checkAPIkey(k):
## Getting dict representations of database objects ## Getting dict representations of database objects
#### ####
def getScrobbleObject(o): def get_scrobble_dict(o):
track = getTrackObject(TRACKS[o.track) track = get_track_dict(TRACKS[o.track)
return {"artists":track["artists"],"title":track["title"],"time":o.time} return {"artists":track["artists"],"title":track["title"],"time":o.time}
def getArtistObject(o): def get_artist_dict(o):
return o return o
#technically not a dict, but... you know
def getTrackObject(o): def get_track_dict(o):
artists = [getArtistObject(ARTISTS[a]) for a in o.artists] artists = [get_artist_dict(ARTISTS[a]) for a in o.artists]
return {"artists":artists,"title":o.title} return {"artists":artists,"title":o.title}
@ -305,10 +306,10 @@ def get_tracks(artist=None):
artistid = None artistid = None
# Option 1 # Option 1
return [getTrackObject(t) for t in TRACKS if (artistid in t.artists) or (artistid==None)] return [get_track_dict(t) for t in TRACKS if (artistid in t.artists) or (artistid==None)]
# Option 2 is a bit more elegant but much slower # Option 2 is a bit more elegant but much slower
#tracklist = [getTrackObject(t) for t in TRACKS] #tracklist = [get_track_dict(t) for t in TRACKS]
#ls = [t for t in tracklist if (artist in t["artists"]) or (artist==None)] #ls = [t for t in tracklist if (artist in t["artists"]) or (artist==None)]
@ -818,7 +819,7 @@ def sync():
for idx in range(len(SCROBBLES)): for idx in range(len(SCROBBLES)):
if not SCROBBLES[idx][2]: if not SCROBBLES[idx][2]:
t = getScrobbleObject(SCROBBLES[idx]) t = get_scrobble_dict(SCROBBLES[idx])
artistlist = list(t["artists"]) artistlist = list(t["artists"])
artistlist.sort() #we want the order of artists to be deterministic so when we update files with new rules a diff can see what has actually been changed artistlist.sort() #we want the order of artists to be deterministic so when we update files with new rules a diff can see what has actually been changed
@ -944,7 +945,7 @@ def db_query_full(artist=None,artists=None,title=None,track=None,since=None,to=N
for s in scrobbles_in_range(since,to,reverse=True): for s in scrobbles_in_range(since,to,reverse=True):
if i == max_: break if i == max_: break
if (track is None or s[0] == track) and (artist is None or artist in TRACKS[s[0]][0] or associated and artist in coa.getCreditedList(TRACKS[s[0]][0])): if (track is None or s[0] == track) and (artist is None or artist in TRACKS[s[0]][0] or associated and artist in coa.getCreditedList(TRACKS[s[0]][0])):
result.append(getScrobbleObject(s)) result.append(get_scrobble_dict(s))
i += 1 i += 1
return result return result
@ -974,7 +975,7 @@ def db_aggregate_full(by=None,since=None,to=None,within=None,artist=None):
# this either creates the new entry or increments the existing one # this either creates the new entry or increments the existing one
charts[a] = charts.setdefault(a,0) + 1 charts[a] = charts.setdefault(a,0) + 1
ls = [{"artist":getArtistObject(ARTISTS[a]),"scrobbles":charts[a],"counting":coa.getAllAssociated(ARTISTS[a])} for a in charts] ls = [{"artist":get_artist_dict(ARTISTS[a]),"scrobbles":charts[a],"counting":coa.getAllAssociated(ARTISTS[a])} for a in charts]
ls.sort(key=lambda k:k["scrobbles"],reverse=True) ls.sort(key=lambda k:k["scrobbles"],reverse=True)
# add ranks # add ranks
for rnk in range(len(ls)): for rnk in range(len(ls)):
@ -992,7 +993,7 @@ def db_aggregate_full(by=None,since=None,to=None,within=None,artist=None):
# this either creates the new entry or increments the existing one # this either creates the new entry or increments the existing one
charts[track] = charts.setdefault(track,0) + 1 charts[track] = charts.setdefault(track,0) + 1
ls = [{"track":getTrackObject(TRACKS[t]),"scrobbles":charts[t]} for t in charts] ls = [{"track":get_track_dict(TRACKS[t]),"scrobbles":charts[t]} for t in charts]
ls.sort(key=lambda k:k["scrobbles"],reverse=True) ls.sort(key=lambda k:k["scrobbles"],reverse=True)
# add ranks # add ranks
for rnk in range(len(ls)): for rnk in range(len(ls)):
@ -1021,7 +1022,7 @@ def db_search(query,type=None):
for t in TRACKS: for t in TRACKS:
#if query.lower() in t[1].lower(): #if query.lower() in t[1].lower():
if simplestr(query) in simplestr(t[1]): if simplestr(query) in simplestr(t[1]):
results.append(getTrackObject(t)) results.append(get_track_dict(t))
return results return results
@ -1083,17 +1084,17 @@ def generateStuff(num=0,pertrack=0,mult=0):
import random import random
for i in range(num): for i in range(num):
track = random.choice(TRACKS) track = random.choice(TRACKS)
t = getTrackObject(track) t = get_track_dict(track)
time = random.randint(STAMPS[0],STAMPS[-1]) time = random.randint(STAMPS[0],STAMPS[-1])
createScrobble(t["artists"],t["title"],time,volatile=True) createScrobble(t["artists"],t["title"],time,volatile=True)
for track in TRACKS: for track in TRACKS:
t = getTrackObject(track) t = get_track_dict(track)
for i in range(pertrack): for i in range(pertrack):
time = random.randint(STAMPS[0],STAMPS[-1]) time = random.randint(STAMPS[0],STAMPS[-1])
createScrobble(t["artists"],t["title"],time,volatile=True) createScrobble(t["artists"],t["title"],time,volatile=True)
for scrobble in SCROBBLES: for scrobble in SCROBBLES:
s = getScrobbleObject(scrobble) s = get_scrobble_dict(scrobble)
for i in range(mult): for i in range(mult):
createScrobble(s["artists"],s["title"],s["time"] - i*500,volatile=True) createScrobble(s["artists"],s["title"],s["time"] - i*500,volatile=True)

View File

@ -173,17 +173,25 @@ def KeySplit(keys,forceTrack=False,forceArtist=False):
# 2 # 2
resultkeys2 = {} resultkeys2 = {}
if "since" in keys: resultkeys2["since"] = time_fix(keys.get("since")) if "since" in keys: resultkeys2["since"] = uri_to_internal(keys.get("since"))
elif "from" in keys: resultkeys2["since"] = time_fix(keys.get("from")) elif "from" in keys: resultkeys2["since"] = uri_to_internal(keys.get("from"))
elif "start" in keys: resultkeys2["since"] = time_fix(keys.get("start")) elif "start" in keys: resultkeys2["since"] = uri_to_internal(keys.get("start"))
# #
if "to" in keys: resultkeys2["to"] = time_fix(keys.get("to")) if "to" in keys: resultkeys2["to"] = uri_to_internal(keys.get("to"))
elif "until" in keys: resultkeys2["to"] = time_fix(keys.get("until")) elif "until" in keys: resultkeys2["to"] = uri_to_internal(keys.get("until"))
elif "end" in keys: resultkeys2["to"] = time_fix(keys.get("end")) elif "end" in keys: resultkeys2["to"] = uri_to_internal(keys.get("end"))
# #
if "in" in keys: resultkeys2["within"] = time_fix(keys.get("in")) if "since" in resultkeys2 and "to" in resultkeys2 and resultkeys2["since"] == resultkeys2["to"]:
elif "within" in keys: resultkeys2["within"] = time_fix(keys.get("within")) resultkeys2["within"] = resultkeys2["since"]
elif "during" in keys: resultkeys2["within"] = time_fix(keys.get("during")) del resultkeys2["since"]
del resultkeys2["to"]
#
if "in" in keys: resultkeys2["within"] = uri_to_internal(keys.get("in"))
elif "within" in keys: resultkeys2["within"] = uri_to_internal(keys.get("within"))
elif "during" in keys: resultkeys2["within"] = uri_to_internal(keys.get("during"))
if "within" in resultkeys2:
del resultkeys2["since"]
del resultkeys2["to"]
#3 #3