1
// Copyright 2014 The Go Authors. All rights reserved.
2
// Use of this source code is governed by a BSD-style
3
// license that can be found in the LICENSE file.
15
fitness "google.golang.org/api/fitness/v1"
19
layout = "Jan 2, 2006 at 3:04pm" // for time.Format
25
fitness.FitnessActivityReadScope,
26
fitness.FitnessActivityWriteScope,
27
fitness.FitnessBodyReadScope,
28
fitness.FitnessBodyWriteScope,
29
fitness.FitnessLocationReadScope,
30
fitness.FitnessLocationWriteScope,
32
registerDemo("fitness", strings.Join(scopes, " "), fitnessMain)
35
// millisToTime converts Unix millis to time.Time.
36
func millisToTime(t int64) time.Time {
37
return time.Unix(0, t*nanosPerMilli)
40
// fitnessMain is an example that demonstrates calling the Fitness API.
43
// go build -o go-api-demo *.go
44
// go-api-demo -clientid="my-clientid" -secret="my-secret" fitness
45
func fitnessMain(client *http.Client, argv []string) {
47
fmt.Fprintln(os.Stderr, "Usage: fitness")
51
svc, err := fitness.New(client)
53
log.Fatalf("Unable to create Fitness service: %v", err)
56
us, err := svc.Users.Sessions.List("me").Do()
58
log.Fatalf("Unable to retrieve user's sessions: %v", err)
60
if len(us.Session) == 0 {
61
log.Fatal("You have no user sessions to explore.")
64
var minTime, maxTime time.Time
65
for _, s := range us.Session {
66
start := millisToTime(s.StartTimeMillis)
67
end := millisToTime(s.EndTimeMillis)
68
if minTime.IsZero() || start.Before(minTime) {
71
if maxTime.IsZero() || end.After(maxTime) {
74
log.Printf("Session %q, %v - %v, activity type=%v", s.Name, start.Format(layout), end.Format(layout), s.ActivityType)
77
ds, err := svc.Users.DataSources.List("me").Do()
79
log.Fatalf("Unable to retrieve user's data sources: %v", err)
81
if len(ds.DataSource) == 0 {
82
log.Fatal("You have no data sources to explore.")
84
for _, d := range ds.DataSource {
86
if d.DataType != nil && len(d.DataType.Field) > 0 {
87
f := d.DataType.Field[0]
89
log.Printf("Data source %q, name %q is of type %q", d.DataStreamName, f.Name, format)
91
log.Printf("Data source %q is of type %q", d.DataStreamName, d.Type)
93
setID := fmt.Sprintf("%v-%v", minTime.UnixNano(), maxTime.UnixNano())
94
data, err := svc.Users.DataSources.Datasets.Get("me", d.DataStreamId, setID).Do()
96
log.Fatalf("Unable to retrieve user's data source stream %v, %v: %v", d.DataStreamId, setID, err)
98
for _, p := range data.Point {
99
for _, v := range p.Value {
100
t := millisToTime(p.ModifiedTimeMillis).Format(layout)
101
if format == "integer" {
102
log.Printf("data at %v = %v", t, v.IntVal)
104
log.Printf("data at %v = %v", t, v.FpVal)