コード例 #1
0
ファイル: dashboard_service.go プロジェクト: nlefler/jawbone
func makeCoffeesTodayHandler(userToken auth.UserToken) func(http.ResponseWriter, *http.Request) {
	return func(resp http.ResponseWriter, req *http.Request) {
		mealsRequest := endpoint.JawboneRequest{
			"@me",
			endpoint.JawboneMealsEndpoint,
			url.Values{
				"date": {time.Now().Format("20060102")},
			},
			"",
		}

		meals := endpoint.JawboneResponseMeals{}
		err := mealsRequest.MakeRequest(userToken, &meals, false)
		if err != nil {
			http.Error(resp, err.Error(), 500)
		}
		if meals.Data.Size == 0 {
			http.Error(resp, err.Error(), 500)
		}

		numCoffees := 0
		for _, meal := range meals.Data.Items {
			if strings.Contains(meal.Title, "Coffee") {
				numCoffees += meal.Details.NumDrinks
			}
		}

		marshaled, err := json.Marshal(numCoffees)
		if err != nil {
			http.Error(resp, err.Error(), 500)
		}

		fmt.Fprintf(resp, "%s", string(marshaled))
	}
}
コード例 #2
0
ファイル: dashboard_service.go プロジェクト: nlefler/jawbone
func makeMovesDurationsHandler(userToken auth.UserToken) func(http.ResponseWriter, *http.Request) {
	return func(resp http.ResponseWriter, req *http.Request) {
		movesRequest := endpoint.JawboneRequest{
			"@me",
			endpoint.JawboneMovesEndpoint,
			url.Values{},
			"",
		}

		moves := endpoint.JawboneResponseMoves{}
		err := movesRequest.MakeRequest(userToken, &moves, false)
		if err != nil {
			http.Error(resp, err.Error(), 500)
		}
		if moves.Data.Size == 0 {
			http.Error(resp, err.Error(), 500)
		}

		move := moves.Data.Items[0]
		moveData := map[string]float32{
			"distance_km": move.Details.DistanceKm,
			"steps":       float32(move.Details.Steps),
			"calories":    move.Details.Calories,
		}

		marshaled, err := json.Marshal(moveData)
		if err != nil {
			http.Error(resp, err.Error(), 500)
		}

		fmt.Fprintf(resp, "%s", string(marshaled))
	}
}
コード例 #3
0
ファイル: dashboard_service.go プロジェクト: nlefler/jawbone
func makeWeekBikeDurationsHandler(userToken auth.UserToken) func(http.ResponseWriter, *http.Request) {
	var countMiles = func(miles float32, workouts endpoint.JawboneResponseWorkouts) float32 {
		sortedItems := workouts.Data.Items
		sort.Sort(workoutByDateDesc(sortedItems))
		for _, workout := range sortedItems {
			if workout.SubType != 14 {
				continue
			}

			miles += workout.Details.DistanceKm
		}

		return miles
	}

	return func(resp http.ResponseWriter, req *http.Request) {
		startTime := time.Now()
		dayDiff := time.Sunday - startTime.Weekday()
		startTime = startTime.AddDate(0, 0, int(dayDiff))
		endTime := time.Now()
		workoutsRequest := endpoint.JawboneRequest{
			"@me",
			endpoint.JawboneWorkoutsEndpoint,
			url.Values{
				"start_time": {fmt.Sprintf("%d", startTime.Unix())},
				"end_time":   {fmt.Sprintf("%d", endTime.Unix())},
			},
			"",
		}

		workouts := endpoint.JawboneResponseWorkouts{}
		err := workoutsRequest.MakeRequest(userToken, &workouts, false)
		if err != nil {
			http.Error(resp, err.Error(), 500)
			return
		}

		miles := countMiles(0.0, workouts)
		if err != nil {
			http.Error(resp, err.Error(), 500)
			return
		}

		for workouts.HasNextPage() {
			workouts = endpoint.JawboneResponseWorkouts{}
			err = workoutsRequest.MakeRequest(userToken, &workouts, true)
			if err != nil {
				http.Error(resp, err.Error(), 500)
				return
			}

			miles = countMiles(miles, workouts)
			if err != nil {
				http.Error(resp, err.Error(), 500)
				return
			}
		}
		fmt.Fprintf(resp, "%f", miles)
	}
}
コード例 #4
0
ファイル: playground.go プロジェクト: nlefler/jawbone
func main() {
	workoutsRequest := endpoint.JawboneRequest{
		"@me",
		endpoint.JawboneWorkoutsEndpoint,
		url.Values{
			"date": {"20140704"},
		},
	}

	userToken, err := auth.LoadUserToken("./user_tokens.json")
	if err != nil {
		log.Fatal(err)
	}

	workouts := endpoint.JawboneResponseWorkout{}
	err = workoutsRequest.MakeRequest(userToken, &workouts)
	if err != nil {
		log.Fatal(err)
	}
	log.Println(workouts)
}
コード例 #5
0
ファイル: dashboard_service.go プロジェクト: nlefler/jawbone
func makeSleepDurationsHandler(userToken auth.UserToken) func(http.ResponseWriter, *http.Request) {
	return func(resp http.ResponseWriter, req *http.Request) {
		now := time.Now()
		lastWeek := now.AddDate(0, 0, -7)

		sleepsRequest := endpoint.JawboneRequest{
			"@me",
			endpoint.JawboneSleepsEndpoint,
			url.Values{
				"start_time": {fmt.Sprintf("%d", lastWeek.Unix())},
				"end_time":   {fmt.Sprintf("%d", now.Unix())},
			},
			"",
		}

		sleeps := endpoint.JawboneResponseSleeps{}
		err := sleepsRequest.MakeRequest(userToken, &sleeps, false)
		if err != nil {
			http.Error(resp, err.Error(), 500)
		}
		durations := []map[string]int64{}
		for _, sleep := range sleeps.Data.Items {
			if sleep.SubType == 0 || sleep.SubType == 2 {
				durations = append(durations, map[string]int64{
					"date":     sleep.TimeCreated,
					"duration": sleep.Details.Duration,
					"light":    sleep.Details.Light,
					"sound":    sleep.Details.Sound,
				})
			}
		}
		marshaled, err := json.Marshal(durations)
		if err != nil {
			http.Error(resp, err.Error(), 500)
		}

		fmt.Fprintf(resp, "%s", string(marshaled))
	}
}
コード例 #6
0
ファイル: dashboard_service.go プロジェクト: nlefler/jawbone
func makeWorkoutStreakHandler(userToken auth.UserToken) func(http.ResponseWriter, *http.Request) {
	var countStreak = func(streak int, lastDate time.Time, workouts endpoint.JawboneResponseWorkouts) (int, error) {
		sortedItems := workouts.Data.Items
		sort.Sort(workoutByDateDesc(sortedItems))
		for _, workout := range sortedItems {
			workoutDate, err := time.Parse("20060102", fmt.Sprintf("%d", workout.Date))
			if err != nil {
				return 0, err
			}

			if workout.SubType != 4 {
				continue
			}

			dateDiff := workoutDate.Sub(lastDate)
			lastDate = workoutDate

			if streak == 0 && dateDiff == 0 {
				streak = 1
				continue
			}

			if dateDiff < -24*time.Hour {
				break
			}

			streak++
		}

		return streak, nil
	}

	return func(resp http.ResponseWriter, req *http.Request) {
		workoutsRequest := endpoint.JawboneRequest{
			"@me",
			endpoint.JawboneWorkoutsEndpoint,
			url.Values{},
			"",
		}

		workouts := endpoint.JawboneResponseWorkouts{}
		err := workoutsRequest.MakeRequest(userToken, &workouts, false)
		if err != nil {
			http.Error(resp, err.Error(), 500)
			return
		}

		lastDate := time.Now().Truncate(24 * time.Hour)
		streak, err := countStreak(0, lastDate, workouts)
		if err != nil {
			http.Error(resp, err.Error(), 500)
			return
		}

		for workouts.HasNextPage() {
			workouts = endpoint.JawboneResponseWorkouts{}
			err = workoutsRequest.MakeRequest(userToken, &workouts, true)
			if err != nil {
				http.Error(resp, err.Error(), 500)
				return
			}

			streak, err = countStreak(streak, lastDate, workouts)
			if err != nil {
				http.Error(resp, err.Error(), 500)
				return
			}
		}
		fmt.Fprintf(resp, "%d", streak)
	}
}