diff --git a/zwift_offline.py b/zwift_offline.py index 3891bae9..c4e5020a 100644 --- a/zwift_offline.py +++ b/zwift_offline.py @@ -25,7 +25,7 @@ from copy import deepcopy from functools import wraps from io import BytesIO -from shutil import copyfile +from shutil import copyfile, rmtree from urllib.parse import quote from flask import Flask, request, jsonify, redirect, render_template, url_for, flash, session, make_response, send_file, send_from_directory from flask_login import UserMixin, AnonymousUserMixin, LoginManager, login_user, current_user, login_required, logout_user @@ -896,15 +896,8 @@ def garmin(username): if request.form['username'] == "" or request.form['password'] == "": flash("Garmin credentials can't be empty.") return render_template("garmin.html", username=current_user.username) - try: - import garth - garth.login(request.form['username'], request.form['password']) - garth.save('%s/%s/garth' % (STORAGE_DIR, current_user.player_id)) - except Exception as exc: - logger.warning('garth: %s' % repr(exc)) - flash("Invalid username or password.") - return render_template("garmin.html", username=current_user.username) encrypt_credentials(file, (request.form['username'], request.form['password'])) + rmtree('%s/%s/garth' % (STORAGE_DIR, current_user.player_id), ignore_errors=True) return redirect(url_for('settings', username=current_user.username)) cred = decrypt_credentials(file) return render_template("garmin.html", username=current_user.username, uname=cred[0], passw=cred[1]) @@ -2260,13 +2253,15 @@ def garmin_upload(player_id, activity): except Exception as exc: logger.warning("Failed to read %s. Skipping Garmin upload attempt: %s" % (garmin_credentials, repr(exc))) return - garmin_domain = '%s/garmin_domain.txt' % STORAGE_DIR - if os.path.exists(garmin_domain): + domain = 'garmin.com' + domain_file = '%s/garmin_domain.txt' % STORAGE_DIR + if os.path.exists(domain_file): try: - with open(garmin_domain) as f: - garth.configure(domain=f.readline().rstrip('\r\n')) + with open(domain_file) as f: + domain = f.readline().rstrip('\r\n') + garth.configure(domain=domain) except Exception as exc: - logger.warning("Failed to read %s: %s" % (garmin_domain, repr(exc))) + logger.warning("Failed to read %s: %s" % (domain_file, repr(exc))) tokens_dir = '%s/garth' % profile_dir try: garth.resume(tokens_dir) @@ -2274,7 +2269,7 @@ def garmin_upload(player_id, activity): if not garth.client.oauth2_token or garth.client.oauth2_token.expired: garth.client.refresh_oauth2() garth.save(tokens_dir) - assert 'userName' in requests.get('https://connectapi.garmin.com/userprofile-service/socialProfile', headers={'authorization': str(garth.client.oauth2_token)}).json() + assert 'userName' in requests.get('https://connectapi.%s/userprofile-service/socialProfile' % domain, headers={'authorization': str(garth.client.oauth2_token)}).json() except: try: garth.login(username, password) @@ -2283,7 +2278,7 @@ def garmin_upload(player_id, activity): logger.warning("Garmin login failed: %s" % repr(exc)) return try: - requests.post('https://connectapi.garmin.com/upload-service/upload', files={"file": (activity.fit_filename, BytesIO(activity.fit))}, headers={'authorization': str(garth.client.oauth2_token)}) + requests.post('https://connectapi.%s/upload-service/upload' % domain, files={"file": (activity.fit_filename, BytesIO(activity.fit))}, headers={'authorization': str(garth.client.oauth2_token)}) except Exception as exc: logger.warning("Garmin upload failed. No internet? %s" % repr(exc))