Dailymile.com is one of the premier website to share your training with your friends. They provide apis to build application. I wrote a small code to upload my runkeeper data to dailymile.com. And this is how I did.
- Register one application with dailymile.com as mentioned at http://www.dailymile.com/api/consumers/new.
- Get access token as documented at http://www.dailymile.com/api/documentation/oauth.
- Write a small code like following to extract data from a csv file and post it to dailymile.com using REST APIs as documented at http://www.dailymile.com/api/documentation
#!/usr/bin/python
from datetime import datetime, date, time
import httplib, urllib, string, datetime, sys
csvFile = open("test.csv", 'r')
for line in csvFile.readlines():
stringTokens = line.split(',')
timeTokens = stringTokens[4].split(':')
timetaken=(int(timeTokens[0])*60+int(timeTokens[1]))*60+int(timeTokens[2])
actDat = datetime.datetime.strptime(stringTokens[1], "%m-%d-%Y %H:%M:%S").strftime("%Y-%m-%d %H:%M:%S")
para = '{"oauth_token":"<Your Auth Code Goes here>", "message": "'+stringTokens[7].strip() + '", "workout": {"activity_type":"'+ string.lower(stringTokens[2]).strip() + '", "distance": {"value": ' + stringTokens[3] + ', "units":"kilometers"}, "duration": '+ str(timetaken) + ', "title":"' + stringTokens[1].strip() + '", "completed_at":"' + actDat +'" }}'
print para
headers = {"Accept": "application/json", "Content-type": "application/json"}
conn=httplib.HTTPSConnection("api.dailymile.com")
conn.request("POST","/entries.json", para, headers)
r1=conn.getresponse()
print r1.status, r1.reason
print r1.read()
conn.close()
Tweet
I was able to get a csv out from your other script, but I am getting an error when trying to use this script with it.
ReplyDeleteTraceback (most recent call last):
File "dailymile_brandontest.py", line 10, in
timetaken=(int(timeTokens[0])*60+int(timeTokens[1]))*60+int(timeTokens[2])
ValueError: invalid literal for int() with base 10: 'Time'
Hi Unknown,
ReplyDeleteI believe somewhere the literal is not coming as integer in csv. Can you check if csv created is correct?
Nitin
Hi -
ReplyDeleteI am not a programmer... I think I'll be able to plow my way through the python to do the import and I already have the .csv. But cannot seem to figure out how to get my 0auth token.
Any tips? I am looking at the dailymile doc for this right now. Just cannot seem to make it work.
I am using "www.google.com" as my callback url.
Thank You,
- Mike
I was now able to get the oauth token.
ReplyDeleteBut get a 401 error (unauthorized).
I've simplified the code line to:
conn.request("POST","/entries.json", '{"oauth_token": "tokenstring"}', headers)