Skip to content

Commit

Permalink
Store garth session and upload via garth
Browse files Browse the repository at this point in the history
  • Loading branch information
jrast committed Sep 28, 2023
1 parent 43f036e commit 64d8ec1
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 18 deletions.
34 changes: 18 additions & 16 deletions withings_sync/garmin.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
"""This module handles the Garmin connectivity."""
import logging
import cloudscraper
import garth

import os

log = logging.getLogger("garmin")

Expand All @@ -22,31 +21,34 @@ class APIException(Exception):
class GarminConnect:
"""Main GarminConnect class"""

UPLOAD_URL = "https://connect.garmin.com/upload-service/upload/.fit"

# From https://github.com/cpfair/tapiriik
@staticmethod
def get_session(email=None, password=None):
"""tapiriik get_session code"""
session = cloudscraper.CloudScraper()

try:
garth.login(email, password)
except Exception as ex:
raise APIException("Authentication failure: {}. Did you enter correct credentials?".format(ex))
logged_in = False
if os.path.exists('./garmin_session'):
garth.resume('./garmin_session')
try:
garth.client.username
logged_in = True
except Exception:
pass

if not logged_in:
try:
garth.login(email, password)
garth.save('./garmin_session')
except Exception as ex:
raise APIException("Authentication failure: {}. Did you enter correct credentials?".format(ex))

session.headers.update({'NK': 'NT', 'authorization': garth.client.oauth2_token.__str__(), 'di-backend': 'connectapi.garmin.com'})
return session

@staticmethod
def login(username, password):
"""login to Garmin"""
return GarminConnect.get_session(email=username, password=password)

def upload_file(self, ffile, session):
def upload_file(self, ffile):
"""upload fit file to Garmin connect"""
files = {"data": ("withings.fit", ffile)}
res = session.post(self.UPLOAD_URL, files=files, headers={"nk": "NT"})
res = garth.client.post('connect', '/upload-service/upload/.fit', files=files, api=True, headers={'di-backend': 'connectapi.garmin.com'})
try:
resp = res.json()
if "detailedImportResult" not in resp:
Expand Down
4 changes: 2 additions & 2 deletions withings_sync/sync.py
Original file line number Diff line number Diff line change
Expand Up @@ -159,8 +159,8 @@ def date_parser(date_string):
def sync_garmin(fit_file):
"""Sync generated fit file to Garmin Connect"""
garmin = GarminConnect()
session = garmin.login(ARGS.garmin_username, ARGS.garmin_password)
return garmin.upload_file(fit_file.getvalue(), session)
garmin.login(ARGS.garmin_username, ARGS.garmin_password)
return garmin.upload_file(fit_file.getvalue())


def sync_trainerroad(last_weight):
Expand Down

0 comments on commit 64d8ec1

Please sign in to comment.