-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 6524ac9
Showing
5 changed files
with
236 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
{"calories_burnt": 1198.6584783802084} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
from flask import Flask, jsonify | ||
from google.oauth2 import service_account | ||
from googleapiclient.discovery import build | ||
import time | ||
from datetime import datetime | ||
from googleapiclient.discovery import build | ||
from oauth2client.client import OAuth2WebServerFlow | ||
|
||
from google_auth_oauthlib.flow import InstalledAppFlow | ||
from google.auth.transport.requests import Request | ||
import pickle | ||
import os.path | ||
import json | ||
|
||
# Copy your credentials from the Google Developers Console | ||
CLIENT_ID = '264617833817-3ogggshae5i2ccjo54ilsv80m8muuhuk.apps.googleusercontent.com' | ||
CLIENT_SECRET = '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' | ||
|
||
# Check https://developers.google.com/fit/rest/v1/reference/users/dataSources/datasets/get | ||
# for all available scopes | ||
OAUTH_SCOPE = 'https://www.googleapis.com/auth/fitness.activity.read' | ||
|
||
# DATA SOURCE | ||
DATA_SOURCE = "derived:com.google.step_count.delta:com.google.android.gms:estimated_steps" | ||
# The ID is formatted like: "startTime-endTime" where startTime and endTime are | ||
# 64 bit integers (epoch time with nanoseconds). | ||
TODAY = datetime.today().date() | ||
NOW = datetime.today() | ||
START = int(time.mktime(TODAY.timetuple())*1000000000) | ||
END = int(time.mktime(NOW.timetuple())*1000000000) | ||
DATA_SET = "%s-%s" % (START, END) | ||
|
||
|
||
creds = None | ||
# The file token.pickle stores the user's access and refresh tokens, and is | ||
# created automatically when the authorization flow completes for the first | ||
# time. | ||
if os.path.exists('token.pickle'): | ||
with open('token.pickle', 'rb') as token: | ||
creds = pickle.load(token) | ||
# If there are no (valid) credentials available, let the user log in. | ||
if not creds or not creds.valid: | ||
if creds and creds.expired and creds.refresh_token: | ||
creds.refresh(Request()) | ||
else: | ||
flow = InstalledAppFlow.from_client_secrets_file( | ||
'credentials.json', OAUTH_SCOPE) | ||
creds = flow.run_console() | ||
# Save the credentials for the next run | ||
with open('token.pickle', 'wb') as token: | ||
pickle.dump(creds, token) | ||
|
||
# Redirect URI for installed apps | ||
REDIRECT_URI = 'urn:ietf:wg:oauth:2.0:oob' | ||
|
||
flow = OAuth2WebServerFlow(CLIENT_ID, CLIENT_SECRET, OAUTH_SCOPE, REDIRECT_URI) | ||
|
||
fitness = build('fitness', 'v1', credentials=creds) | ||
|
||
response = fitness.users().dataset().aggregate( | ||
userId='me', | ||
body={ | ||
'aggregateBy': [ | ||
{'dataTypeName': 'com.google.calories.expended'}, | ||
#{'dataTypeName': 'com.google.heart_rate.bpm'} | ||
], | ||
'bucketByTime': {'durationMillis': 86400000}, | ||
'startTimeMillis': int(time.time() * 1000) - 86400000, | ||
'endTimeMillis': int(time.time() * 1000) | ||
} | ||
).execute() | ||
|
||
calories_burnt = response['bucket'][0]['dataset'][0]['point'][0]['value'][0]['fpVal'] | ||
#heart_rate = response['bucket'][0]['dataset'][1]['point'][0]['value'][0]['fpVal'] | ||
with open('calories.json', 'w') as f: | ||
json.dump({'calories_burnt': calories_burnt}, f) |
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
import pickle | ||
import os.path | ||
from googleapiclient.discovery import build | ||
from google_auth_oauthlib.flow import InstalledAppFlow | ||
from google.auth.transport.requests import Request | ||
import time | ||
from datetime import datetime | ||
from datetime import timedelta | ||
|
||
# If modifying these scopes, delete the file token.pickle. | ||
SCOPES = ['https://www.googleapis.com/auth/fitness.activity.read'] | ||
|
||
def main(): | ||
creds = None | ||
# The file token.pickle stores the user's access and refresh tokens, and is | ||
# created automatically when the authorization flow completes for the first | ||
# time. | ||
if os.path.exists('token.pickle'): | ||
with open('token.pickle', 'rb') as token: | ||
creds = pickle.load(token) | ||
# If there are no (valid) credentials available, let the user log in. | ||
if not creds or not creds.valid: | ||
if creds and creds.expired and creds.refresh_token: | ||
creds.refresh(Request()) | ||
else: | ||
flow = InstalledAppFlow.from_client_secrets_file( | ||
'credentials.json', SCOPES) | ||
creds = flow.run_console() | ||
# Save the credentials for the next run | ||
with open('token.pickle', 'wb') as token: | ||
pickle.dump(creds, token) | ||
|
||
fit = build('fitness', 'v1', credentials=creds) | ||
|
||
DATA_SOURCE = "derived:com.google.step_count.delta:com.google.android.gms:estimated_steps" | ||
# The ID is formatted like: "startTime-endTime" where startTime and endTime are | ||
# 64 bit integers (epoch time with nanoseconds). | ||
TODAY = datetime.today().date() | ||
NOW = datetime.today() | ||
START = int(time.mktime(TODAY.timetuple())*1000000000) | ||
END = int(time.mktime(NOW.timetuple())*1000000000) | ||
DATA_SET = "%s-%s" % (START, END) | ||
|
||
# Call the Drive v3 API | ||
response = fit.users().dataSources().list(userId='me').execute() | ||
#https://www.googleapis.com/fitness/v1/users/me/dataSources | ||
response2=fit.users().dataSources(). \ | ||
datasets(). \ | ||
get(userId='me', dataSourceId=DATA_SOURCE, datasetId=DATA_SET). \ | ||
execute() | ||
print(response) | ||
|
||
print(response2) | ||
|
||
if __name__ == '__main__': | ||
main() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
import json | ||
import time | ||
from datetime import datetime | ||
from googleapiclient.discovery import build | ||
from oauth2client.client import OAuth2WebServerFlow | ||
import pickle | ||
import os.path | ||
from google_auth_oauthlib.flow import InstalledAppFlow | ||
from google.auth.transport.requests import Request | ||
import csv | ||
|
||
# Copy your credentials from the Google Developers Console | ||
CLIENT_ID = '264617833817-3ogggshae5i2ccjo54ilsv80m8muuhuk.apps.googleusercontent.com' | ||
CLIENT_SECRET = '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' | ||
|
||
# Check https://developers.google.com/fit/rest/v1/reference/users/dataSources/datasets/get | ||
# for all available scopes | ||
OAUTH_SCOPE = 'https://www.googleapis.com/auth/fitness.activity.read' | ||
|
||
# DATA SOURCE | ||
DATA_SOURCE = "derived:com.google.step_count.delta:com.google.android.gms:estimated_steps" | ||
# The ID is formatted like: "startTime-endTime" where startTime and endTime are | ||
# 64 bit integers (epoch time with nanoseconds). | ||
TODAY = datetime.today().date() | ||
NOW = datetime.today() | ||
START = int(time.mktime(TODAY.timetuple())*1000000000) | ||
END = int(time.mktime(NOW.timetuple())*1000000000) | ||
DATA_SET = "%s-%s" % (START, END) | ||
|
||
|
||
creds = None | ||
# The file token.pickle stores the user's access and refresh tokens, and is | ||
# created automatically when the authorization flow completes for the first | ||
# time. | ||
if os.path.exists('token.pickle'): | ||
with open('token.pickle', 'rb') as token: | ||
creds = pickle.load(token) | ||
# If there are no (valid) credentials available, let the user log in. | ||
if not creds or not creds.valid: | ||
if creds and creds.expired and creds.refresh_token: | ||
creds.refresh(Request()) | ||
else: | ||
flow = InstalledAppFlow.from_client_secrets_file( | ||
'credentials.json', OAUTH_SCOPE) | ||
creds = flow.run_console() | ||
# Save the credentials for the next run | ||
with open('token.pickle', 'wb') as token: | ||
pickle.dump(creds, token) | ||
|
||
# Redirect URI for installed apps | ||
REDIRECT_URI = 'urn:ietf:wg:oauth:2.0:oob' | ||
|
||
flow = OAuth2WebServerFlow(CLIENT_ID, CLIENT_SECRET, OAUTH_SCOPE, REDIRECT_URI) | ||
''' | ||
authorize_url = flow.step1_get_authorize_url() | ||
webbrowser.open(authorize_url) | ||
code = input("Enter code -") | ||
credentials = flow.step2_exchange(code) | ||
# Create an httplib2.Http object and authorize it with our credentials | ||
http = httplib2.Http() | ||
http = credentials.authorize(http)''' | ||
|
||
|
||
fitness_service = build('fitness', 'v1', credentials=creds) | ||
""" | ||
Run through the OAuth flow and retrieve credentials. | ||
Returns a dataset (Users.dataSources.datasets): | ||
https://developers.google.com/fit/rest/v1/reference/users/dataSources/datasets | ||
""" | ||
|
||
dataset= fitness_service.users().dataSources().datasets().get(userId='me', dataSourceId=DATA_SOURCE, datasetId=DATA_SET).execute() | ||
|
||
with open('dataset.txt', 'w') as outfile: | ||
json.dump(dataset, outfile) | ||
|
||
starts,ends,values = [],[],[] | ||
|
||
def nanoseconds(nanotime): | ||
""" | ||
Convert epoch time with nanoseconds to human-readable. | ||
""" | ||
dt = datetime.fromtimestamp(nanotime // 1000000000) | ||
return dt.strftime('%H:%M:%S') | ||
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']) | ||
rows=[] | ||
a=list(map(lambda x:nanoseconds(x),starts)) | ||
b=list(map(lambda x:nanoseconds(x),ends)) | ||
for i,j,k in zip(a,b,values): | ||
print(i,"to",j,"---",k,"steps") | ||
print("Total Steps - ",sum(values)) | ||
|
||
dict={} | ||
for i,j in zip(a,values): | ||
if i in dict: | ||
dict[i]+=j | ||
else: | ||
dict[i]=j | ||
|