mirror of
https://github.com/sstent/go-garth.git
synced 2026-02-06 06:22:10 +00:00
porting - part2 wk3 done
This commit is contained in:
@@ -5,16 +5,21 @@ import (
|
||||
"flag"
|
||||
"fmt"
|
||||
"log"
|
||||
"os"
|
||||
"time"
|
||||
|
||||
"garmin-connect/garth/client"
|
||||
"garmin-connect/garth"
|
||||
"garmin-connect/garth/credentials"
|
||||
"garmin-connect/garth/types"
|
||||
)
|
||||
|
||||
func main() {
|
||||
// Parse command line flags
|
||||
outputTokens := flag.Bool("tokens", false, "Output OAuth tokens in JSON format")
|
||||
dataType := flag.String("data", "", "Data type to fetch (bodybattery, sleep, hrv, weight)")
|
||||
statsType := flag.String("stats", "", "Stats type to fetch (steps, stress, hydration, intensity, sleep, hrv)")
|
||||
dateStr := flag.String("date", "", "Date in YYYY-MM-DD format (default: yesterday)")
|
||||
days := flag.Int("days", 1, "Number of days to fetch")
|
||||
outputFile := flag.String("output", "", "Output file for JSON results")
|
||||
flag.Parse()
|
||||
|
||||
// Load credentials from .env file
|
||||
@@ -24,7 +29,7 @@ func main() {
|
||||
}
|
||||
|
||||
// Create client
|
||||
garminClient, err := client.NewClient(domain)
|
||||
garminClient, err := garth.NewClient(domain)
|
||||
if err != nil {
|
||||
log.Fatalf("Failed to create client: %v", err)
|
||||
}
|
||||
@@ -48,33 +53,136 @@ func main() {
|
||||
|
||||
// If tokens flag is set, output tokens and exit
|
||||
if *outputTokens {
|
||||
tokens := struct {
|
||||
OAuth1 *types.OAuth1Token `json:"oauth1"`
|
||||
OAuth2 *types.OAuth2Token `json:"oauth2"`
|
||||
}{
|
||||
OAuth1: garminClient.OAuth1Token,
|
||||
OAuth2: garminClient.OAuth2Token,
|
||||
}
|
||||
|
||||
jsonBytes, err := json.MarshalIndent(tokens, "", " ")
|
||||
if err != nil {
|
||||
log.Fatalf("Failed to marshal tokens: %v", err)
|
||||
}
|
||||
fmt.Println(string(jsonBytes))
|
||||
outputTokensJSON(garminClient)
|
||||
return
|
||||
}
|
||||
|
||||
// Test getting activities
|
||||
// Handle data requests
|
||||
if *dataType != "" {
|
||||
handleDataRequest(garminClient, *dataType, *dateStr, *days, *outputFile)
|
||||
return
|
||||
}
|
||||
|
||||
// Handle stats requests
|
||||
if *statsType != "" {
|
||||
handleStatsRequest(garminClient, *statsType, *dateStr, *days, *outputFile)
|
||||
return
|
||||
}
|
||||
|
||||
// Default: show recent activities
|
||||
activities, err := garminClient.GetActivities(5)
|
||||
if err != nil {
|
||||
log.Fatalf("Failed to get activities: %v", err)
|
||||
}
|
||||
|
||||
// Display activities
|
||||
displayActivities(activities)
|
||||
}
|
||||
|
||||
func displayActivities(activities []types.Activity) {
|
||||
func outputTokensJSON(c *garth.Client) {
|
||||
tokens := struct {
|
||||
OAuth1 *garth.OAuth1Token `json:"oauth1"`
|
||||
OAuth2 *garth.OAuth2Token `json:"oauth2"`
|
||||
}{
|
||||
OAuth1: c.OAuth1Token,
|
||||
OAuth2: c.OAuth2Token,
|
||||
}
|
||||
|
||||
jsonBytes, err := json.MarshalIndent(tokens, "", " ")
|
||||
if err != nil {
|
||||
log.Fatalf("Failed to marshal tokens: %v", err)
|
||||
}
|
||||
fmt.Println(string(jsonBytes))
|
||||
}
|
||||
|
||||
func handleDataRequest(c *garth.Client, dataType, dateStr string, days int, outputFile string) {
|
||||
endDate := time.Now().AddDate(0, 0, -1) // default to yesterday
|
||||
if dateStr != "" {
|
||||
parsedDate, err := time.Parse("2006-01-02", dateStr)
|
||||
if err != nil {
|
||||
log.Fatalf("Invalid date format: %v", err)
|
||||
}
|
||||
endDate = parsedDate
|
||||
}
|
||||
|
||||
var result interface{}
|
||||
var err error
|
||||
|
||||
switch dataType {
|
||||
case "bodybattery":
|
||||
bb := &garth.BodyBatteryData{}
|
||||
result, err = bb.Get(endDate, c)
|
||||
case "sleep":
|
||||
sleep := &garth.SleepData{}
|
||||
result, err = sleep.Get(endDate, c)
|
||||
case "hrv":
|
||||
hrv := &garth.HRVData{}
|
||||
result, err = hrv.Get(endDate, c)
|
||||
case "weight":
|
||||
weight := &garth.WeightData{}
|
||||
result, err = weight.Get(endDate, c)
|
||||
default:
|
||||
log.Fatalf("Unknown data type: %s", dataType)
|
||||
}
|
||||
|
||||
if err != nil {
|
||||
log.Fatalf("Failed to get %s data: %v", dataType, err)
|
||||
}
|
||||
|
||||
outputResult(result, outputFile)
|
||||
}
|
||||
|
||||
func handleStatsRequest(c *garth.Client, statsType, dateStr string, days int, outputFile string) {
|
||||
endDate := time.Now().AddDate(0, 0, -1) // default to yesterday
|
||||
if dateStr != "" {
|
||||
parsedDate, err := time.Parse("2006-01-02", dateStr)
|
||||
if err != nil {
|
||||
log.Fatalf("Invalid date format: %v", err)
|
||||
}
|
||||
endDate = parsedDate
|
||||
}
|
||||
|
||||
var stats garth.Stats
|
||||
switch statsType {
|
||||
case "steps":
|
||||
stats = garth.NewDailySteps()
|
||||
case "stress":
|
||||
stats = garth.NewDailyStress()
|
||||
case "hydration":
|
||||
stats = garth.NewDailyHydration()
|
||||
case "intensity":
|
||||
stats = garth.NewDailyIntensityMinutes()
|
||||
case "sleep":
|
||||
stats = garth.NewDailySleep()
|
||||
case "hrv":
|
||||
stats = garth.NewDailyHRV()
|
||||
default:
|
||||
log.Fatalf("Unknown stats type: %s", statsType)
|
||||
}
|
||||
|
||||
result, err := stats.List(endDate, days, c)
|
||||
if err != nil {
|
||||
log.Fatalf("Failed to get %s stats: %v", statsType, err)
|
||||
}
|
||||
|
||||
outputResult(result, outputFile)
|
||||
}
|
||||
|
||||
func outputResult(data interface{}, outputFile string) {
|
||||
jsonBytes, err := json.MarshalIndent(data, "", " ")
|
||||
if err != nil {
|
||||
log.Fatalf("Failed to marshal result: %v", err)
|
||||
}
|
||||
|
||||
if outputFile != "" {
|
||||
if err := os.WriteFile(outputFile, jsonBytes, 0644); err != nil {
|
||||
log.Fatalf("Failed to write output file: %v", err)
|
||||
}
|
||||
fmt.Printf("Results saved to %s\n", outputFile)
|
||||
} else {
|
||||
fmt.Println(string(jsonBytes))
|
||||
}
|
||||
}
|
||||
|
||||
func displayActivities(activities []garth.Activity) {
|
||||
fmt.Printf("\n=== Recent Activities ===\n")
|
||||
for i, activity := range activities {
|
||||
fmt.Printf("%d. %s\n", i+1, activity.ActivityName)
|
||||
|
||||
Reference in New Issue
Block a user