Skip to content
Merged
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
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -44,4 +44,4 @@ jobs:
env:
# Path the compat-tagged test reads from os.Getenv.
WITHINGS_EXPORT_BIN: /tmp/withings-export
run: go test -tags=compat -run TestContractFormats ./...
run: go test -tags=compat -run 'TestContractFormats|TestResolvedWindow' ./...
3 changes: 3 additions & 0 deletions cmd/activity.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,9 @@ var activityCmd = &cobra.Command{
if err != nil {
return err
}
if printed, err := printResolvedWindow(cmd, since, until.AddDate(0, 0, 1)); printed || err != nil {
return err
}

dataFields := "steps,distance,elevation,soft,moderate,intense,active," +
"calories,totalcalories,hr_average,hr_min,hr_max,hr_zone_0,hr_zone_1,hr_zone_2,hr_zone_3"
Expand Down
3 changes: 3 additions & 0 deletions cmd/intraday.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,9 @@ Default window is the last 24h — intraday is dense; wider ranges are slow.`,
if err != nil {
return err
}
if printed, err := printResolvedWindow(cmd, since, until); printed || err != nil {
return err
}

dataFields := "steps,elevation,calories,distance,duration,heart_rate," +
"hrv_quality,rmssd,sdnn1,spo2_auto"
Expand Down
11 changes: 7 additions & 4 deletions cmd/measurements.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,10 +54,10 @@ type measure struct {
}

type getmeasResponse struct {
Timezone string `json:"timezone"`
Measuregrps []measureGroup `json:"measuregrps"`
More int `json:"more"`
Offset int `json:"offset"`
Timezone string `json:"timezone"`
Measuregrps []measureGroup `json:"measuregrps"`
More int `json:"more"`
Offset int `json:"offset"`
}

type measurementRow struct {
Expand Down Expand Up @@ -94,6 +94,9 @@ var measurementsCmd = &cobra.Command{
if err != nil {
return err
}
if printed, err := printResolvedWindow(cmd, since, until); printed || err != nil {
return err
}

params := url.Values{}
params.Set("action", "getmeas")
Expand Down
22 changes: 22 additions & 0 deletions cmd/resolved_window.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package cmd

import (
"fmt"
"github.com/spf13/cobra"
"time"
)

func init() {
for _, cmd := range []*cobra.Command{measurementsCmd, intradayCmd, sleepCmd, activityCmd, workoutsCmd} {
cmd.Flags().Bool("print-resolved-window", false, "Print resolved dates without contacting Withings")
}
}

func printResolvedWindow(cmd *cobra.Command, since, until time.Time) (bool, error) {
enabled, err := cmd.Flags().GetBool("print-resolved-window")
if err != nil || !enabled {
return false, err
}
_, err = fmt.Fprintf(cmd.OutOrStdout(), "since=%s until_exclusive=%s\n", since.Format(time.RFC3339), until.Format(time.RFC3339))
return true, err
}
5 changes: 4 additions & 1 deletion cmd/sleep.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,9 @@ var sleepCmd = &cobra.Command{
if err != nil {
return err
}
if printed, err := printResolvedWindow(cmd, since, until.AddDate(0, 0, 1)); printed || err != nil {
return err
}

// All sleep summary data fields we care about. Must be explicitly requested.
dataFields := "wakeupduration,lightsleepduration,deepsleepduration,remsleepduration," +
Expand Down Expand Up @@ -244,7 +247,7 @@ func deriveSleep(c *client.Client, date time.Time) (*sleepSeries, error) {
// heart rate, or a meaningful burst of steps. Small step samples (bathroom
// trips, rolling over) do not break the run — they're just HealthKit's way
// of reporting brief motion without an accompanying HR reading.
const hrBreak = 90 // bpm above this = awake/active
const hrBreak = 90 // bpm above this = awake/active
const stepsBreak = 30

const maxGap int64 = 90 * 60 // 90 min — tolerate watch-off-for-charging
Expand Down
75 changes: 39 additions & 36 deletions cmd/workouts.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,42 +16,42 @@ import (

// Withings workout category codes. Common ones only — unknowns are rendered as numbers.
var workoutCategoryNames = map[int]string{
1: "walk",
2: "run",
3: "hiking",
4: "skating",
5: "bmx",
6: "bicycling",
7: "swimming",
8: "surfing",
9: "kitesurfing",
10: "windsurfing",
11: "bodyboard",
12: "tennis",
13: "tabletennis",
14: "squash",
15: "badminton",
16: "lift_weights",
17: "calisthenics",
18: "elliptical",
19: "pilates",
20: "basketball",
21: "soccer",
22: "football",
23: "rugby",
24: "volleyball",
25: "waterpolo",
26: "horse_riding",
27: "golf",
28: "yoga",
29: "dancing",
30: "boxing",
31: "fencing",
32: "wrestling",
33: "martial_arts",
34: "skiing",
35: "snowboarding",
36: "other",
1: "walk",
2: "run",
3: "hiking",
4: "skating",
5: "bmx",
6: "bicycling",
7: "swimming",
8: "surfing",
9: "kitesurfing",
10: "windsurfing",
11: "bodyboard",
12: "tennis",
13: "tabletennis",
14: "squash",
15: "badminton",
16: "lift_weights",
17: "calisthenics",
18: "elliptical",
19: "pilates",
20: "basketball",
21: "soccer",
22: "football",
23: "rugby",
24: "volleyball",
25: "waterpolo",
26: "horse_riding",
27: "golf",
28: "yoga",
29: "dancing",
30: "boxing",
31: "fencing",
32: "wrestling",
33: "martial_arts",
34: "skiing",
35: "snowboarding",
36: "other",
128: "rowing",
187: "zumba",
188: "baseball",
Expand Down Expand Up @@ -109,6 +109,9 @@ var workoutsCmd = &cobra.Command{
if err != nil {
return err
}
if printed, err := printResolvedWindow(cmd, since, until.AddDate(0, 0, 1)); printed || err != nil {
return err
}

dataFields := "calories,effduration,intensity,manual_distance,manual_calories," +
"hr_average,hr_min,hr_max,hr_zone_0,hr_zone_1,hr_zone_2,hr_zone_3," +
Expand Down
10 changes: 10 additions & 0 deletions compat_contract_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"time"

"github.com/quantcli/common/compat"
"github.com/quantcli/common/compat/dates"
"github.com/quantcli/common/compat/formats"
)

Expand Down Expand Up @@ -112,3 +113,12 @@ func writeFakeAuthToken(t *testing.T) string {
}
return home
}

// TestResolvedWindow verifies local dates independently of upstream credentials.
func TestResolvedWindow(t *testing.T) {
bin := os.Getenv("WITHINGS_EXPORT_BIN")
if bin == "" {
t.Skip("WITHINGS_EXPORT_BIN not set")
}
dates.RunResolvedWindow(t, compat.Runner{Binary: bin, Subcommands: []string{"activity", "intraday", "measurements", "sleep", "workouts"}})
}
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ module github.com/quantcli/withings-export-cli
go 1.26.10

require (
github.com/quantcli/common/compat v0.0.0-20260511001927-62c4c6634ff5
github.com/quantcli/common/compat v0.0.0-20261110001356-0b66a42dfa85
github.com/spf13/cobra v1.10.2
)

Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
github.com/cpuguy83/go-md2man/v2 v2.0.6/go.mod h1:oOW0eioCTA6cOiMLiUPZOpcVxMig6NIQQ7OS05n1F4g=
github.com/inconshreveable/mousetrap v1.1.0 h1:wN+x4NVGpMsO7ErUn/mUI3vEoE6Jt13X2s0bqwp9tc8=
github.com/inconshreveable/mousetrap v1.1.0/go.mod h1:vpF70FUmC8bwa3OWnCshd2FqLfsEA9PFc4w1p2J65bw=
github.com/quantcli/common/compat v0.0.0-20260511001927-62c4c6634ff5 h1:NoWh40ITAC6W7YyIp2eM6RyRKLHzXcs8TXUjwoYrlUY=
github.com/quantcli/common/compat v0.0.0-20260511001927-62c4c6634ff5/go.mod h1:VBC/zEphSZgCZS1rhWsR3A8EWYSbTkP/MwqWHL7266s=
github.com/quantcli/common/compat v0.0.0-20261110001356-0b66a42dfa85 h1:NxUSVjE+3clP9Ss6uvlwZ0SToE7kmpkioRJ5r8ZUmK4=
github.com/quantcli/common/compat v0.0.0-20261110001356-0b66a42dfa85/go.mod h1:VBC/zEphSZgCZS1rhWsR3A8EWYSbTkP/MwqWHL7266s=
github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
github.com/spf13/cobra v1.10.2 h1:DMTTonx5m65Ic0GOoRY2c16WCbHxOOw6xxezuLaBpcU=
github.com/spf13/cobra v1.10.2/go.mod h1:7C1pvHqHw5A4vrJfjNwvOdzYu0Gml16OCs2GRiTUUS4=
Expand Down
36 changes: 36 additions & 0 deletions resolved_window_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package main_test

import (
"os"
"os/exec"
"path/filepath"
"strings"
"testing"
)

func TestResolvedWindowOffline(t *testing.T) {
bin := filepath.Join(t.TempDir(), "export-cli")
build := exec.Command("go", "build", "-o", bin, ".")
if out, err := build.CombinedOutput(); err != nil {
t.Fatalf("build: %v: %s", err, out)
}
cases := []struct{ zone, day, want string }{
{"Pacific/Honolulu", "2026-04-15", "since=2026-04-15T00:00:00-10:00 until_exclusive=2026-04-16T00:00:00-10:00\n"},
{"Pacific/Kiritimati", "2026-04-15", "since=2026-04-15T00:00:00+14:00 until_exclusive=2026-04-16T00:00:00+14:00\n"},
{"America/New_York", "2026-03-08", "since=2026-03-08T00:00:00-05:00 until_exclusive=2026-03-09T00:00:00-04:00\n"},
{"America/New_York", "2026-11-01", "since=2026-11-01T00:00:00-04:00 until_exclusive=2026-11-02T00:00:00-05:00\n"},
}
for _, sub := range []string{"activity", "intraday", "measurements", "sleep", "workouts"} {
for _, tc := range cases {
t.Run(sub+"/"+tc.zone+"/"+tc.day, func(t *testing.T) {
args := append(strings.Fields(sub), "--since", tc.day, "--until", tc.day, "--print-resolved-window")
cmd := exec.Command(bin, args...)
cmd.Env = []string{"TZ=" + tc.zone, "HOME=" + t.TempDir(), "HTTPS_PROXY=http://127.0.0.1:1", "PATH=" + os.Getenv("PATH")}
out, err := cmd.CombinedOutput()
if err != nil || string(out) != tc.want {
t.Fatalf("output=%q error=%v, want %q", out, err, tc.want)
}
})
}
}
}
Loading