-
-
Notifications
You must be signed in to change notification settings - Fork 32
/
Copy pathsync.go
138 lines (109 loc) · 2.59 KB
/
sync.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
package main
import (
"encoding/json"
"errors"
"fmt"
"log"
"strings"
"time"
"github.com/anyappinc/fitbit"
"github.com/jovandeginste/workout-tracker/v2/pkg/app"
"resty.dev/v3"
)
func (fs *fitbitSync) initRESTClient() {
client := resty.New()
client.SetBaseURL(fs.WorkoutConfig.URL + "/api/v1/")
client.SetAuthToken(fs.WorkoutConfig.APIKey)
fs.restClient = client
}
func (fs *fitbitSync) syncActivities(days int) {
fs.initRESTClient()
units := fs.fitbitClient.GetUnit()
end := time.Now()
endDate := end.Format("2006-01-02")
start := end.AddDate(0, 0, -days)
for d := start; !d.After(end); d = d.AddDate(0, 0, 1) {
date := d.Format("2006-01-02")
log.Printf("Syncing: %s\n", date)
act, err := fs.getDailyActivitySummary(d)
if err != nil {
log.Printf("could not get daily activity summary: %v", err)
continue
}
if act == nil {
continue
}
if fs.WorkoutConfig.syncActivities {
for _, a := range act.Activities {
if !a.HasStartTime {
continue
}
if err := fs.uploadActivity(a); err != nil {
log.Printf("could not sync activity TCX: %v", err)
continue
}
}
}
final := date == endDate
mw := fs.buildMeasurement(date, final, units, act)
if err := fs.postMeasurement(mw); err != nil {
log.Printf("could not post measurement: %v", err)
}
}
}
func (fs *fitbitSync) buildMeasurement(date string, final bool, units *fitbit.Unit, act *fitbit.DailyActivitySummary) *app.Measurement {
mw := &app.Measurement{
Date: date,
}
if fs.WorkoutConfig.syncSteps {
if act.Summary != nil {
mw.Steps = float64(act.Summary.Steps)
}
}
if !final {
return mw
}
if fs.WorkoutConfig.syncHeight {
mw.Height = fs.profile.Height
mw.HeightUnit = units.Height
}
if fs.WorkoutConfig.syncWeight {
mw.Weight = fs.profile.Weight
mw.WeightUnit = units.Weight
}
return mw
}
func (fs *fitbitSync) postMeasurement(m *app.Measurement) error {
res, err := fs.restClient.R().
SetBody(m).
Post("/daily")
if err != nil {
return err
}
if !res.IsSuccess() {
return errors.New(res.Status())
}
return nil
}
func (fs *fitbitSync) uploadActivity(a fitbit.Activity) error {
tcx, err := fs.getActivityTCX(a)
if err != nil {
return err
}
name := fmt.Sprintf("%d.tcx", a.LogID)
res, err := fs.restClient.R().
SetBody(tcx).
SetQueryParam("name", name).
Post("/import/generic")
if err != nil {
return err
}
var response app.APIResponse
if err := json.Unmarshal(res.Bytes(), &response); err != nil {
return err
}
if !res.IsSuccess() {
return errors.New(res.Status() + ": " + strings.Join(response.Errors, ","))
}
return nil
}