jaroslawhartman / withings-sync

Synchronisation of Withings weight
MIT License
455 stars 89 forks source link

Store the garth session information and upload the fit file via garth #132

Closed jrast closed 1 year ago

jrast commented 1 year ago

To reduce the requests during garmin login, the garth session information is stored similar to the withing session data. Also garth is used to upload the fit-file and cloudscrapper is no longer required. I don't know if we can get rid of cloudscrapper or if there is still an advantage if the upload is done with cloudscrapper.

As with my other PR: If you like these changes let me know and I can cleanup. For example the directory in which the garth session is stored should be defined at a central place.

stynoo commented 1 year ago

Imho, it looks like this is the way to go. Garth does all the heavy lifting and should be able to handle MFA as well (not sure if extra code is needed to save the oauth1/2 tokens somewhere) The directory should ideally be the same place where .withings_user.json is stored. Iirc it's /root in the containers.

As for cloudscraper: this was a drop in replacement that fixed the last auth issues. If Garth works without it then I don't see a reason to keep it.

longstone commented 1 year ago

As with my other PR: If you like these changes let me know and I can cleanup. For example the directory in which the garth session is stored should be defined at a central place.

Yes! I very appreciate every PR making this simple and maintanable. As @stynoo already said, a file should be where the other files are. Currently /root

jrast commented 1 year ago

With https://github.com/matin/garth/pull/9 merged in garth, the garmin upload can be further simplified. Currently the implementation is more of a proof-of-concept, there are some points I personally would change:

jrast commented 1 year ago

Imho, it looks like this is the way to go. Garth does all the heavy lifting and should be able to handle MFA as well (not sure if extra code is needed to save the oauth1/2 tokens somewhere)

Storing the tokens reduces the network request considerably, making the upload process more robust as less things can fail.

The directory should ideally be the same place where .withings_user.json is stored. Iirc it's /root in the containers.

I will look into this.

matin commented 1 year ago

Garth now natively supports upload as of 0.4.32.

with open("12129115726_ACTIVITY.fit", "rb") as f:
    uploaded = garth.client.upload(f)

Let me know if anyone runs into issues, and I can fix them.

matin commented 1 year ago

@jrast if you decide to do this ...

Create a custom garth client instead of using the global one (might be overkill)

take a look at how garminconnect creates a custom Garth Client vs using the global one.