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

More album functionality

This commit is contained in:
krateng 2023-03-28 20:58:50 +02:00
parent 27a2bc705a
commit 4a0bd4b97e
2 changed files with 35 additions and 3 deletions

View File

@ -267,6 +267,8 @@ def get_charts_tracks(dbconn=None,**keys):
(since,to) = keys.get('timerange').timestamps()
if 'artist' in keys:
result = sqldb.count_scrobbles_by_track_of_artist(since=since,to=to,artist=keys['artist'],dbconn=dbconn)
elif 'album' in keys:
result = sqldb.count_scrobbles_by_track_of_album(since=since,to=to,album=keys['album'],dbconn=dbconn)
else:
result = sqldb.count_scrobbles_by_track(since=since,to=to,dbconn=dbconn)
return result

View File

@ -213,11 +213,12 @@ def scrobble_db_to_dict(row,dbconn=None):
def tracks_db_to_dict(rows,dbconn=None):
artists = get_artists_of_tracks(set(row.id for row in rows),dbconn=dbconn)
albums = get_albums_map(set(row.album_id for row in rows),dbconn=dbconn)
return [
{
"artists":artists[row.id],
"title":row.title,
#"album":
"album":albums.get(row.album_id),
"length":row.length
}
for row in rows
@ -387,7 +388,7 @@ def get_track_id(trackdict,create_new=True,update_album=False,dbconn=None):
#print("required artists",artist_ids,"this match",match_artist_ids)
if set(artist_ids) == set(match_artist_ids):
#print("ID for",trackdict['title'],"was",row[0])
if 'album' in trackdict:
if trackdict.get('album'):
add_track_to_album(row.id,get_album_id(trackdict['album'],dbconn=dbconn),replace=update_album,dbconn=dbconn)
return row.id
@ -407,7 +408,7 @@ def get_track_id(trackdict,create_new=True,update_album=False,dbconn=None):
result = dbconn.execute(op)
#print("Created",trackdict['title'],track_id)
if 'album' in trackdict:
if trackdict.get('album'):
add_track_to_album(track_id,get_album_id(trackdict['album'],dbconn=dbconn),dbconn=dbconn)
return track_id
@ -896,6 +897,35 @@ def count_scrobbles_by_track_of_artist(since,to,artist,dbconn=None):
return result
@cached_wrapper
@connection_provider
def count_scrobbles_by_track_of_album(since,to,album,dbconn=None):
album_id = get_album_id(album,dbconn=dbconn)
jointable = sql.join(
DB['scrobbles'],
DB['tracks'],
DB['scrobbles'].c.track_id == DB['tracks'].c.id
)
op = sql.select(
sql.func.count(sql.func.distinct(DB['scrobbles'].c.timestamp)).label('count'),
DB['scrobbles'].c.track_id
).select_from(jointable).filter(
DB['scrobbles'].c.timestamp<=to,
DB['scrobbles'].c.timestamp>=since,
DB['tracks'].c.album_id==album_id
).group_by(DB['scrobbles'].c.track_id).order_by(sql.desc('count'))
result = dbconn.execute(op).all()
counts = [row.count for row in result]
tracks = get_tracks_map([row.track_id for row in result],dbconn=dbconn)
result = [{'scrobbles':row.count,'track':tracks[row.track_id]} for row in result]
result = rank(result,key='scrobbles')
return result
### functions that get mappings for several entities -> rows