Skip to content

Commit

Permalink
major rewrite and update for Python 3
Browse files Browse the repository at this point in the history
  • Loading branch information
stv0g committed Aug 18, 2020
1 parent c8f32ca commit 2cb7f82
Show file tree
Hide file tree
Showing 25 changed files with 840 additions and 1,103 deletions.
6 changes: 1 addition & 5 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,2 @@
runme.sh
*.pyc
runme2.sh
weight.fit
.DS_Store
config/withings_user.json
*.egg-info/
29 changes: 0 additions & 29 deletions Docker/Dockerfile

This file was deleted.

7 changes: 0 additions & 7 deletions Docker/build.sh

This file was deleted.

14 changes: 14 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
FROM python:3.8-alpine

RUN apk add --update --no-cache libxml2-dev libxslt-dev gcc musl-dev

# Profit from Docker build cache buy building python lxml here..
RUN pip3 install lxml requests

RUN mkdir -p /src
COPY . /src

RUN cd /src && \
python3 ./setup.py install

ENTRYPOINT ["withings-sync"]
1 change: 1 addition & 0 deletions MANIFEST.in
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
include withings_sync/config/*.json
112 changes: 34 additions & 78 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,76 +1,59 @@
# withings-garmin-v2 (and TrainerRoad)
# withings-sync

A tool for synchronisation of Withings (ex. Nokia Health Body) to Garmin Connect and Trainer Road.
A tool for synchronisation of Withings (ex. Nokia Health Body) to:

- Garmin Connect
- Trainer Road

**NOTE: For Docker usage hits see at end of this document** https://hub.docker.com/r/jaroslawhartman/withings-garmin
**NOTE: For Docker usage hits see at end of this document:** https://hub.docker.com/r/stv0g/withings-sync

**NOTE: Included support for Withings OAuth2! See 'Obtaining Withings authorization'**

## References

* Based on withings-garmin by Masayuki Hamasaki, improved to support SSO authorization in Garmin Connect 2.
* Based on withings-garmin-v2 by Jarek Hartman, improved Python 3 compatability and code-style
* SSO authorization derived from https://github.com/cpfair/tapiriik
* TrainerRoad API from https://github.com/stuwilkins/python-trainerroad

## Pre-requisites

* Python 3
* 'Requests: HTTP for Humans' (http://docs.python-requests.org/en/latest/)
* lxml

Using pip:

```
yum install libxml2-dev libxslt-dev gcc
pip install --upgrade pip && pip install requests && pip install lxml
```

```
$ sudo easy_install requests
```

* simplejson
## Installation

```
$ sudo easy_install simplejson
```bash
$ pip install withings-sync
```

## Usage

```
Usage: sync.py [options]
usage: withings-sync [-h] [--garmin-username GARMIN_USERNAME] [--garmin-password GARMIN_PASSWORD] [--trainerroad-username TRAINERROAD_USERNAME] [--trainerroad-password TRAINERROAD_PASSWORD]
[--fromdate DATE] [--todate DATE] [--no-upload] [--verbose]
A tool for synchronisation of Withings (ex. Nokia Health Body) to Garmin Connect and Trainer Road.
Options:
optional arguments:
-h, --help show this help message and exit
--garmin-username=<user>, --gu=<user>
--garmin-username GARMIN_USERNAME, --gu GARMIN_USERNAME
username to login Garmin Connect.
--garmin-password=<pass>, --gp=<pass>
--garmin-password GARMIN_PASSWORD, --gp GARMIN_PASSWORD
password to login Garmin Connect.
--trainerroad-username=<user>, --tu=<user>
--trainerroad-username TRAINERROAD_USERNAME, --tu TRAINERROAD_USERNAME
username to login TrainerRoad.
--trainerroad-password=<user>, --tp=<user>
--trainerroad-password TRAINERROAD_PASSWORD, --tp TRAINERROAD_PASSWORD
username to login TrainerRoad.
-f <date>, --fromdate=<date>
-t <date>, --todate=<date>
--no-upload Won't upload to Garmin Connect and output binary-
strings to stdout.
-v, --verbose Run verbosely
--fromdate DATE, -f DATE
--todate DATE, -t DATE
--no-upload Won't upload to Garmin Connect and output binary-strings to stdout.
--verbose, -v Run verbosely
```

### Obtaining Withings Authorization Code


When running for a very first time, you need to obtain Withings authorization:

```
$ ./sync.py -f 2019-01-25 -v
```bash
$ withings-sync -f 2019-01-25 -v
Can't read config file config/withings_user.json
***************************************
* W A R N I N G *
***************************************
User interaction needed to get Authentification Code from Withings!
Open the following URL in your web browser and copy back the token. You will have *30 seconds* before the token expires. HURRY UP!
Expand All @@ -91,42 +74,18 @@ This is one-time activity and it will not be needed to repeat.
### Docker
```
$ docker pull jaroslawhartman/withings-garmin
$ docker pull stv0g/withings-sync
```
First start to ensure the script can start successfully:
```
jhartman@docker:~/withings-garmin-v2/Docker$ docker run -it --rm --name withings jaroslawhartman/withings-garmin
Usage: sync.py [options]
Options:
-h, --help show this help message and exit
--garmin-username=<user>, --gu=<user>
username to login Garmin Connect.
--garmin-password=<pass>, --gp=<pass>
password to login Garmin Connect.
--trainerroad-username=<user>, --tu=<user>
username to login TrainerRoad.
--trainerroad-password=<user>, --tp=<user>
username to login TrainerRoad.
-f <date>, --fromdate=<date>
-t <date>, --todate=<date>
--no-upload Won't upload to Garmin Connect and output binary-
strings to stdout.
-v, --verbose Run verbosely
```

Obtaining Withings authoorisation:
Obtaining Withings authorisation:
```
$ docker run -it --name withings jaroslawhartman/withings-garmin --garmin-username=<username> --garmin-password=<password>
$ docker run -v $HOME:/root --interactive --tty --name withings jaroslawhartman/withings-garmin --garmin-username=<username> --garmin-password=<password>
Can't read config file config/withings_user.json
***************************************
* W A R N I N G *
***************************************
User interaction needed to get Authentification Code from Withings!

Open the following URL in your web browser and copy back the token. You will have *30 seconds* before the token expires. HURRY UP!
Expand Down Expand Up @@ -156,17 +115,12 @@ Garmin Connect User Name: JaHa.WAW.PL
Fit file uploaded to Garmin Connect
```

### Run a periodic Kubernetes job

### You can hardcode your usernames and passwords in the script (`sync.py`):

...but why would you? Better to provide them as commandline parameters.
Edit the credentials in `contrib/k8s-job.yaml` and run:

```
GARMIN_USERNAME = ''
GARMIN_PASSWORD = ''
TRAINERROAD_USERNAME = ''
TRAINERROAD_PASSWORD = ''
```bash
$ kubectl apply -f contrib/k8s-job.yaml
```

### For advanced users - registering own Withings application
Expand Down Expand Up @@ -194,3 +148,5 @@ Configure them in `config/withings_app.json`, for example:
"consumer_secret": "a75d65******1df1514c16719ef7bd69fa7*****2e2b0ed48f1765"
}
```

For the callback URL you will need to setup a webserver hosting `contrib/withings.html`.
70 changes: 0 additions & 70 deletions README.origin

This file was deleted.

File renamed without changes
72 changes: 72 additions & 0 deletions contrib/k8s-job.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
---
apiVersion: "v1
kind: Namespace
metadata:
name: withings
---
# Secret for Garmin and TrainerRoad credentials
# PLEASE CHANGE!
apiVersion: v1
kind: Secret
metadata:
name: credentials
namespace: withings
type: Opaque
data:
# Base64 encoded!!!
GARMIN_PASSWORD: ""
GARMIN_USERNAME: ""
TRAINERROAD_PASSWORD: ""
TRAINERROAD_USERNAME: ""
---
# PVC for storing Withings OAuth tokens
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
finalizers:
- kubernetes.io/pvc-protection
name: withings-oauth-cache
namespace: withings
spec:
accessModes:
- ReadWriteOnce
resources:
requests:
storage: 1Mi
volumeMode: Filesystem
---
apiVersion: batch/v1beta1
kind: CronJob
metadata:
name: withings-garmin-sync
namespace: withings
spec:
concurrencyPolicy: Allow
failedJobsHistoryLimit: 1
jobTemplate:
spec:
template:
spec:
containers:
- args:
- -v
env:
- name: HOME
value: /root
envFrom:
- secretRef:
name: credentials
image: stv0g/withings-sync
imagePullPolicy: Always
name: withings-garmin-sync
volumeMounts:
- mountPath: /root/
name: oauth-cache
restartPolicy: Never
volumes:
- name: oauth-cache
persistentVolumeClaim:
claimName: withings-oauth-cache
schedule: '*/15 * * * *'
successfulJobsHistoryLimit: 3
Loading

0 comments on commit 2cb7f82

Please sign in to comment.