private async void GetDataButton_Click(object sender, RoutedEventArgs e)
        {
            double progress = 0.0;

            var p = new PelotonDataDownloader();

            await SetProgress(0);

            string outputDirectory = OutputDirectoryTextBox.Text;

            if (!Directory.Exists(outputDirectory))
            {
                Logger.LogError($"The output directory does not exist: {outputDirectory}\nAborting.");
                return;
            }
            Logger.Log("Authenticating");
            var authTask = p.AuthenticateAsync(UsernameTextBox.Text, PasswordTextBox.Password, this, Path.Combine(outputDirectory, "authentication_response.json"));

            try
            {
                auth = await authTask;
            }
            catch (Exception ex)
            {
                HandleException(ex);
                Logger.Log("Aborting...");
                return;
            }

            progress += 5.0;
            await SetProgress(progress);

            Logger.Log("Success!");

            List <RideDatum> workoutList;

            Logger.Log("Getting list of workouts");
            try
            {
                workoutList = await p.GetWorkoutListAsync(auth, this, outputDirectory);
            } catch (Exception ex)
            {
                HandleException(ex);
                Logger.Log("Aborting...");
                return;
            }

            Logger.Log($"Received {workoutList.Count} workouts");

            progress += 5.0;
            await SetProgress(progress);

            double progressStep = (100 - progress) / workoutList.Count;
            var    jsonRoot     = Path.Combine(OutputDirectoryTextBox.Text, "json");

            if (!Directory.Exists(jsonRoot))
            {
                Directory.CreateDirectory(jsonRoot);
            }

            Logger.Log("Downloading data for each workout");
            bool redownloadWorkoutDetails = RedownloadCheckbox.IsChecked.HasValue && RedownloadCheckbox.IsChecked.Value;

            var jsonPath = Path.Combine(OutputDirectoryTextBox.Text, "json");

            if (!Directory.Exists(jsonPath))
            {
                Directory.CreateDirectory(jsonPath);
            }
            var metricsPath = Path.Combine(OutputDirectoryTextBox.Text, "metrics");

            if (!Directory.Exists(metricsPath))
            {
                Directory.CreateDirectory(metricsPath);
            }
            var detailsPath = Path.Combine(OutputDirectoryTextBox.Text, "details");

            if (!Directory.Exists(detailsPath))
            {
                Directory.CreateDirectory(detailsPath);
            }

            foreach (var ride in workoutList)
            {
                try
                {
                    var    fileName        = p.GetFileNameBaseFromRideDatum(ride);
                    string metricsFilename = Path.Combine(metricsPath, fileName + "_Metrics.csv");
                    string detailsJson     = Path.Combine(jsonPath, fileName + "_UserWorkoutDetails.json");
                    string detailsFilename = Path.Combine(detailsPath, fileName + "_UserWorkoutDetails.csv");

                    if (File.Exists(metricsFilename))
                    {
                        Logger.Log($"Already of workout metrics, skipping: {ride.ride.title} on {Util.DateTimeFromEpochSeconds(ride.device_time_created_at).ToShortDateString()}");
                    }
                    else
                    {
                        var data = await p.GetWorkoutMetricsAsync(ride, this, metricsFilename);

                        Logger.Log($"Writing data to {metricsFilename}");
                        p.OutputRideCSV(data, metricsFilename);
                        //var details = await p.GetWorkoutEventDetails(ride, this, filenameBase + "_EventDetails.json");
                    }

                    if (File.Exists(detailsFilename) && !redownloadWorkoutDetails)
                    {
                        Logger.Log($"Already have user details for workout, skipping: {ride.ride.title} on {Util.DateTimeFromEpochSeconds(ride.device_time_created_at).ToShortDateString()}");
                    }
                    else
                    {
                        var userDetails = await p.GetWorkoutUserDetails(ride, this, detailsJson);

                        var propertyDict = Util.GetObjectPropertiesAsDictionaryRecursive(userDetails);
                        Util.WriteDictionaryAsCSV(propertyDict, detailsFilename);
                    }
                } catch (Exception ex)
                {
                    HandleException(ex);
                    Logger.Log("Skipping workout");
                }
                progress += progressStep;
                await SetProgress(progress);
            }
            await SetProgress(100);

            Logger.Log("Successfully Completed Data Download!");
        }
Beispiel #2
0
        private async void GetDataButton_Click(object sender, RoutedEventArgs e)
        {
            double progress = 0.0;

            var p = new PelotonDataDownloader();

            await SetProgress(0);

            string outputDirectory = OutputDirectoryTextBox.Text;

            if (!Directory.Exists(outputDirectory))
            {
                Logger.LogError($"The output directory does not exist: {outputDirectory}\nAborting.");
                return;
            }
            Logger.Log("Authenticating");
            var authTask = p.AuthenticateAsync(UsernameTextBox.Text, PasswordTextBox.Password, this, Path.Combine(outputDirectory, "authentication_response.json"));

            try
            {
                auth = await authTask;
            }
            catch (Exception ex)
            {
                HandleException(ex);
                Logger.Log("Aborting...");
                return;
            }

            progress += 5.0;
            await SetProgress(progress);

            Logger.Log("Success!");

            List <RideDatum> workoutList;

            Logger.Log("Getting list of workouts");
            try
            {
                workoutList = await p.GetWorkoutListAsync(auth, this);
            } catch (Exception ex)
            {
                HandleException(ex);
                Logger.Log("Aborting...");
                return;
            }

            Logger.Log($"Received {workoutList.Count} workouts");

            progress += 5.0;
            await SetProgress(progress);

            double progressStep = (100 - progress) / workoutList.Count;

            Logger.Log("Downloading data for each workout");
            foreach (var ride in workoutList)
            {
                try
                {
                    string filenameBase = Path.Combine(OutputDirectoryTextBox.Text, p.GetFileNameBaseFromRideDatum(ride));
                    string path         = filenameBase + "_Metrics.csv";
                    if (File.Exists(path))
                    {
                        Logger.Log($"Skipping workout: {ride.ride.title} on {Util.DateTimeFromEpochSeconds(ride.device_time_created_at).ToShortDateString()} because file already exists");
                    }
                    else
                    {
                        var data = await p.GetWorkoutMetricsAsync(ride, this, filenameBase + "_Metrics.json");

                        Logger.Log($"Writing data to {path}");
                        p.OutputRideCSV(data, path);
                        var details = await p.GetWorkoutEventDetails(ride, this, filenameBase + "_EventDetails.json");
                    }
                } catch (Exception ex)
                {
                    HandleException(ex);
                    Logger.Log("Skipping workout");
                }
                progress += progressStep;
                await SetProgress(progress);
            }
            await SetProgress(100);

            Logger.Log("Successfully Completed Data Download!");
        }