Last active
January 4, 2016 12:58
-
-
Save eternal512/8624620 to your computer and use it in GitHub Desktop.
Query Nike+ API to get elevation min/max/gain data
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python | |
""" Nike plus elevation data for last run """ | |
import json | |
import urllib2 | |
import prettytable as pt | |
# look here for docs: https://developer.nike.com/console | |
# i started with a script here (https://gist.github.com/leah/5655437) | |
ACCESS_TOKEN = YOUR_ACCESS_TOKEN_HERE_GO_TO_DEVELOPER_DOT_NIKE_DOT_COM_TO_GET_ONE | |
base_url = 'https://api.nike.com' | |
subCollec = '/me/sport/activities' | |
tokenTail = '?access_token=%s' % ACCESS_TOKEN | |
headers = {'appid':'fuelband', 'Accept':'application/json;charset=UTF-8'} # weird required headers, blah. | |
current_month = None | |
def getDataForURL(data_url): | |
req = urllib2.Request(data_url, None, headers) | |
r = urllib2.urlopen(req) | |
resp = json.loads(r.read()) | |
r.close() | |
return resp | |
def getLastActivity(): | |
""" returns the last activity (get ID by 'activityId') """ | |
data_url = '%s%s%s' % (base_url, subCollec,tokenTail) | |
data = getDataForURL(data_url)['data'] | |
if data: | |
activity = data[0] | |
return activity | |
else: | |
print "AHH! something went wrong for %s " % data_url | |
return None | |
def getGPS(ac_id): | |
spec = "/%s/gps" % ac_id | |
gps_url = "%s%s%s%s" % ( base_url, subCollec, spec, tokenTail) | |
print "asking for %s" % gps_url | |
data = getDataForURL(gps_url) | |
return data | |
def pp(js): | |
""" pretty prints js """ | |
return json.dumps(js,sort_keys = False, indent = 4) | |
def toFt(met): | |
"""convert meter to feet""" | |
return met*3.28084 | |
def kmToMiles(met): | |
"""converts km to miles""" | |
return met*0.621371; | |
def toTwo(val): | |
"""converts val to float, prints as 2 digits past decimal""" | |
x = float(val) | |
return "{0:.2f}".format(x) | |
def relevantInfo(activity,gps): | |
""" packs a map with the stuff I want to output """ | |
res = [] | |
res.append(("distance",toTwo(kmToMiles(activity['metricSummary']['distance'])))) | |
res.append(("duration",activity['metricSummary']['duration'])) | |
res.append(("calories",activity['metricSummary']['calories'])) | |
res.append(("steps",activity['metricSummary']['steps'])) | |
res.append(("elevationMin",toTwo(toFt(gps['elevationMin'])))) | |
res.append(("elevationMax",toTwo(toFt(gps['elevationMax'])))) | |
res.append(("elevationGain",toTwo(toFt(gps['elevationGain'])))) | |
return res | |
def getValues(cached=False): | |
if cached: | |
with open ('data/last_activity.json','r') as ins: | |
act = json.loads(ins.read()) | |
with open ('data/last_gps.json','r') as ins: | |
gps = json.loads(ins.read()) | |
else: | |
act = getLastActivity() | |
acID = act['activityId'] | |
gps = getGPS(acID) | |
with open('data/last_activity.json','w') as outs: | |
outs.write(pp(act)) | |
with open('data/last_gps.json','w') as outs: | |
outs.write(pp(gps)) | |
return (act,gps) | |
def asTable(rel): | |
""" prints relevatnInfo as table """ | |
flds = ["Metric","Value"] | |
x = pt.PrettyTable(flds) | |
x.aligns[0] = "l" | |
x.aligns[1] = "r" | |
for (f,v) in rel: | |
x.add_row([f,v]) | |
return x | |
#we use cached to avoid asking the server for the same data again and again while | |
# debugging, and testing some theories. | |
(act,gps) = getValues(cached=False) | |
print asTable(relevantInfo(act, gps)) |
updated to convert distance to miles
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Output is like this: