mirror of
https://github.com/krateng/maloja.git
synced 2023-08-10 21:12:55 +03:00
I promise this is temporary code!
This commit is contained in:
parent
8ab42b844b
commit
0dd6cd9dd5
@ -9,7 +9,7 @@ from .malojauri import uri_to_internal, internal_to_uri, compose_querystring
|
|||||||
from .thirdparty import proxy_scrobble_all
|
from .thirdparty import proxy_scrobble_all
|
||||||
from .globalconf import data_dir, malojaconfig, apikeystore
|
from .globalconf import data_dir, malojaconfig, apikeystore
|
||||||
#db
|
#db
|
||||||
from .db.sqldb import *
|
from .db import sqldb
|
||||||
|
|
||||||
# doreah toolkit
|
# doreah toolkit
|
||||||
from doreah.logging import log
|
from doreah.logging import log
|
||||||
@ -545,7 +545,7 @@ def get_predefined_rulesets():
|
|||||||
|
|
||||||
def start_db():
|
def start_db():
|
||||||
from . import upgrade
|
from . import upgrade
|
||||||
upgrade.upgrade_db(add_scrobbles)
|
upgrade.upgrade_db(sqldb.add_scrobbles)
|
||||||
dbstatus['healthy'] = True
|
dbstatus['healthy'] = True
|
||||||
dbstatus['complete'] = True
|
dbstatus['complete'] = True
|
||||||
|
|
||||||
@ -735,22 +735,20 @@ def db_query_full(artist=None,artists=None,title=None,track=None,timerange=None,
|
|||||||
#artist = None
|
#artist = None
|
||||||
|
|
||||||
if artists is not None and title is not None:
|
if artists is not None and title is not None:
|
||||||
return get_scrobbles_of_track(track={"artists":artists,"title":title},since=since,to=to)
|
return sqldb.get_scrobbles_of_track(track={"artists":artists,"title":title},since=since,to=to)
|
||||||
|
|
||||||
if artist is not None:
|
if artist is not None:
|
||||||
return get_scrobbles_of_artist(artist=artist,since=since,to=to)
|
return sqldb.get_scrobbles_of_artist(artist=artist,since=since,to=to)
|
||||||
|
|
||||||
return get_scrobbles(since=since,to=to)
|
return sqldb.get_scrobbles(since=since,to=to)
|
||||||
|
|
||||||
|
|
||||||
# pointless to check for artist when track is checked because every track has a fixed set of artists, but it's more elegant this way
|
|
||||||
|
|
||||||
|
|
||||||
# Queries that... well... aggregate
|
# Queries that... well... aggregate
|
||||||
def db_aggregate_full(by=None,since=None,to=None,within=None,timerange=None,artist=None):
|
def db_aggregate_full(by=None,since=None,to=None,within=None,timerange=None,artist=None):
|
||||||
|
|
||||||
if not dbstatus['healthy']: raise DatabaseNotBuilt()
|
if not dbstatus['healthy']: raise DatabaseNotBuilt()
|
||||||
(since, to) = time_stamps(since=since,to=to,within=within,range=timerange)
|
(since, to) = time_stamps(range=timerange)
|
||||||
|
|
||||||
if isinstance(artist, str):
|
if isinstance(artist, str):
|
||||||
artist = ARTISTS.index(artist)
|
artist = ARTISTS.index(artist)
|
||||||
|
@ -1,40 +0,0 @@
|
|||||||
#### ATTENTION ALL ADVENTURERS
|
|
||||||
#### THIS IS WHAT A SCROBBLE DICT WILL LOOK LIKE FROM NOW ON
|
|
||||||
#### THIS IS THE SINGLE CANONICAL SOURCE OF TRUTH
|
|
||||||
#### STOP MAKING DIFFERENT LITTLE DICTS IN EVERY SINGLE FUNCTION
|
|
||||||
#### THIS IS THE SCHEMA THAT WILL DEFINITELY 100% STAY LIKE THIS AND NOT
|
|
||||||
#### RANDOMLY GET CHANGED TWO VERSIONS LATER
|
|
||||||
#### HERE WE GO
|
|
||||||
#
|
|
||||||
# {
|
|
||||||
# "time":int,
|
|
||||||
# "track":{
|
|
||||||
# "artists":list,
|
|
||||||
# "title":string,
|
|
||||||
# "album":{
|
|
||||||
# "name":string,
|
|
||||||
# "artists":list
|
|
||||||
# },
|
|
||||||
# "length":None
|
|
||||||
# },
|
|
||||||
# "duration":int,
|
|
||||||
# "origin":string
|
|
||||||
# }
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
def scrobble_db_to_dict(resultrow):
|
|
||||||
return {
|
|
||||||
"time":resultrow.timestamp,
|
|
||||||
"track":track_db_to_dict(resultrow.track),
|
|
||||||
"duration":resultrow.duration,
|
|
||||||
"origin":resultrow.origin
|
|
||||||
}
|
|
||||||
|
|
||||||
def track_db_to_dict(resultrow):
|
|
||||||
return {
|
|
||||||
"artists":[],
|
|
||||||
"title":resultrow.title,
|
|
||||||
"album":{},
|
|
||||||
"length":resultrow.length
|
|
||||||
}
|
|
@ -6,6 +6,8 @@ from ..globalconf import data_dir
|
|||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
##### DB Technical
|
||||||
|
|
||||||
DB = {}
|
DB = {}
|
||||||
|
|
||||||
|
|
||||||
@ -42,9 +44,76 @@ DB['trackartists'] = sql.Table(
|
|||||||
|
|
||||||
meta.create_all(engine)
|
meta.create_all(engine)
|
||||||
|
|
||||||
|
##### DB <-> Dict translations
|
||||||
|
|
||||||
|
## ATTENTION ALL ADVENTURERS
|
||||||
|
## this is what a scrobble dict will look like from now on
|
||||||
|
## this is the single canonical source of truth
|
||||||
|
## stop making different little dicts in every single function
|
||||||
|
## this is the schema that will definitely 100% stay like this and not
|
||||||
|
## randomly get changed two versions later
|
||||||
|
## here we go
|
||||||
|
#
|
||||||
|
# {
|
||||||
|
# "time":int,
|
||||||
|
# "track":{
|
||||||
|
# "artists":list,
|
||||||
|
# "title":string,
|
||||||
|
# "album":{
|
||||||
|
# "name":string,
|
||||||
|
# "artists":list
|
||||||
|
# },
|
||||||
|
# "length":None
|
||||||
|
# },
|
||||||
|
# "duration":int,
|
||||||
|
# "origin":string,
|
||||||
|
# "extra":{string-keyed mapping for all flags with the scrobble}
|
||||||
|
# }
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
def scrobble_db_to_dict(row):
|
||||||
|
|
||||||
|
|
||||||
|
return {
|
||||||
|
"time":row.timestamp,
|
||||||
|
"track":get_track(row.track_id),
|
||||||
|
"duration":row.duration,
|
||||||
|
"origin":row.origin
|
||||||
|
}
|
||||||
|
|
||||||
|
def track_db_to_dict(row):
|
||||||
|
return {
|
||||||
|
"title":row.title,
|
||||||
|
"length":row.length
|
||||||
|
}
|
||||||
|
|
||||||
|
def artist_db_to_dict(row):
|
||||||
|
return row.name
|
||||||
|
|
||||||
|
def scrobble_dict_to_db(info):
|
||||||
|
return {
|
||||||
|
"rawscrobble":json.dumps(info),
|
||||||
|
"timestamp":info['time'],
|
||||||
|
"origin":info['origin'],
|
||||||
|
"duration":info['duration'],
|
||||||
|
"extra":info['extra'],
|
||||||
|
"track_id":get_track_id(info['track'])
|
||||||
|
}
|
||||||
|
|
||||||
|
def track_dict_to_db(info):
|
||||||
|
return {
|
||||||
|
"title":info['title'],
|
||||||
|
"title_normalized":normalize_name(info['title']),
|
||||||
|
"length":info['length']
|
||||||
|
}
|
||||||
|
|
||||||
|
def artist_dict_to_db(info):
|
||||||
|
return {
|
||||||
|
"name": info,
|
||||||
|
"name_normalized":normalize_name(info)
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@ -60,7 +129,7 @@ def add_scrobbles(scrobbleslist):
|
|||||||
rawscrobble=json.dumps(s),
|
rawscrobble=json.dumps(s),
|
||||||
timestamp=s['time'],
|
timestamp=s['time'],
|
||||||
origin=s['origin'],
|
origin=s['origin'],
|
||||||
duration=s['duration'] or -1,
|
duration=s['duration'],
|
||||||
track_id=get_track_id(s['track'])
|
track_id=get_track_id(s['track'])
|
||||||
) for s in scrobbleslist
|
) for s in scrobbleslist
|
||||||
]
|
]
|
||||||
@ -180,20 +249,51 @@ def get_scrobbles_of_track(track,since,to):
|
|||||||
|
|
||||||
|
|
||||||
def get_scrobbles(since,to):
|
def get_scrobbles(since,to):
|
||||||
|
print(since,to)
|
||||||
artist_id = get_artist_id(artist)
|
|
||||||
|
|
||||||
with engine.begin() as conn:
|
with engine.begin() as conn:
|
||||||
op = DB['scrobbles'].select().where(
|
op = DB['scrobbles'].select().where(
|
||||||
DB['scrobbles'].c.timestamp<=to,
|
DB['scrobbles'].c.timestamp<=to,
|
||||||
DB['scrobbles'].c.timestamp>=since,
|
DB['scrobbles'].c.timestamp>=since,
|
||||||
)
|
)
|
||||||
|
print(str(op))
|
||||||
result = conn.execute(op).all()
|
result = conn.execute(op).all()
|
||||||
|
|
||||||
print(result)
|
result = [scrobble_db_to_dict(row) for row in result]
|
||||||
|
return result
|
||||||
|
|
||||||
|
def get_track(id):
|
||||||
|
with engine.begin() as conn:
|
||||||
|
op = DB['tracks'].select().where(
|
||||||
|
DB['tracks'].c.id==id
|
||||||
|
)
|
||||||
|
result = conn.execute(op).all()
|
||||||
|
|
||||||
|
trackinfo = result[0]
|
||||||
|
|
||||||
|
|
||||||
|
with engine.begin() as conn:
|
||||||
|
op = DB['trackartists'].select().where(
|
||||||
|
DB['trackartists'].c.track_id==id
|
||||||
|
)
|
||||||
|
result = conn.execute(op).all()
|
||||||
|
|
||||||
|
artists = [get_artist(row.artist_id) for row in result]
|
||||||
|
|
||||||
|
result = track_db_to_dict(trackinfo)
|
||||||
|
result['artists'] = artists
|
||||||
return result
|
return result
|
||||||
|
|
||||||
|
|
||||||
|
def get_artist(id):
|
||||||
|
with engine.begin() as conn:
|
||||||
|
op = DB['artists'].select().where(
|
||||||
|
DB['artists'].c.id==id
|
||||||
|
)
|
||||||
|
result = conn.execute(op).all()
|
||||||
|
|
||||||
|
artistinfo = result[0]
|
||||||
|
return artist_db_to_dict(artistinfo)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
@ -27,6 +27,7 @@ def upgrade_db(callback_add_scrobbles):
|
|||||||
print(col['yellow']("Upgrading v2 Database to v3 Database. This could take a while..."))
|
print(col['yellow']("Upgrading v2 Database to v3 Database. This could take a while..."))
|
||||||
oldfolder = os.path.join(dir_settings['state'],"scrobbles")
|
oldfolder = os.path.join(dir_settings['state'],"scrobbles")
|
||||||
newfolder = os.path.join(dir_settings['state'],".oldscrobbles")
|
newfolder = os.path.join(dir_settings['state'],".oldscrobbles")
|
||||||
|
os.makedirs(newfolder,exist_ok=True)
|
||||||
if os.path.exists(oldfolder):
|
if os.path.exists(oldfolder):
|
||||||
scrobblefiles = os.listdir(oldfolder)
|
scrobblefiles = os.listdir(oldfolder)
|
||||||
for sf in scrobblefiles:
|
for sf in scrobblefiles:
|
||||||
@ -63,3 +64,4 @@ def upgrade_db(callback_add_scrobbles):
|
|||||||
})
|
})
|
||||||
callback_add_scrobbles(scrobblelist)
|
callback_add_scrobbles(scrobblelist)
|
||||||
os.rename(os.path.join(oldfolder,sf),os.path.join(newfolder,sf))
|
os.rename(os.path.join(oldfolder,sf),os.path.join(newfolder,sf))
|
||||||
|
print(col['yellow']("Done!"))
|
||||||
|
Loading…
x
Reference in New Issue
Block a user