From 38f2173bde858c888f188ed25f980d4051a4ff02 Mon Sep 17 00:00:00 2001 From: krateng Date: Tue, 29 Mar 2022 17:27:34 +0200 Subject: [PATCH] Added handling for invalid Spotify scrobbles --- maloja/__pkginfo__.py | 2 +- maloja/proccontrol/tasks/__init__.py | 4 +- maloja/proccontrol/tasks/importer.py | 55 ++++++++++++++++++---------- 3 files changed, 39 insertions(+), 22 deletions(-) diff --git a/maloja/__pkginfo__.py b/maloja/__pkginfo__.py index 9bfb1d5..9e1199e 100644 --- a/maloja/__pkginfo__.py +++ b/maloja/__pkginfo__.py @@ -4,7 +4,7 @@ # you know what f*ck it # this is hardcoded for now because of that damn project / package name discrepancy # i'll fix it one day -VERSION = "2.14.6" +VERSION = "2.14.7" HOMEPAGE = "https://github.com/krateng/maloja" diff --git a/maloja/proccontrol/tasks/__init__.py b/maloja/proccontrol/tasks/__init__.py index 837a2e4..b8af5f3 100644 --- a/maloja/proccontrol/tasks/__init__.py +++ b/maloja/proccontrol/tasks/__init__.py @@ -14,8 +14,10 @@ def loadexternal(filename): print("Please wait...") from .importer import import_scrobbles - imported,failed = import_scrobbles(filename) + imported,failed,warning = import_scrobbles(filename) print("Successfully imported",imported,"scrobbles!") + if warning > 0: + print(col['orange'](str(warning) + " Warnings!")) if failed > 0: print(col['red'](str(failed) + " Errors!")) diff --git a/maloja/proccontrol/tasks/importer.py b/maloja/proccontrol/tasks/importer.py index e24f740..e3d571e 100644 --- a/maloja/proccontrol/tasks/importer.py +++ b/maloja/proccontrol/tasks/importer.py @@ -39,11 +39,14 @@ def import_scrobbles(inputf): with open(outputf,"w") as outputfd: success = 0 failed = 0 + warning = 0 timestamps = set() for scrobble in importfunc(inputf): if scrobble is None: failed += 1 + if scrobble is False: + warning += 1 else: success += 1 @@ -73,7 +76,7 @@ def import_scrobbles(inputf): if success % 100 == 0: print(f"Imported {success} scrobbles...") - return success,failed + return success,failed,warning def parse_spotify(inputf): @@ -84,22 +87,34 @@ def parse_spotify(inputf): sec = int(entry['ms_played'] / 1000) - if sec > 30: - try: - yield { - 'title':entry['master_metadata_track_name'], - 'artiststr': entry['master_metadata_album_artist_name'], - 'album': entry['master_metadata_album_album_name'], - 'timestamp': int(datetime.datetime.strptime( - entry['ts'].replace('Z','+0000',), - "%Y-%m-%dT%H:%M:%S%z" - ).timestamp()), - 'duration':sec - } - except: - print(col['red'](str(entry) + " could not be parsed. Scrobble not imported.")) - yield None - continue + if entry['master_metadata_track_name'] is None: + print(col['orange'](f"{entry} has no title, skipping...")) + yield False + continue + if entry['master_metadata_album_artist_name'] is None: + print(col['orange'](f"{entry} has no artist, skipping...")) + yield False + continue + if sec < 30: + print(col['orange'](f"{entry} is shorter than 30 seconds, skipping...")) + yield False + continue + + try: + yield { + 'title':entry['master_metadata_track_name'], + 'artiststr': entry['master_metadata_album_artist_name'], + 'album': entry['master_metadata_album_album_name'], + 'timestamp': int(datetime.datetime.strptime( + entry['ts'].replace('Z','+0000',), + "%Y-%m-%dT%H:%M:%S%z" + ).timestamp()), + 'duration':sec + } + except Exception as e: + print(col['red'](f"{entry} could not be parsed. Scrobble not imported. ({repr(e)})")) + yield None + continue def parse_lastfm(inputf): @@ -110,7 +125,7 @@ def parse_lastfm(inputf): try: artist,album,title,time = row except ValueError: - print(col['red'](str(row) + " does not look like a valid entry. Scrobble not imported.")) + print(col['red'](f"{row} does not look like a valid entry. Scrobble not imported.")) yield None continue @@ -125,7 +140,7 @@ def parse_lastfm(inputf): ).timestamp()), 'duration':None } - except: - print(col['red'](str(row) + " could not be parsed. Scrobble not imported.")) + except Exception as e: + print(col['red'](f"{entry} could not be parsed. Scrobble not imported. ({repr(e)})")) yield None continue