Return multiple top results for ranges, GH-278

This commit is contained in:
krateng 2023-12-28 03:09:25 +01:00
parent d160078def
commit 436b40821a
2 changed files with 53 additions and 21 deletions

View File

@ -482,7 +482,7 @@ def get_top_artists_external(k_filter, k_limit, k_delimit, k_amount):
:rtype: Dictionary"""
ckeys = {**k_limit, **k_delimit}
results = database.get_top_artists(**ckeys)
results = database.get_top_artists(**ckeys,compatibility=True)
return {
"status":"ok",
@ -501,7 +501,7 @@ def get_top_tracks_external(k_filter, k_limit, k_delimit, k_amount):
:rtype: Dictionary"""
ckeys = {**k_limit, **k_delimit}
results = database.get_top_tracks(**ckeys)
results = database.get_top_tracks(**ckeys,compatibility=True)
# IMPLEMENT THIS FOR TOP TRACKS OF ARTIST/ALBUM AS WELL?
return {
@ -521,7 +521,7 @@ def get_top_albums_external(k_filter, k_limit, k_delimit, k_amount):
:rtype: Dictionary"""
ckeys = {**k_limit, **k_delimit}
results = database.get_top_albums(**ckeys)
results = database.get_top_albums(**ckeys,compatibility=True)
# IMPLEMENT THIS FOR TOP ALBUMS OF ARTIST AS WELL?
return {

View File

@ -42,6 +42,7 @@ from collections import namedtuple
from threading import Lock
import yaml, json
import math
from itertools import takewhile
# url handling
import urllib
@ -570,7 +571,7 @@ def get_performance(dbconn=None,**keys):
return results
@waitfordb
def get_top_artists(dbconn=None,**keys):
def get_top_artists(dbconn=None,compatibility=True,**keys):
separate = keys.get('separate')
@ -578,42 +579,73 @@ def get_top_artists(dbconn=None,**keys):
results = []
for rng in rngs:
try:
res = get_charts_artists(timerange=rng,separate=separate,dbconn=dbconn)[0]
results.append({"range":rng,"artist":res["artist"],"scrobbles":res["scrobbles"],"real_scrobbles":res["real_scrobbles"],"associated_artists":sqldb.get_associated_artists(res["artist"])})
except Exception:
results.append({"range":rng,"artist":None,"scrobbles":0,"real_scrobbles":0})
result = {'range':rng}
res = get_charts_artists(timerange=rng,separate=separate,dbconn=dbconn)
result['top'] = [
{'artist': r['artist'], 'scrobbles': r['scrobbles'], 'real_scrobbles':r['real_scrobbles'], 'associated_artists': sqldb.get_associated_artists(r['artist'])}
for r in takewhile(lambda x:x['rank']==1,res)
]
# for third party applications
if compatibility:
if result['top']:
result.update(result['top'][0])
else:
result.update({'artist':None,'scrobbles':0,'real_scrobbles':0})
results.append(result)
return results
@waitfordb
def get_top_tracks(dbconn=None,**keys):
def get_top_tracks(dbconn=None,compatibility=True,**keys):
rngs = ranges(**{k:keys[k] for k in keys if k in ["since","to","within","timerange","step","stepn","trail"]})
results = []
for rng in rngs:
try:
res = get_charts_tracks(timerange=rng,dbconn=dbconn)[0]
results.append({"range":rng,"track":res["track"],"scrobbles":res["scrobbles"]})
except Exception:
results.append({"range":rng,"track":None,"scrobbles":0})
result = {'range':rng}
res = get_charts_tracks(timerange=rng,dbconn=dbconn)
result['top'] = [
{'track': r['track'], 'scrobbles': r['scrobbles']}
for r in takewhile(lambda x:x['rank']==1,res)
]
# for third party applications
if compatibility:
if result['top']:
result.update(result['top'][0])
else:
result.update({'track':None,'scrobbles':0})
results.append(result)
return results
@waitfordb
def get_top_albums(dbconn=None,**keys):
def get_top_albums(dbconn=None,compatibility=True,**keys):
rngs = ranges(**{k:keys[k] for k in keys if k in ["since","to","within","timerange","step","stepn","trail"]})
results = []
for rng in rngs:
try:
res = get_charts_albums(timerange=rng,dbconn=dbconn)[0]
results.append({"range":rng,"album":res["album"],"scrobbles":res["scrobbles"]})
except Exception:
results.append({"range":rng,"album":None,"scrobbles":0})
result = {'range':rng}
res = get_charts_tracks(timerange=rng,dbconn=dbconn)
result['top'] = [
{'album': r['album'], 'scrobbles': r['scrobbles']}
for r in takewhile(lambda x:x['rank']==1,res)
]
# for third party applications
if compatibility:
if result['top']:
result.update(result['top'][0])
else:
result.update({'album':None,'scrobbles':0})
results.append(result)
return results