mirror of
https://github.com/sstent/go-garth.git
synced 2026-01-25 16:42:28 +00:00
93 lines
2.3 KiB
Go
93 lines
2.3 KiB
Go
package main
|
|
|
|
import (
|
|
"encoding/json"
|
|
"flag"
|
|
"fmt"
|
|
"log"
|
|
"time"
|
|
|
|
"garmin-connect/garth/client"
|
|
"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")
|
|
flag.Parse()
|
|
|
|
// Load credentials from .env file
|
|
email, password, domain, err := credentials.LoadEnvCredentials()
|
|
if err != nil {
|
|
log.Fatalf("Failed to load credentials: %v", err)
|
|
}
|
|
|
|
// Create client
|
|
garminClient, err := client.NewClient(domain)
|
|
if err != nil {
|
|
log.Fatalf("Failed to create client: %v", err)
|
|
}
|
|
|
|
// Try to load existing session first
|
|
sessionFile := "garmin_session.json"
|
|
if err := garminClient.LoadSession(sessionFile); err != nil {
|
|
fmt.Println("No existing session found, logging in with credentials from .env...")
|
|
|
|
if err := garminClient.Login(email, password); err != nil {
|
|
log.Fatalf("Login failed: %v", err)
|
|
}
|
|
|
|
// Save session for future use
|
|
if err := garminClient.SaveSession(sessionFile); err != nil {
|
|
fmt.Printf("Failed to save session: %v\n", err)
|
|
}
|
|
} else {
|
|
fmt.Println("Loaded existing session")
|
|
}
|
|
|
|
// 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))
|
|
return
|
|
}
|
|
|
|
// Test getting 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) {
|
|
fmt.Printf("\n=== Recent Activities ===\n")
|
|
for i, activity := range activities {
|
|
fmt.Printf("%d. %s\n", i+1, activity.ActivityName)
|
|
fmt.Printf(" Type: %s\n", activity.ActivityType.TypeKey)
|
|
fmt.Printf(" Date: %s\n", activity.StartTimeLocal)
|
|
if activity.Distance > 0 {
|
|
fmt.Printf(" Distance: %.2f km\n", activity.Distance/1000)
|
|
}
|
|
if activity.Duration > 0 {
|
|
duration := time.Duration(activity.Duration) * time.Second
|
|
fmt.Printf(" Duration: %v\n", duration.Round(time.Second))
|
|
}
|
|
fmt.Println()
|
|
}
|
|
}
|