From f1d1421374cdce115cc6d4e0ab87479a9914cb81 Mon Sep 17 00:00:00 2001 From: Krateng Date: Wed, 12 Dec 2018 19:37:59 +0100 Subject: [PATCH] Minor fixes and structure --- cleanup.py | 10 ++++++- database.py | 82 ++++++++++++++++++++++++++++++++++++++++++----------- 2 files changed, 75 insertions(+), 17 deletions(-) diff --git a/cleanup.py b/cleanup.py index 906e4f8..a51024e 100644 --- a/cleanup.py +++ b/cleanup.py @@ -85,6 +85,14 @@ class CleanerAgent: (title,artists) = self.parseTitleForArtists(re.sub(r"(.*) \(" + d + " (.*?)\)",r"\1",t)) artists += self.parseArtists(re.sub(r"(.*) \(" + d + " (.*?)\).*",r"\2",t)) return (title,artists) + if re.match(r"(.*) - " + d + " (.*)",t) is not None: + (title,artists) = self.parseTitleForArtists(re.sub(r"(.*) - " + d + " (.*)",r"\1",t)) + artists += self.parseArtists(re.sub(r"(.*) - " + d + " (.*).*",r"\2",t)) + return (title,artists) + if re.match(r"(.*) " + d + " (.*)",t) is not None: + (title,artists) = self.parseTitleForArtists(re.sub(r"(.*) " + d + " (.*)",r"\1",t)) + artists += self.parseArtists(re.sub(r"(.*) " + d + " (.*).*",r"\2",t)) + return (title,artists) return (t,[]) @@ -103,7 +111,7 @@ class CollectorAgent: for a in self.rules_countas: self.rules_include[self.rules_countas[a]] = self.rules_include.setdefault(self.rules_countas[a],[]) + [a] - # this agent needs to be aware of the current id assignment in the main program. but unelegant, but the best way i can think of + # this agent needs to be aware of the current id assignment in the main program. unelegant, but the best way i can think of def updateIDs(self,artistlist): self.rules_countas_id = {artistlist.index(a):artistlist.index(self.rules_countas[a]) for a in self.rules_countas} #self.rules_include_id = {artistlist.index(a):artistlist.index(self.rules_include[a]) for a in self.rules_include} diff --git a/database.py b/database.py index f0c9493..a4c875b 100644 --- a/database.py +++ b/database.py @@ -31,6 +31,11 @@ def loadAPIkeys(): def checkAPIkey(k): return (k in [k for [k,d] in clients]) + +#### +## Getting dict representations of database objects +#### + def getScrobbleObject(o): track = getTrackObject(TRACKS[o[0]]) return {"artists":track["artists"],"title":track["title"],"time":o[1]} @@ -43,6 +48,11 @@ def getTrackObject(o): return {"artists":artists,"title":o[1]} +#### +## Creating or finding existing database entries +#### + + def createScrobble(artists,title,time): while (time in timestamps): @@ -86,6 +96,11 @@ def getTrackID(artists,title): return i +#### +## HTTP requests +#### + + @route("/scrobbles") def get_scrobbles(): keys = request.query @@ -290,6 +305,13 @@ def post_scrobble(): def abouttoshutdown(): sync() #sys.exit() + + +#### +## Server operation +#### + + # Starts the server def runserver(DATABASE_PORT): @@ -364,8 +386,17 @@ def sync(): print("Database saved to disk.") + + + +#### +## Database queries +#### + + + # Queries the database -def db_query(artist=None,track=None,since=0,to=9999999999): +def db_query(artist=None,track=None,since=None,to=None): (since, to) = getTimestamps(since,to) @@ -384,7 +415,7 @@ def db_query(artist=None,track=None,since=0,to=9999999999): # Queries that... well... aggregate -def db_aggregate(by=None,since=0,to=9999999999): +def db_aggregate(by=None,since=None,to=None): (since, to) = getTimestamps(since,to) if (by=="ARTIST"): @@ -418,6 +449,29 @@ def db_aggregate(by=None,since=0,to=9999999999): return len([scr for scr in SCROBBLES if since < scr[1] < to]) + +# Search for strings +def db_search(query,type=None): + if type=="ARTIST": + results = [] + for a in ARTISTS: + if query.lower() in a.lower(): + results.append(a) + + if type=="TRACK": + results = [] + for t in TRACKS: + if query.lower() in t[1].lower(): + results.append(t) + + return results + + +#### +## Useful functions +#### + + # Takes user inputs like YYYY/MM and returns the timestamps. Returns timestamp if timestamp was already given. def getTimestamps(f,t): #(f,t) = inp @@ -447,19 +501,15 @@ def getTimestamps(f,t): return (f,t) -# Search for strings -def db_search(query,type=None): - if type=="ARTIST": - results = [] - for a in ARTISTS: - if query.lower() in a.lower(): - results.append(a) + - if type=="TRACK": - results = [] - for t in TRACKS: - if query.lower() in t[1].lower(): - results.append(t) - - return results +def getArtistId(nameorid): + if isinstance(nameorid,int): + return nameorid + else: + try: + return ARTISTS.index(nameorid) + except: + return -1 +