0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

自作のFitbit WEB API Wrapperで色々情報を取得してみる

Last updated at Posted at 2025-01-11

ライブラリの情報

使用しているライブラリに関しては、私の以前の記事を見てもらえると良いかもです。

本編

では本編です。なるべくスマホアプリと近い情報が欲しいなと思いながら作っています。

初期化部分

test.py
import json
import fitbitApp
import os
import datetime

curdir = os.path.dirname(__file__) + "/"

loginRes = "expired_token"
i = 1
while(loginRes == "expired_token"):
    config_json = open("{}Config.json".format(curdir), "r")
    token_json = open("{}Token.json".format(curdir), "r")
    CONFIG = json.load(config_json)
    TOKEN = json.load(token_json)
    
    access_token = TOKEN["access_token"]
    refresh_token = TOKEN["refresh_token"]
    client_id = CONFIG["CLIENT_ID"]

    app = fitbitApp.app(access_token, refresh_token, client_id, curdir)

    _ = app.Profile()
    try:
        loginRes = _.get("errors")[0].get("errorType")
        print("login err {}".format(i))
        i += 1
    except:
        loginRes = "login ok"
        print(loginRes)

睡眠データ関連

睡眠関連のデータについては、各睡眠モードのパーセンテージが欲しかったのでパーセントを加えました。関連記事を調査してみましたがレム睡眠の割合が結構重要みたいですね。

test.py
# 睡眠データ
slog = app.SleepLogByDate(date="today")
## ベッド入り時間(h)
timeInBed = slog.get("summary").get("totalTimeInBed") / 60
## 睡眠時間(h)
timeAsSleep = slog.get("summary").get("totalMinutesAsleep") / 60
## 入眠までの時間(h)
timeInSleep = timeInBed - timeAsSleep
## 深い睡眠時間(h)
timeDeepSleep = slog.get("summary").get("stages").get("deep") / 60
## 浅い睡眠時間(h)
timeLightSleep = slog.get("summary").get("stages").get("light") / 60
## REM睡眠時間(h)
timeRemSleep = slog.get("summary").get("stages").get("rem") / 60
## 覚醒時間(h)
timeAwakeSleep = slog.get("summary").get("stages").get("wake") / 60
## 睡眠時間(h)
timeTotalSleep = timeDeepSleep + timeLightSleep + timeRemSleep + timeAwakeSleep
## 深い睡眠時間(per)
timeDeepSleepPer = timeDeepSleep / timeTotalSleep * 100
## 浅い睡眠時間(per)
timeLightSleepPer = timeLightSleep / timeTotalSleep * 100
## REM睡眠時間(per)
timeRemSleepPer = timeRemSleep / timeTotalSleep * 100
## 覚醒時間(per)
timeAwakeSleepPer = timeAwakeSleep / timeTotalSleep * 100

print(timeTotalSleep, timeInSleep, timeDeepSleepPer, timeLightSleepPer, timeRemSleepPer, timeAwakeSleepPer)

心拍指標関連

お馴染み心拍系の指標の御三家、HRV(Heart Rate Variabilit)、BR(Breathing Rate)、RHR(Resting Heart Rate)です。この3つは体調を知る上で欠かせないですよね。

test.py
# 心拍指標
## HRV
i = 0
hrvData = {"hrv":[]}
while(hrvData.get("hrv") == []):
    tmpDay = (datetime.datetime.now()) - datetime.timedelta(days=i)
    tmpDay = tmpDay.strftime("%Y-%m-%d")
    hrvData = app.HRVSummaryByDate(date="{}".format(tmpDay))
hrv = hrvData.get("hrv")[0].get("value").get("dailyRmssd")
## BR
i = 0
brData = {"br":[]}
while(brData.get("br") == []):
    tmpDay = (datetime.datetime.now()) - datetime.timedelta(days=i)
    tmpDay = tmpDay.strftime("%Y-%m-%d")
    brData = app.BreathingRateSummaryByDate(date="{}".format(tmpDay))
br = brData.get("br")[0].get("value").get("breathingRate")
## RHR
rhrData = app.HeartRateTimeSeriesByDate()
rhr = rhrData.get("activities-heart")[0].get("value").get("restingHeartRate")

print(hrv, br, rhr)

ゾーン関連

test.py
# エクササイズ
## ゾーン時間とゾーンカロリー
tmpDay = datetime.datetime.now().strftime("%Y-%m-%d")
zoneTimeData = app.AZMTimeSeriesByDate(date=tmpDay).get("activities-active-zone-minutes")
if zoneTimeData == []:
    zoneTime = 0
    zoneCalorie = 0
else:
    zoneTime = zoneTimeData[0].get("value").get("activeZoneMinutes")
    activityData = app.DailyActivitySummary(date=tmpDay)
    zoneCalorie = activityData.get("summary").get("heartRateZones")[1].get("caloriesOut") + activityData.get("summary").get("heartRateZones")[2].get("caloriesOut") + activityData.get("summary").get("heartRateZones")[3].get("caloriesOut")
print(zoneTime, zoneCalorie)

Body系データ

test.py
# Body系データ
## 体重
weightData = app.BodyTimeSeriesByDate(resource="weight")
weight = float(weightData.get("body-weight")[0].get("value"))
## 体脂肪率
fatData = app.BodyTimeSeriesByDate(resource="fat")
fat = float(fatData.get("body-fat")[0].get("value"))
## BMI
bmiData = app.BodyTimeSeriesByDate(resource="bmi")
bmi = float(bmiData.get("body-bmi")[0].get("value"))

print(weight, fat, bmi)
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?