-
Notifications
You must be signed in to change notification settings - Fork 3
/
bandsaw.py
56 lines (43 loc) · 2.12 KB
/
bandsaw.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
import csv
import datetime
import re
import requests
CSV_NAME = 'data.csv'
def create_csv():
with open(CSV_NAME, 'wb') as csv_file:
writer = csv.writer(csv_file)
writer.writerow(['timestamp', 'skin_temp', 'air_temp', 'heartrate', 'steps', 'gsr', 'calories'])
def append_to_csv(json):
"""Takes JSON and flattens into CSVs."""
with open(CSV_NAME, 'ab') as csv_file:
writer = csv.writer(csv_file)
if 'endtime' not in json:
return
for i in range((json['endtime'] - json['starttime']) / json['interval']):
unix_time_utc = json['starttime'] + i*json['interval']
skin_temp = json['metrics']['skin_temp']['values'][i]
air_temp = json['metrics']['air_temp']['values'][i]
heartrate = json['metrics']['heartrate']['values'][i]
steps = json['metrics']['steps']['values'][i]
gsr = json['metrics']['gsr']['values'][i]
calories = json['metrics']['calories']['values'][i]
timestamp = datetime.datetime.fromtimestamp(unix_time_utc)
writer.writerow([timestamp, skin_temp, air_temp, heartrate, steps, gsr, calories])
def get_user_id(session):
"""This won't work until logging into the site with a username and password is working properly."""
js = session.get('https://app.mybasis.com/js/lib/basis_api.js').text
m = re.search('ClientID:"(?P<user_id>[0-9a-f])"', js)
if m is None:
return None
return m.group('user_id')
def get_data(user_id, start_date, end_date):
delta = datetime.timedelta(days=1)
d = start_date
while d <= end_date:
url = 'https://app.mybasis.com/api/v1/chart/{0}.json?summary=true&interval=60&units=ms&start_date={1}&start_offset=-10800&end_offset=10800&heartrate=true&steps=true&calories=true&gsr=true&skin_temp=true&air_temp=true&bodystates=true'.format(user_id, d.strftime('%Y-%m-%d'))
yield requests.get(url).json()
d += delta
if __name__ == '__main__':
create_csv()
for data in get_data('BASIS USER ID GOES HERE', datetime.datetime(2013, 8, 13), datetime.datetime(2013, 8, 28)):
append_to_csv(data)