-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathupdate_google_fit.py
executable file
·108 lines (99 loc) · 4.55 KB
/
update_google_fit.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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
#!/usr/bin/env python
import json
from datetime import datetime
import MySQLdb.cursors
import httplib2
import pytz
from googleapiclient.discovery import build
from oauth2client import client
import backend
def get_aggregate(fit_service, startTimeMillis, endTimeMillis, dataSourceId):
return fit_service.users().dataset().aggregate(userId="me", body={
"aggregateBy": [{
"dataTypeName": "com.google.step_count.delta",
"dataSourceId": dataSourceId
}],
"bucketByTime": {"durationMillis": backend.ONE_DAY_MS},
"startTimeMillis": startTimeMillis,
"endTimeMillis": endTimeMillis
}).execute()
def get_and_store_fit_data(http_auth, cur, username, past_n_days=30):
fit_service = build('fitness', 'v1', http=http_auth)
n_days_ago_millis = backend.calc_n_days_ago(past_n_days)
# print(help(fit_service.users().dataset().aggregate))
steps = []
activity = []
now_millis = backend.current_milli_time()
print('get and store fitness data for user {}'.format(username))
try:
stepsData = get_aggregate(fit_service, n_days_ago_millis, now_millis, backend.STEPS_DATASOURCE)
for day in stepsData['bucket']:
# store local date in the database
d = datetime.fromtimestamp(int(day['startTimeMillis']) / 1000,
tz=pytz.timezone(backend.DEFAULT_TIMEZONE)).strftime(backend.DATE_FORMAT)
if day['dataset'][0]['point']:
s = day['dataset'][0]['point'][0]['value'][0]['intVal']
steps.append([d, s])
else:
steps.append([d, 0])
print("Steps:", steps)
except Exception as e:
print(e)
print("No steps found")
try:
activityData = get_aggregate(fit_service, n_days_ago_millis, now_millis, backend.ACTIVITY_DATASOURCE)
for day in activityData['bucket']:
# store local date in the database
d = datetime.fromtimestamp(int(day['startTimeMillis']) / 1000,
tz=pytz.timezone(backend.DEFAULT_TIMEZONE)).strftime(backend.DATE_FORMAT)
if day['dataset'][0]['point']:
for a in day['dataset'][0]['point']:
activity_type = a['value'][0]['intVal']
length_ms = a['value'][1]['intVal']
n_segments = a['value'][2]['intVal']
activity.append([d, activity_type, length_ms, n_segments])
else:
activity.append([d, 4, 0, 0])
print("Activity:", activity)
except Exception as e:
print(e)
print("No activity found")
try:
rows = cur.executemany("REPLACE INTO steps SET username=%s, day=%s, steps=%s".format(username),
[[username] + s for s in steps])
print("steps: {} rows affected".format(rows))
rows = cur.executemany(
"REPLACE INTO activity SET username=%s, day=%s, activity_type=%s, length_ms=%s, n_segments=%s",
[[username] + a for a in activity])
print("activity: {} rows affected".format(rows))
except Exception as e:
print(e)
return steps, activity
if __name__ == "__main__":
with open(backend.client_secret_file) as f:
client_secret_json = json.load(f)
client_id = client_secret_json['web']['client_id']
client_secret = client_secret_json['web']['client_secret']
db = MySQLdb.connect(host=backend.config.get('database_config', 'dbhost'),
port=int(backend.config.get('database_config', 'dbport')),
user=backend.config.get('database_config', 'dbuser'),
passwd=backend.config.get('database_config', 'dbpass'),
db=backend.config.get('database_config', 'dbname'),
cursorclass=MySQLdb.cursors.DictCursor)
cur = db.cursor()
n_rows = cur.execute("SELECT * FROM google_fit")
rows = cur.fetchall()
for r in rows:
username = r['username']
refresh_token = r['refresh_token']
creds = client.GoogleCredentials("", client_id, client_secret, refresh_token, 0,
"https://accounts.google.com/o/oauth2/token", "Python")
http_auth = creds.authorize(httplib2.Http())
try:
steps, activity = get_and_store_fit_data(http_auth, cur, username)
except Exception as e:
print("Unable to get fit data for {}! {}".format(username, e))
db.commit()
cur.close()
# disconnect from server
db.close()