forked from philosowaffle/peloton-to-garmin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
peloton-to-garmin.py
121 lines (95 loc) · 3.57 KB
/
peloton-to-garmin.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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
#
# Author: Bailey Belvis (https://github.com/philosowaffle)
#
# Tool to generate a valid TCX file from a Peloton activity for Garmin.
#
import os
import sys
import json
import logging
from lib import pelotonApi
from lib import config_helper as config
from lib import tcx_builder
##############################
# Debugging Setup
##############################
if config.ConfigSectionMap("DEBUG")['pauseonfinish'] is None:
pause_on_finish = "false"
else:
pause_on_finish = config.ConfigSectionMap("DEBUG")['pauseonfinish']
##############################
# Logging Setup
##############################
if len(sys.argv) > 3:
file_handler = logging.FileHandler(sys.argv[3])
else:
if config.ConfigSectionMap("LOGGER")['logfile'] is None:
logger.error("Please specify a path for the logfile.")
sys.exit(1)
file_handler = logging.FileHandler(config.ConfigSectionMap("LOGGER")['logfile'])
logger = logging.getLogger('peloton-to-garmin')
# logger.setLevel(logging.DEBUG)
logger.setLevel(logging.INFO)
# Formatter
formatter = logging.Formatter('%(asctime)s - %(levelname)s - %(name)s: %(message)s')
# File Handler
file_handler.setLevel(logging.DEBUG)
file_handler.setFormatter(formatter)
# Console Handler
console_handler = logging.StreamHandler()
console_handler.setLevel(logging.INFO)
console_handler.setFormatter(formatter)
logger.addHandler(file_handler)
logger.addHandler(console_handler)
logger.debug("Peloton to Garmin Magic :)")
##############################
# Environment Variables Setup
##############################
if os.getenv("NUM_ACTIVITIES") is not None:
numActivities = os.getenv("NUM_ACTIVITIES")
else:
numActivities = None
if os.getenv("OUTPUT_DIRECTORY") is not None:
output_directory = os.getenv("OUTPUT_DIRECTORY")
else:
output_directory = config.ConfigSectionMap("OUTPUT")['directory']
##############################
# Peloton Setup
##############################
if len(sys.argv) > 2:
user_email = sys.argv[1]
user_password = sys.argv[2]
else :
if config.ConfigSectionMap("PELOTON")['email'] is None:
logger.error("Please specify your Peloton login email in the config.ini file.")
sys.exit(1)
if config.ConfigSectionMap("PELOTON")['password'] is None:
logger.error("Please specify your Peloton login password in the config.ini file.")
sys.exit(1)
user_email = config.ConfigSectionMap("PELOTON")['email']
user_password = config.ConfigSectionMap("PELOTON")['password']
api = pelotonApi.PelotonApi(user_email, user_password)
##############################
# Main
##############################
if numActivities is None:
numActivities = input("How many past activities do you want to grab? ")
logger.info("Get latest " + str(numActivities) + " workouts.")
workouts = api.getXWorkouts(numActivities)
for w in workouts:
workoutId = w["id"]
logger.info("Get workout: " + str(workoutId))
workout = api.getWorkoutById(workoutId)
logger.info("Get workout samples")
workoutSamples = api.getWorkoutSamplesById(workoutId)
logger.info("Get workout summary")
workoutSummary = api.getWorkoutSummaryById(workoutId)
logger.info("Writing TCX file")
try:
tcx_builder.workoutSamplesToTCX(workout, workoutSummary, workoutSamples, output_directory)
except Exception as e:
logger.error("Failed to write TCX file for workout {} - Exception: {}".format(workoutId, e))
logger.info("Done!")
logger.info("Your Garmin TCX files can be found in the Output directory: " + output_directory)
if pause_on_finish == "true":
input("Press the <ENTER> key to continue...")