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

Minor fixes and structure

This commit is contained in:
Krateng 2018-12-12 19:37:59 +01:00
parent e7e4cdebee
commit f1d1421374
2 changed files with 75 additions and 17 deletions

View File

@ -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}

View File

@ -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