-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
87 lines (71 loc) · 2.83 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# -*- coding: utf-8 -*-
from __future__ import print_function
import json
import time
from os import path, makedirs
import pickle
from googleapiclient.discovery import build
from google_auth_oauthlib.flow import InstalledAppFlow
from google.auth.transport.requests import Request
import pprint
pp = pprint.PrettyPrinter(indent=4)
# If modifying these scopes, delete the file token.pickle.
SCOPES = ['https://www.googleapis.com/auth/fitness.activity.read']
# DATA SOURCE
DATA_SOURCE = "derived:com.google.step_count.delta:com.google.android.gms:estimated_steps"
START_TIME = "1051700038292387000-"
# test time
# START_TIME = "1577854800000000000-"
def saveJSON(name, data):
dir_name = path.dirname(path.abspath(__file__))
file_name = "exports/" + name + "-" + str(int(time.time())) + ".json"
full_name = path.join(dir_name, file_name)
with open(full_name, 'w') as outfile:
json.dump(data, outfile, indent=4, sort_keys=True)
def main():
"""Shows basic usage of the FIT API.
"""
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 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_local_server(port=54547)
# Save the credentials for the next run
with open('token.pickle', 'wb') as token:
pickle.dump(creds, token)
service = build('fitness', 'v1', credentials=creds)
# Call the Fitness API
DATA_SET = START_TIME + str(time.time_ns())
data_sources = service.users().dataSources(). \
list(userId='me'). \
execute()
saveJSON("dataSources", data_sources)
# create subdir for datastreams
timestamp = str(time.time_ns())
makedirs(f"exports/segments/{timestamp}")
data_list = []
for index, s in enumerate(data_sources['dataSource']):
try:
dataset = service.users().dataSources(). \
datasets(). \
get(userId='me', dataSourceId=s['dataStreamId'], datasetId=DATA_SET). \
execute()
# saveJSON("segments/" + s['dataStreamId'], dataset)
saveJSON( f"segments/{timestamp}/{s['dataStreamId']}", dataset)
# data_list.append(dataset)
except Exception as e:
print("Error at " + s['dataStreamId'])
print(e)
# saveJSON("dataset", data_list)
if __name__ == '__main__':
main()