-
Notifications
You must be signed in to change notification settings - Fork 0
/
sample-get-sleep-data.py
47 lines (38 loc) · 1.14 KB
/
sample-get-sleep-data.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
#!/usr/bin/env python
# -*- coding: UTF-8 -*-
# https://dev.fitbit.com/docs
import requests
import json
import time
import datetime
import os
filename = "{path to]/refresh.py"
execfile(filename)
def get_sleep(datevar):
# date should be a datetime.date object ie "2016-03-23".
url = "https://api.fitbit.com/1/user/-/sleep/date/"+datevar+".json"
access_path = "{path declared in refresh.py}/access.txt"
# open and read refresh.txt to var remove newline
opr = open(access_path, "r")
token = opr.readline().strip()
access_token = "Bearer %s" % (token)
opr.close()
headers = {
'authorization': access_token,
'cache-control': "no-cache"
}
response = requests.request("GET", url, headers=headers)
#print(response.text)
return response.json()
#get todays date
to = time.strftime("%Y-%m-%d")
#run funtion with todays date
get_sleep(to)
#if there is sleep data print specific json elements
if len(todayvar['sleep']) >= 1:
myvar = todayvar['sleep'][0]['isMainSleep']
startTime = todayvar['sleep'][0]['startTime']
print(myvar)
print(startTime)
else:
print("No Sleep Data")