Vous devez utiliser OAuth2 pour obtenir les données avec l'API Google Fit. Il est pratique d'utiliser google-api-python-client lors de la récupération de données à l'aide d'OAuth2. La méthode pour obtenir les données à l'aide de google-api-python-client est décrite ci-dessous.
pip3 install google-api-python-client
getfit.py
import os
import json
import httplib2
import requests
import time
from datetime import datetime, timedelta
from apiclient.discovery import build
from oauth2client.client import OAuth2WebServerFlow, flow_from_clientsecrets
from oauth2client.file import Storage
OAUTH_SCOPE = 'https://www.googleapis.com/auth/fitness.activity.read'
DATA_SOURCE = "derived:com.google.step_count.delta:com.google.android.gms:estimated_steps"
REDIRECT_URI = 'urn:ietf:wg:oauth:2.0:oob'
CREDENTIALS_FILE = "./secret/credentials"
def auth_data():
credentials = ""
if os.path.exists(CREDENTIALS_FILE):
credentials = Storage(CREDENTIALS_FILE).get()
else:
#flow = OAuth2WebServerFlow(CLIENT_ID, CLIENT_SECRET, OAUTH_SCOPE, REDIRECT_URI)
flow = flow_from_clientsecrets(
#Spécifiez le fichier JSON pour OAuth acquis lorsque l'API est activée
'./secret/oauth2.json',
#Spécifiez la portée
scope=OAUTH_SCOPE,
#Spécifiez la méthode de réception du jeton après l'authentification de l'utilisateur (décrite plus loin)
redirect_uri=REDIRECT_URI)
authorize_url = flow.step1_get_authorize_url()
print('Veuillez démarrer l'URL suivante dans votre navigateur.')
print(authorize_url)
code = input('Veuillez saisir le code: ').strip()
credentials = flow.step2_exchange(code)
if not os.path.exists(CREDENTIALS_FILE):
Storage(CREDENTIALS_FILE).put(credentials)
# Create an httplib2.Http object and authorize it with our credentials
http = httplib2.Http()
http = credentials.authorize(http)
fitness_service = build('fitness', 'v1', http=http)
return fitness_service
def retrieve_data(fitness_service, dataset):
return fitness_service.users().dataSources(). \
datasets(). \
get(userId='me', dataSourceId=DATA_SOURCE, datasetId=dataset). \
execute()
def nanoseconds(nanotime):
"""
Convertir en nanosecondes
"""
dt = datetime.fromtimestamp(nanotime // 1000000000)
return dt.strftime('%Y-%m-%d %H:%M:%S')
def logwrite(date, step):
with open('./data/step.log', 'a') as outfile:
outfile.write(str(date) + "," + str(step) + "\n")
if __name__ == "__main__":
authdata = auth_data()
#Récupérez les données de la veille
TODAY = datetime.today() - timedelta(days=1)
STARTDAY = datetime(TODAY.year, TODAY.month, TODAY.day, 0, 0, 0)
NEXTDAY = datetime(TODAY.year, TODAY.month, TODAY.day, 23, 59, 59)
NOW = datetime.today()
START = int(time.mktime(STARTDAY.timetuple())*1000000000)
NEXT = int(time.mktime(NEXTDAY.timetuple())*1000000000)
END = int(time.mktime(NOW.timetuple())*1000000000)
data_set = "%s-%s" % (START, NEXT)
while True:
if END < NEXT:
break
dataset = retrieve_data(authdata, data_set)
starts = []
ends = []
values = []
for point in dataset["point"]:
if int(point["startTimeNanos"]) > START:
starts.append(int(point["startTimeNanos"]))
ends.append(int(point["endTimeNanos"]))
values.append(point['value'][0]['intVal'])
print("From: {}".format(nanoseconds(min(starts))))
print("To: {}".format(nanoseconds(max(ends))))
print("Steps:{}".format(sum(values)))
step = sum(values)
startdate = STARTDAY.date()
logwrite(startdate, step)
STARTDAY = STARTDAY + timedelta(days=1)
NEXTDAY = NEXTDAY + timedelta(days=1)
START = int(time.mktime(STARTDAY.timetuple())*1000000000)
NEXT = int(time.mktime(NEXTDAY.timetuple())*1000000000)
data_set = "%s-%s" % (START, NEXT)
time.sleep(5)
Recommended Posts