From 472281230ccf048618be92fbe67cf23492740ff9 Mon Sep 17 00:00:00 2001
From: krateng <git.noreply@krateng.ch>
Date: Thu, 28 Dec 2023 02:05:22 +0100
Subject: [PATCH] Make Maloja export file recognition more resilient, fix
 GH-309

---
 maloja/proccontrol/tasks/export.py           |  3 ++-
 maloja/proccontrol/tasks/import_scrobbles.py | 14 +++++++++++++-
 2 files changed, 15 insertions(+), 2 deletions(-)

diff --git a/maloja/proccontrol/tasks/export.py b/maloja/proccontrol/tasks/export.py
index a1e2a2b..2b68919 100644
--- a/maloja/proccontrol/tasks/export.py
+++ b/maloja/proccontrol/tasks/export.py
@@ -12,11 +12,12 @@ def export(targetfolder=None):
 		targetfolder = os.getcwd()
 
 	timestr = time.strftime("%Y_%m_%d_%H_%M_%S")
+	timestamp = int(time.time()) # ok this is technically a separate time get from above, but those ms are not gonna matter, and im too lazy to change it all to datetime
 	filename = f"maloja_export_{timestr}.json"
 	outputfile = os.path.join(targetfolder,filename)
 	assert not os.path.exists(outputfile)
 
-	data = {'scrobbles':get_scrobbles()}
+	data = {'maloja':{'export_time': timestamp },'scrobbles':get_scrobbles()}
 	with open(outputfile,'w') as outfd:
 		json.dump(data,outfd,indent=3)
 
diff --git a/maloja/proccontrol/tasks/import_scrobbles.py b/maloja/proccontrol/tasks/import_scrobbles.py
index 245a49b..986b86f 100644
--- a/maloja/proccontrol/tasks/import_scrobbles.py
+++ b/maloja/proccontrol/tasks/import_scrobbles.py
@@ -32,6 +32,8 @@ def import_scrobbles(inputf):
 	}
 
 	filename = os.path.basename(inputf)
+	importfunc = None
+
 
 	if re.match(r".*\.csv",filename):
 		typeid,typedesc = "lastfm","Last.fm"
@@ -62,7 +64,17 @@ def import_scrobbles(inputf):
 		typeid,typedesc = "rockbox","Rockbox"
 		importfunc = parse_rockbox
 
-	else:
+	elif re.match(r".*\.json",filename):
+		try:
+			with open(filename,'r') as fd:
+				data = json.load(fd)
+			if 'maloja' in data:
+				typeid,typedesc = "maloja","Maloja"
+				importfunc = parse_maloja
+		except Exception:
+			pass
+
+	if not importfunc:
 		print("File",inputf,"could not be identified as a valid import source.")
 		return result