Implement import for ghan CSV files, fix GH-382

This commit is contained in:
krateng 2025-02-05 19:31:09 +01:00
parent 7f774f03c4
commit 126d155208
2 changed files with 32 additions and 6 deletions

View File

@ -1,6 +1,8 @@
# server
from bottle import request, response, FormsDict
from ..pkg_global import conf
# decorator that makes sure this function is only run in normal operation,
# not when we run a task that needs to access the database
@ -932,6 +934,9 @@ def get_predefined_rulesets(dbconn=None):
def start_db():
conf.AUX_MODE = True # that is, without a doubt, the worst python code you have ever seen
# Upgrade database
from .. import upgrade
upgrade.upgrade_db(sqldb.add_scrobbles)
@ -950,6 +955,8 @@ def start_db():
dbstatus['healthy'] = True
conf.AUX_MODE = False # but you have seen it
# inform time module about begin of scrobbling
try:
firstscrobble = sqldb.get_scrobbles(limit=1)[0]

View File

@ -34,9 +34,12 @@ def import_scrobbles(inputf):
filename = os.path.basename(inputf)
importfunc = None
if re.match(r"recenttracks-.*\.csv", filename):
typeid, typedesc = "lastfm", "Last.fm (ghan CSV)"
importfunc = parse_lastfm_ghan_csv
if re.match(r".*\.csv", filename):
typeid,typedesc = "lastfm", "Last.fm (benjaminbenben export)"
elif re.match(r".*\.csv", filename):
typeid,typedesc = "lastfm", "Last.fm (benjaminbenben CSV)"
importfunc = parse_lastfm
elif re.match(r"Streaming_History_Audio.+\.json", filename):
@ -65,8 +68,8 @@ def import_scrobbles(inputf):
importfunc = parse_rockbox
elif re.match(r"recenttracks-.*\.json", filename):
typeid, typedesc = "lastfm", "Last.fm (ghan export)"
importfunc = parse_lastfm_ghan
typeid, typedesc = "lastfm", "Last.fm (ghan JSON)"
importfunc = parse_lastfm_ghan_json
elif re.match(r".*\.json",filename):
try:
@ -83,7 +86,8 @@ def import_scrobbles(inputf):
return result
print(f"Parsing {col['yellow'](inputf)} as {col['cyan'](typedesc)} export")
print(f"Parsing {col['yellow'](inputf)} as {col['cyan'](typedesc)} export.")
print(col['red']("Please double-check if this is correct - if the import fails, the file might have been interpreted as the wrong type."))
timestamps = set()
scrobblebuffer = []
@ -410,7 +414,7 @@ def parse_lastfm(inputf):
continue
def parse_lastfm_ghan(inputf):
def parse_lastfm_ghan_json(inputf):
with open(inputf, 'r') as inputfd:
data = json.load(inputfd)
@ -432,6 +436,21 @@ def parse_lastfm_ghan(inputf):
}, '')
def parse_lastfm_ghan_csv(inputf):
with open(inputf, 'r') as inputfd:
reader = csv.DictReader(inputfd)
for row in reader:
yield ('CONFIDENT_IMPORT', {
'track_title': row['track'],
'track_artists': row['artist'],
'track_length': None,
'album_name': row['album'],
'scrobble_time': int(row['uts']),
'scrobble_duration': None
}, '')
def parse_listenbrainz(inputf):
with open(inputf,'r') as inputfd: