0

语境:

有一个名为Fit as应用程序,它会将活动添加到您的 Google Fit 帐户中,但它看起来像是基于真实活动,例如模拟 GPS 位置或设备加速度计。

还有一个名为Step Me应用程序,它还会将活动添加到您的 Google fit 帐户,但它是即时的。该应用程序开发人员本身声称它可以添加从现在到过去的数据。(我认为这可能是基于 API 或框架来将数据添加到 Google fit)。

我在寻找什么:

一种像Step Me一样添加数据的方法,但使用 Python。

我已经知道可以做的事情:

使用代码作为示例,可以从 API 获取数据,如下所示:

#! /usr/bin/env python
#-*- coding: utf-8 -*-

import json
import httplib2
from datetime import datetime
from apiclient.discovery import build
from oauth2client.client import OAuth2WebServerFlow

CLIENT_ID = 'XXXXXXXXXXXXXXXXXX.apps.googleusercontent.com'
CLIENT_SECRET = 'XXXXXXXXXXXXXXXXXXXXXXX'
OAUTH_SCOPE = 'https://www.googleapis.com/auth/fitness.activity.read'
DATA_SOURCE = "derived:com.google.step_count.delta:com.google.android.gms:estimated_steps"
DATA_SET = "1051700038292387000-1451700038292387000"
REDIRECT_URI = 'urn:ietf:wg:oauth:2.0:oob'

def retrieve_data():
    flow = OAuth2WebServerFlow(CLIENT_ID, CLIENT_SECRET, OAUTH_SCOPE, REDIRECT_URI)
    authorize_url = flow.step1_get_authorize_url()

    code = raw_input('Enter verification code: ').strip()
    credentials = flow.step2_exchange(code)

    http = httplib2.Http()
    http = credentials.authorize(http)

    fitness_service = build('fitness', 'v1', http=http)

    return fitness_service.users().dataSources(). \
              datasets(). \
              get(userId='me', dataSourceId=DATA_SOURCE, datasetId=DATA_SET). \
              execute()

def nanoseconds(nanotime):
    dt = datetime.fromtimestamp(nanotime // 1000000000)
    return dt.strftime('%Y-%m-%d %H:%M:%S')

if __name__ == "__main__":

    dataset = retrieve_data()
    with open('dataset.txt', 'w') as outfile:
        json.dump(dataset, outfile)

    last_point = dataset["point"][-1]
    print("Start time:", nanoseconds(int(last_point.get("startTimeNanos", 0))))
    print("End time:", nanoseconds(int(last_point.get("endTimeNanos", 0))))
    print("Data type:", last_point.get("dataTypeName", None))
    print("Steps:", last_point["value"][0].get("intVal", None))

而且,是的,我可以在java中找到一些 示例,但是当我需要在 python 程序本身上使用它时,它并没有帮助。

4

0 回答 0