mirror of
https://github.com/krateng/maloja.git
synced 2025-08-11 09:13:43 -04:00
Implemented associated artists
This commit is contained in:
parent
65a076c249
commit
632905a1c7
@ -4,7 +4,7 @@ from bottle import request, response, FormsDict, HTTPError
|
|||||||
# rest of the project
|
# rest of the project
|
||||||
from ..cleanup import CleanerAgent, CollectorAgent
|
from ..cleanup import CleanerAgent, CollectorAgent
|
||||||
from .. import utilities
|
from .. import utilities
|
||||||
from ..malojatime import register_scrobbletime, time_stamps, ranges
|
from ..malojatime import register_scrobbletime, time_stamps, ranges, alltime
|
||||||
from ..malojauri import uri_to_internal, internal_to_uri, compose_querystring
|
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
|
||||||
@ -225,30 +225,28 @@ def get_top_tracks(**keys):
|
|||||||
|
|
||||||
return results
|
return results
|
||||||
|
|
||||||
|
@waitfordb
|
||||||
def artist_info(artist):
|
def artist_info(artist):
|
||||||
|
|
||||||
charts = db_aggregate(by="ARTIST")
|
alltimecharts = get_charts_artists(timerange=alltime())
|
||||||
scrobbles = len(db_query(artists=[artist]))
|
scrobbles = get_scrobbles_num(artist=artist,timerange=alltime())
|
||||||
#we cant take the scrobble number from the charts because that includes all countas scrobbles
|
#we cant take the scrobble number from the charts because that includes all countas scrobbles
|
||||||
try:
|
try:
|
||||||
c = [e for e in charts if e["artist"] == artist][0]
|
c = [e for e in alltimecharts if e["artist"] == artist][0]
|
||||||
others = [a for a in coa.getAllAssociated(artist) if a in ARTISTS]
|
others = sqldb.get_associated_artists(artist)
|
||||||
position = c["rank"]
|
position = c["rank"]
|
||||||
performance = get_performance(artist=artist,step="week")
|
performance = get_performance(artist=artist,step="week")
|
||||||
return {
|
return {
|
||||||
"artist":artist,
|
"artist":artist,
|
||||||
"scrobbles":scrobbles,
|
"scrobbles":scrobbles,
|
||||||
"position":position,
|
"position":position,
|
||||||
"associated":others,
|
"associated":others
|
||||||
"medals":{"gold":[],"silver":[],"bronze":[],**MEDALS_ARTISTS.get(artist,{})},
|
|
||||||
"topweeks":WEEKLY_TOPARTISTS.get(artist,0)
|
|
||||||
}
|
}
|
||||||
except:
|
except:
|
||||||
# if the artist isnt in the charts, they are not being credited and we
|
# if the artist isnt in the charts, they are not being credited and we
|
||||||
# need to show information about the credited one
|
# need to show information about the credited one
|
||||||
artist = coa.getCredited(artist)
|
artist = sqldb.get_credited_artists(artist)[0]
|
||||||
c = [e for e in charts if e["artist"] == artist][0]
|
c = [e for e in alltimecharts if e["artist"] == artist][0]
|
||||||
position = c["rank"]
|
position = c["rank"]
|
||||||
return {"replace":artist,"scrobbles":scrobbles,"position":position}
|
return {"replace":artist,"scrobbles":scrobbles,"position":position}
|
||||||
|
|
||||||
@ -494,6 +492,10 @@ def get_predefined_rulesets():
|
|||||||
def start_db():
|
def start_db():
|
||||||
from .. import upgrade
|
from .. import upgrade
|
||||||
upgrade.upgrade_db(sqldb.add_scrobbles)
|
upgrade.upgrade_db(sqldb.add_scrobbles)
|
||||||
|
|
||||||
|
from . import associated
|
||||||
|
associated.load_associated_rules()
|
||||||
|
|
||||||
dbstatus['healthy'] = True
|
dbstatus['healthy'] = True
|
||||||
dbstatus['complete'] = True
|
dbstatus['complete'] = True
|
||||||
|
|
||||||
|
36
maloja/database/associated.py
Normal file
36
maloja/database/associated.py
Normal file
@ -0,0 +1,36 @@
|
|||||||
|
## dealing with loading the associated artists rules into a database
|
||||||
|
## right now this is kind of absurd because we're storing it in a db while not
|
||||||
|
## actually using its permanence, but this makes it possible to use the information
|
||||||
|
## directly in sql
|
||||||
|
|
||||||
|
|
||||||
|
from doreah import tsv
|
||||||
|
|
||||||
|
|
||||||
|
from . import sqldb
|
||||||
|
from ..globalconf import data_dir
|
||||||
|
|
||||||
|
|
||||||
|
def load_associated_rules():
|
||||||
|
# delete old
|
||||||
|
with sqldb.engine.begin() as conn:
|
||||||
|
op = sqldb.DB['associated_artists'].delete().where()
|
||||||
|
conn.execute(op)
|
||||||
|
|
||||||
|
# load from file
|
||||||
|
raw = tsv.parse_all(data_dir["rules"](),"string","string","string")
|
||||||
|
rules = [{'source_artist':b,'target_artist':c} for [a,b,c] in raw if a=="countas"]
|
||||||
|
#allartists = set([*[r['source_artist'] for r in rules],*[r['target_artist'] for r in rules]])
|
||||||
|
|
||||||
|
# find ids
|
||||||
|
rules = [{k:sqldb.get_artist_id(rule[k]) for k in rule} for rule in rules]
|
||||||
|
|
||||||
|
# write to db
|
||||||
|
ops = [
|
||||||
|
sqldb.DB['associated_artists'].insert().values(**r)
|
||||||
|
for r in rules
|
||||||
|
]
|
||||||
|
|
||||||
|
with sqldb.engine.begin() as conn:
|
||||||
|
for op in ops:
|
||||||
|
conn.execute(op)
|
@ -488,8 +488,43 @@ def get_artists_map(artist_ids):
|
|||||||
return artists
|
return artists
|
||||||
|
|
||||||
|
|
||||||
|
### associations
|
||||||
|
|
||||||
|
def get_associated_artists(*artists):
|
||||||
|
artist_ids = [get_artist_id(a) for a in artists]
|
||||||
|
|
||||||
|
jointable = sql.join(
|
||||||
|
DB['associated_artists'],
|
||||||
|
DB['artists'],
|
||||||
|
DB['associated_artists'].c.source_artist == DB['artists'].c.id
|
||||||
|
)
|
||||||
|
|
||||||
|
with engine.begin() as conn:
|
||||||
|
op = jointable.select().where(
|
||||||
|
DB['associated_artists'].c.target_artist.in_(artist_ids)
|
||||||
|
)
|
||||||
|
result = conn.execute(op).all()
|
||||||
|
|
||||||
|
artists = artists_db_to_dict(result)
|
||||||
|
return artists
|
||||||
|
|
||||||
|
def get_credited_artists(*artists):
|
||||||
|
artist_ids = [get_artist_id(a) for a in artists]
|
||||||
|
|
||||||
|
jointable = sql.join(
|
||||||
|
DB['associated_artists'],
|
||||||
|
DB['artists'],
|
||||||
|
DB['associated_artists'].c.target_artist == DB['artists'].c.id
|
||||||
|
)
|
||||||
|
|
||||||
|
with engine.begin() as conn:
|
||||||
|
op = jointable.select().where(
|
||||||
|
DB['associated_artists'].c.source_artist.in_(artist_ids)
|
||||||
|
)
|
||||||
|
result = conn.execute(op).all()
|
||||||
|
|
||||||
|
artists = artists_db_to_dict(result)
|
||||||
|
return artists
|
||||||
|
|
||||||
|
|
||||||
### get a specific entity by id
|
### get a specific entity by id
|
||||||
|
@ -24,14 +24,15 @@ def upgrade_apikeys():
|
|||||||
|
|
||||||
|
|
||||||
def upgrade_db(callback_add_scrobbles):
|
def upgrade_db(callback_add_scrobbles):
|
||||||
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)
|
os.makedirs(newfolder,exist_ok=True)
|
||||||
if os.path.exists(oldfolder):
|
if os.path.exists(oldfolder):
|
||||||
scrobblefiles = os.listdir(oldfolder)
|
scrobblefiles = [f for f in os.listdir(oldfolder) if f.endswith(".tsv")]
|
||||||
for sf in scrobblefiles:
|
if len(scrobblefiles) > 0:
|
||||||
if sf.endswith(".tsv"):
|
print(col['yellow']("Upgrading v2 Database to v3 Database. This could take a while..."))
|
||||||
|
for sf in scrobblefiles:
|
||||||
print(f"\tImporting from old tsv scrobble file: {sf}")
|
print(f"\tImporting from old tsv scrobble file: {sf}")
|
||||||
if re.match(r"[0-9]+_[0-9]+\.tsv",sf):
|
if re.match(r"[0-9]+_[0-9]+\.tsv",sf):
|
||||||
origin = 'legacy'
|
origin = 'legacy'
|
||||||
@ -64,4 +65,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!"))
|
print(col['yellow']("Done!"))
|
||||||
|
Loading…
x
Reference in New Issue
Block a user