Skip to content

Commit

Permalink
Use configured domain
Browse files Browse the repository at this point in the history
  • Loading branch information
oldnapalm committed Nov 29, 2024
1 parent f1d9647 commit 2f0c09d
Showing 1 changed file with 11 additions and 16 deletions.
27 changes: 11 additions & 16 deletions zwift_offline.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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])
Expand Down Expand Up @@ -2260,21 +2253,23 @@ 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)
assert garth.client.oauth1_token
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)
Expand All @@ -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))

Expand Down

0 comments on commit 2f0c09d

Please sign in to comment.