Skip to content

Commit

Permalink
Make logging-in possible without username/password
Browse files Browse the repository at this point in the history
Prompt for username/password only when a new login is required
  • Loading branch information
pe-st committed Feb 25, 2024
1 parent e729cdf commit b994968
Showing 1 changed file with 12 additions and 13 deletions.
25 changes: 12 additions & 13 deletions gcexport.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,14 +46,15 @@

# PyPI imports
import garth
from garth.exc import GarthException

# Local application/library specific imports
from filtering import read_exclude, update_download_stats

COOKIE_JAR = http.cookiejar.CookieJar()
OPENER = urllib.request.build_opener(urllib.request.HTTPCookieProcessor(COOKIE_JAR), urllib.request.HTTPSHandler(debuglevel=0))

SCRIPT_VERSION = '4.2.0'
SCRIPT_VERSION = '4.3.0-Beta'

# This version here should correspond to what is written in CONTRIBUTING.md#python-3x-versions
MINIMUM_PYTHON_VERSION = (3, 8)
Expand Down Expand Up @@ -484,41 +485,39 @@ def login_to_garmin_connect(args):
"""
Perform all HTTP requests to login to Garmin Connect.
"""
username = args.username if args.username else input('Username: ')
password = args.password if args.password else getpass()
garth_session_directory = args.session if args.session else None

print('Authenticating using OAuth...', end=' ')
print('Authenticating...', end='')
try:
login_required = False

# try to load data if a session directory is given
if garth_session_directory:
try:
garth.resume(garth_session_directory)
except Exception:
# error during loading the session, a new login is required
except GarthException as ex:
logging.debug("Could not resume session, error: %s", ex)
login_required = True
pass
try:
garth.client.username
except Exception:
# session expired, a new login is required
except GarthException as ex:
logging.debug("Session expired, error: %s", ex)
login_required = True
pass
logging.info("Authenticating using OAuth token from %s", garth_session_directory)
else:
login_required = True

if login_required:
username = args.username if args.username else input('Username: ')
password = args.password if args.password else getpass()
garth.login(username, password)

# try to store data if a session directory is given
if garth_session_directory:
try:
garth.save(garth_session_directory)
except Exception:
print('Unable to store session data to ' + garth_session_directory)
pass
except GarthException as ex:
logging.warning("Unable to store session data to %s, error: %s", garth_session_directory, ex)

except Exception as ex:
raise GarminException(f'Authentication failure ({ex}). Did you enter correct credentials?') from ex
Expand Down

0 comments on commit b994968

Please sign in to comment.