Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add argument to load/store Garmin SSO data #99

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ usage: gcexport.py [-h] [--version] [-v] [--username USERNAME]
[-f {gpx,tcx,original,json}] [-d DIRECTORY] [-s SUBDIR]
[-lp LOGPATH] [-u] [-ot] [--desc [DESC]] [-t TEMPLATE]
[-fp] [-sa START_ACTIVITY_NO] [-ex FILE]
[-ss DIRECTORY]

Garmin Connect Exporter

Expand Down Expand Up @@ -96,6 +97,8 @@ optional arguments:
-ex FILE, --exclude FILE
JSON file with Array of activity IDs to exclude from download.
Format example: {"ids": ["6176888711"]}
-ss DIRECTORY, --session DIRECTORY
enable loading and storing SSO information from/to given directory
```

### Examples
Expand Down
34 changes: 33 additions & 1 deletion gcexport.py
Original file line number Diff line number Diff line change
Expand Up @@ -473,6 +473,8 @@ def parse_arguments(argv):
help='give index for first activity to import, i.e. skipping the newest activities')
parser.add_argument('-ex', '--exclude', metavar='FILE',
help='JSON file with Array of activity IDs to exclude from download. Format example: {"ids": ["6176888711"]}')
parser.add_argument('-ss', '--session', metavar='DIRECTORY',
help='enable loading and storing SSO information from/to given directory')
# fmt: on

return parser.parse_args(argv[1:])
Expand All @@ -484,10 +486,40 @@ def login_to_garmin_connect(args):
"""
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=' ')
try:
garth.login(username, password)
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
login_required = True
pass
try:
garth.client.username
except Exception:
# session expired, a new login is required
login_required = True
pass
else:
login_required = True

if login_required:
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 Exception as ex:
raise GarminException(f'Authentication failure ({ex}). Did you enter correct credentials?') from ex
print(' Done.')
Expand Down