Improved feedback of import

This commit is contained in:
krateng 2022-04-01 18:19:21 +02:00
parent d8821efeeb
commit a833039ced
2 changed files with 50 additions and 36 deletions

View File

@ -12,14 +12,22 @@ def loadexternal(filename):
return return
from .importer import import_scrobbles from .importer import import_scrobbles
imported,warning,skipped,failed = import_scrobbles(filename) result = import_scrobbles(filename)
print("Successfully imported",imported,"scrobbles!")
if warning > 0: msg = f"Successfully imported {result['CONFIDENT_IMPORT'] + result['UNCERTAIN_IMPORT']} scrobbles"
print(col['orange'](f"{warning} Warning{'s' if warning != 1 else ''}!")) if result['UNCERTAIN_IMPORT'] > 0:
if skipped > 0: warningmsg = col['orange'](f"{result['UNCERTAIN_IMPORT']} Warning{'s' if result['UNCERTAIN_IMPORT'] != 1 else ''}!")
print(col['orange'](f"{skipped} Skipped!")) msg += f" ({warningmsg})"
if failed > 0: print(msg)
print(col['red'](f"{failed} Error{'s' if failed != 1 else ''}!"))
msg = f"Skipped {result['CONFIDENT_SKIP'] + result['UNCERTAIN_SKIP']} scrobbles"
if result['UNCERTAIN_SKIP'] > 0:
warningmsg = col['orange'](f"{result['UNCERTAIN_SKIP']} Warning{'s' if result['UNCERTAIN_SKIP'] != 1 else ''}!")
msg += f" ({warningmsg})"
print(msg)
if result['FAIL'] > 0:
print(col['red'](f"{result['FAIL']} Error{'s' if result['FAIL'] != 1 else ''}!"))
def backuphere(): def backuphere():
from .backup import backup from .backup import backup

View File

@ -12,12 +12,22 @@ c = CleanerAgent()
def warn(msg): def warn(msg):
print(col['orange'](msg)) print(col['orange'](msg))
def skip(msg):
print(col['#ffcba4'](msg))
def err(msg): def err(msg):
print(col['red'](msg)) print(col['red'](msg))
def import_scrobbles(inputf): def import_scrobbles(inputf):
result = {
"CONFIDENT_IMPORT": 0,
"UNCERTAIN_IMPORT": 0,
"CONFIDENT_SKIP": 0,
"UNCERTAIN_SKIP": 0,
"FAIL": 0
}
filename = os.path.basename(inputf) filename = os.path.basename(inputf)
if re.match(".*\.csv",filename): if re.match(".*\.csv",filename):
@ -37,7 +47,7 @@ def import_scrobbles(inputf):
else: else:
print("File",inputf,"could not be identified as a valid import source.") print("File",inputf,"could not be identified as a valid import source.")
return 0,0,0,0 return result
print(f"Parsing {col['yellow'](inputf)} as {col['cyan'](type)} export") print(f"Parsing {col['yellow'](inputf)} as {col['cyan'](type)} export")
@ -47,7 +57,7 @@ def import_scrobbles(inputf):
while True: while True:
action = prompt(f"Already imported {type} data. [O]verwrite, [A]ppend or [C]ancel?",default='c').lower()[0] action = prompt(f"Already imported {type} data. [O]verwrite, [A]ppend or [C]ancel?",default='c').lower()[0]
if action == 'c': if action == 'c':
return 0,0,0,0 return result
elif action == 'a': elif action == 'a':
mode = 'a' mode = 'a'
break break
@ -61,18 +71,12 @@ def import_scrobbles(inputf):
with open(outputf,mode) as outputfd: with open(outputf,mode) as outputfd:
success, warning, skipped, failed = 0, 0, 0, 0
timestamps = set() timestamps = set()
for status,scrobble in importfunc(inputf): for status,scrobble in importfunc(inputf):
if status == 'FAIL': result[status] += 1
failed += 1 if status in ['CONFIDENT_IMPORT','UNCERTAIN_IMPORT']:
elif status == 'SKIP':
skipped += 1
else:
success += 1
if status == 'WARN':
warning += 1
while scrobble['timestamp'] in timestamps: while scrobble['timestamp'] in timestamps:
scrobble['timestamp'] += 1 scrobble['timestamp'] += 1
@ -93,10 +97,10 @@ def import_scrobbles(inputf):
]) ])
outputfd.write(outputline + '\n') outputfd.write(outputline + '\n')
if success % 100 == 0: if (result['CONFIDENT_IMPORT'] + result['UNCERTAIN_IMPORT']) % 100 == 0:
print(f"Imported {success} scrobbles...") print(f"Imported {result['CONFIDENT_IMPORT'] + result['UNCERTAIN_IMPORT']} scrobbles...")
return success, warning, skipped, failed return result
def parse_spotify_lite(inputf): def parse_spotify_lite(inputf):
inputfolder = os.path.dirname(inputf) inputfolder = os.path.dirname(inputf)
@ -145,26 +149,28 @@ def parse_spotify_full(inputf):
if title is None: if title is None:
warn(f"{entry} has no title, skipping...") skip(f"{entry} has no title, skipping...")
yield ('SKIP',None) yield ('CONFIDENT_SKIP',None)
continue continue
if artist is None: if artist is None:
warn(f"{entry} has no artist, skipping...") skip(f"{entry} has no artist, skipping...")
yield ('SKIP',None) yield ('CONFIDENT_SKIP',None)
continue continue
if played < 30: if played < 30:
warn(f"{entry} is shorter than 30 seconds, skipping...") skip(f"{entry} is shorter than 30 seconds, skipping...")
yield ('SKIP',None) yield ('CONFIDENT_SKIP',None)
continue continue
# if offline_timestamp is a proper number, we treat it as # if offline_timestamp is a proper number, we treat it as
# accurate and check duplicates by that exact timestamp # accurate and check duplicates by that exact timestamp
if timestamp != 0: if timestamp != 0:
status = 'SUCCESS'
if timestamp in timestamps and (artist,title) in timestamps[timestamp]: if timestamp in timestamps and (artist,title) in timestamps[timestamp]:
warn(f"{entry} seems to be a duplicate, skipping...") skip(f"{entry} seems to be a duplicate, skipping...")
yield ('SKIP',None) yield ('CONFIDENT_SKIP',None)
continue continue
else:
status = 'CONFIDENT_IMPORT'
timestamps.setdefault(timestamp,[]).append((artist,title)) timestamps.setdefault(timestamp,[]).append((artist,title))
# if it's 0, we use ts instead, but identify duplicates differently # if it's 0, we use ts instead, but identify duplicates differently
@ -188,13 +194,13 @@ def parse_spotify_full(inputf):
# - exact same track uri # - exact same track uri
# - exact same ms_played # - exact same ms_played
if (abs(scr[0] - timestamp) < 30) and scr[1:] == scrobble_describe[1:]: if (abs(scr[0] - timestamp) < 30) and scr[1:] == scrobble_describe[1:]:
warn(f"{entry} has been identified as potential duplicate, skipping...") warn(f"{entry} might be a duplicate, skipping...")
yield ('SKIP',None) yield ('UNCERTAIN_SKIP',None)
found_similar = True found_similar = True
break break
else: else:
# no duplicates, assume proper scrobble but warn # no duplicates, assume proper scrobble but warn
status = 'WARN' status = 'UNCERTAIN_IMPORT'
warn(f"{entry} might have an inaccurate timestamp.") warn(f"{entry} might have an inaccurate timestamp.")
inaccurate_timestamps.setdefault(ts_group,[]).append(scrobble_describe) inaccurate_timestamps.setdefault(ts_group,[]).append(scrobble_describe)
@ -230,7 +236,7 @@ def parse_lastfm(inputf):
continue continue
try: try:
yield ('SUCCESS',{ yield ('CONFIDENT_IMPORT',{
'title': title, 'title': title,
'artiststr': artist, 'artiststr': artist,
'album': album, 'album': album,