forked from matryer/xbar-plugins
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjawbone.10m.py
executable file
·53 lines (37 loc) · 1.56 KB
/
jawbone.10m.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
#!/usr/bin/python
#
# <xbar.author>Till Dettmering</xbar.author>
# <xbar.author.github>dettmering</xbar.author.github>
# <xbar.title>Get steps and sleep duration from your Jawbone UP</xbar.title>
# <xbar.version>1.0</xbar.version>
# <xbar.desc>Get today's steps and sleep duration from your Jawbone UP!</xbar.desc>
# <xbar.image>http://i.imgur.com/JtguThL.png</xbar.image>
# <xbar.dependencies>python</xbar.dependencies>
# <xbar.abouturl>http://tilldettmering.com/</xbar.abouturl>
import json
import urllib2
# You have to follow these steps to request an access token:
# https://jawbone.com/up/developer/authentication
# Set your access token below:
access_token = "my token"
# Function for retrieving data:
def retrieveUpData(type):
url = "https://jawbone.com/nudge/api/v.1.1/users/@me/{0}".format(type)
request = urllib2.Request(url)
request.add_header("Authorization", "Bearer {0}".format(access_token))
response = urllib2.urlopen(request)
data = json.loads(response.read())
return data
# Get steps and distance walked:
step_data = retrieveUpData("moves")
steps = step_data['data']['items'][0]['details']['steps']
distance = step_data['data']['items'][0]['details']['km']
# Get hours of sleep:
sleep_data = retrieveUpData("sleeps")
sleep_duration = sleep_data['data']['items'][0]['details']['duration'] / 3600
sleep_percentage = sleep_duration / 24.0 * 100
# Format output:
print '{0} steps'.format(steps)
print '---'
print '{0} km'.format(round(distance, 1))
print '{0} h slept ({1}%)'.format(round(sleep_duration, 1), round(sleep_percentage, 0))