Skip to content

Instantly share code, notes, and snippets.

@aniav
Created August 16, 2012 22:49
Show Gist options
  • Save aniav/3374308 to your computer and use it in GitHub Desktop.
Save aniav/3374308 to your computer and use it in GitHub Desktop.
Import runmania.com workouts to runkeeper.com
#!/usr/bin/python 2.7
# -*- coding: utf-8 -*-
import csv
import json
import logging
import optparse
import os
import requests
logging.basicConfig(level=logging.INFO,
format='%(asctime)s %(levelname)s %(message)s')
HEALTHGRAPH_API_BASE = 'https://api.runkeeper.com'
HEALTHGRAPH_CLIENT_ID = ''
HEALTHGRAPH_CLIENT_SECRET = ''
HEALTHGRAPG_TOKEN_URL = 'https://runkeeper.com/apps/token'
authorization_code = ''
# Runmania activities that can be mapped to runkeeper
r2r_activities_mapping = {
1: "Running", # Trening biegowy - dystans
2: "Running", # Trening biegowy - interwały
3: "Running", # Trening biegowy - pomiar czasu
4: "Running", # Zawody biegowe
5: "Swimming", # Pływanie
8: "Skating", # Rolki
9: "Cycling", # Rower
10: "Walking", # Marsz
11: "Other", # Inne
12: "Walking", # Chód sportowy
13: "Skiing", # Narty
}
def main():
parser = optparse.OptionParser()
parser.add_option("-f", "--file", dest="filename", help="Read from file",
metavar="FILE", type="string")
(options, args) = parser.parse_args()
if not options.filename:
raise Exception("You need to specify the rumania export CSV file.")
# Get the access token to be used with healthgraph
payload = {
'grant_type': 'authorization_code',
'code': authorization_code,
'client_id': HEALTHGRAPH_CLIENT_ID,
'client_secret': HEALTHGRAPH_CLIENT_SECRET,
'redirect_uri': 'http://localhost:3001' # Needs to stay that way for this moment
}
access_token = requests.get(HEALTHGRAPG_TOKEN_URL, params=payload)
# Read the CSV file
reader = csv.reader(open(options.filename, 'rb'), delimiter=',',
quotechar='"')
# Set the headers needed to post activities
headers = {
'Content-Type': 'application/vnd.com.runkeeper.NewFitnessActivity+json',
'Authorization': 'Bearer #%s' % access_token
}
# go through the rows and add activities, ommit first row
reader.next()
for row in reader:
activity_type = r2r_activities_mapping.get(int(row[1]), None)
if not activity_type:
log.info("No mapping for activity type number %d" % row[1])
continue
data = {
"start_time": row[0],
"type": activity_type,
"total_distance": float(row[2]) * 1000, # km to meters
"duration": row[3], #FIXME
# TODO: other values
"post_to_facebook": False,
"post_to_twitter": False,
}
#requests.post(HEALTHGRAPH_API_BASE, data=json.dumps(payload),
# headers=headers)
# TODO: finish it!
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment