From 7f7a301bd990e4196f45f5497d79a0e4ec6d9b12 Mon Sep 17 00:00:00 2001 From: oldnapalm <38410858+oldnapalm@users.noreply.github.com> Date: Tue, 2 Apr 2024 12:38:17 -0300 Subject: [PATCH] Backup profile and achievements files before overwriting when downloading from Zwift or uploading --- zwift_offline.py | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/zwift_offline.py b/zwift_offline.py index bbc434e4..8f8b9143 100644 --- a/zwift_offline.py +++ b/zwift_offline.py @@ -775,6 +775,10 @@ def decrypt_credentials(file): return cred +def backup_file(file): + if os.path.isfile(file): + copyfile(file, "%s-%s.bak" % (file, datetime.datetime.now(datetime.timezone.utc).strftime("%Y-%m-%d-%H-%M-%S"))) + @app.route("/profile//", methods=["GET", "POST"]) @login_required def profile(username): @@ -796,11 +800,15 @@ def profile(username): try: if request.form.get("zwift_profile"): profile = online_sync.query(session, access_token, "api/profiles/me") - with open('%s/profile.bin' % profile_dir, 'wb') as f: + profile_file = '%s/profile.bin' % profile_dir + backup_file(profile_file) + with open(profile_file, 'wb') as f: f.write(profile) if request.form.get("achievements"): achievements = online_sync.query(session, access_token, "achievement/loadPlayerAchievements") - with open('%s/achievements.bin' % profile_dir, 'wb') as f: + achievements_file = '%s/achievements.bin' % profile_dir + backup_file(achievements_file) + with open(achievements_file, 'wb') as f: f.write(achievements) online_sync.logout(session, refresh_token) if request.form.get("save_zwift"): @@ -943,6 +951,7 @@ def settings(username): uploaded_file = request.files['file'] if uploaded_file.filename in ['profile.bin', 'achievements.bin']: file_path = os.path.join(profile_dir, uploaded_file.filename) + backup_file(file_path) uploaded_file.save(file_path) else: flash("Invalid file name.")