Skip to content

Instantly share code, notes, and snippets.

@paulcalabro paulcalabro/mmr_tcx_downloader.py Secret
Last active Sep 15, 2019

Embed
What would you like to do?
This script downloads Training Center XML (TCX) files from MapMyRun
#!/usr/bin/env python3
from getpass import getpass
from requests import request
###########################################
# Prompt the user for required information.
###########################################
print('\n' + ('*' * 25) + ' Required Information: ' + ('*' * 25))
username = input('Username: ')
password = getpass(prompt='Password (hidden): ')
month = input('Month (e.g. 09): ')
year = input('Years (e.g. 2018): ')
print(('*' * 73) + '\n')
####################
# Login to MapMyRun.
####################
url = 'https://www.mapmyrun.com/auth/login'
headers = {'Accept': 'application/json', 'Content-Type': 'application/json'}
payload = {'username': username, 'password': password}
response = request('POST', url, headers=headers, json=payload)
json_object = response.json()
auth_token = json_object['item']['authToken']
######################
# Get calendar events.
######################
url = 'https://www.mapmyrun.com/workouts/dashboard.json'
querystring = {'month': month, 'year': year}
headers = {'Accept': 'application/json', 'Cookie': "auth-token={0}".format(auth_token)}
response = request('GET', url, headers=headers, params=querystring)
json_object = response.json()
dates = json_object['workout_data']['workouts']
files = []
for date in dates:
workouts = dates[date]
for workout in workouts:
date = workout['date']
url = workout['view_url']
workout_id = url.split('/')[2]
file_name = date.replace('/', '_') + '.' + str(workouts.index(workout)) + '.tcx'
workout_tcx_url = "https://www.mapmyrun.com/workout/export/{0}/tcx".format(workout_id)
file_data = {'file_name': file_name, 'workout_tcx_url': workout_tcx_url}
files.append(file_data)
#####################
# Download TCX files.
#####################
for file in files:
headers = {'Accept': 'application/json', 'Cookie': "auth-token={0}".format(auth_token)}
url = file['workout_tcx_url']
response = request('GET', url, headers=headers)
if response.status_code == 200:
download_directory = 'data'
file_name = file['file_name']
with open("{0}/{1}".format(download_directory, file_name), 'wb') as file:
file.write(response.content)
print("[✓] {0}".format(file_name))
print('\n')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.