From 533fb098e342499403d039428917b97ef196254f Mon Sep 17 00:00:00 2001 From: Riccardo Berto Date: Wed, 24 Feb 2021 17:20:27 +0100 Subject: [PATCH] extrapolate owner from filename --- main.py | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/main.py b/main.py index 3d28532..2c23ffb 100644 --- a/main.py +++ b/main.py @@ -58,10 +58,12 @@ def get_gpx_files_from_mail(): mail.logout() -def process_gpx_files(tx: Connection, owner: str): +def process_gpx_files(tx: Connection): for filepath in glob('gpx_files/*.gpx'): - filename = os.path.split(filepath)[-1] - if list(db.execute(text('select exists(select from training where filename = :filename)'), dict(filename=filename)))[0][0]: + owner = os.path.split(filepath)[-1].split('_workout-')[0] + filename = f'workout-{os.path.split(filepath)[-1].split("_workout-")[1]}' + if list(db.execute(text('select exists(select from training where owner = :owner and filename = :filename)'), + dict(owner=owner, filename=filename,),),)[0][0]: continue with open(filepath) as f: gpx_file = gpxpy.parse(f) @@ -95,15 +97,15 @@ def process_gpx_files(tx: Connection, owner: str): elevation=point.elevation,),) -def main(owner: str): +def main(): init_database() get_gpx_files_from_mail() - db.transaction(process_gpx_files, owner) + db.transaction(process_gpx_files) if __name__ == '__main__': try: - main(sys.argv[1]) + main() except IndexError: print('Run the script with "python main.py OWNER_NAME"') except (KeyboardInterrupt, EOFError):