Skip to content

Commit

Permalink
[404] fix just walk outdoor workout missing gps data (philosowaffle#405)
Browse files Browse the repository at this point in the history
  • Loading branch information
philosowaffle authored Jan 4, 2023
1 parent 08a6bfa commit ecd2bf2
Show file tree
Hide file tree
Showing 7 changed files with 61,459 additions and 47 deletions.
2 changes: 1 addition & 1 deletion src/Common/Constants.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,6 @@ public static class Constants
public const string ApiName = "p2g_api";
public const string WebUIName = "p2g_webui";

public const string AppVersion = "3.3.0";
public const string AppVersion = "3.3.1-rc";
}
}
8 changes: 7 additions & 1 deletion src/Peloton/ApiClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ public async Task<PelotonApiAuthentication> GetAuthAsync(string overrideUserAgen
{
var response = await $"{AuthBaseUrl}"
.WithHeader("Accept-Language", "en-US")
.WithHeader("User-Agent", overrideUserAgent ?? "PostmanRuntime/7.26.10")
.WithHeader("User-Agent", overrideUserAgent ?? "PostmanRuntime/7.26.20")
.StripSensitiveDataFromLogging(auth.Email, auth.Password)
.WithTimeout(30)
.PostJsonAsync(new AuthRequest()
Expand Down Expand Up @@ -89,6 +89,7 @@ public async Task<PagedPelotonResponse<Workout>> GetWorkoutsAsync(int pageSize,
var auth = await GetAuthAsync();
return await $"{BaseUrl}/user/{auth.UserId}/workouts"
.WithCookie("peloton_session_id", auth.SessionId)
.WithCommonHeaders()
.SetQueryParams(new
{
limit = pageSize,
Expand All @@ -105,6 +106,7 @@ public async Task<PelotonResponse<Workout>> GetWorkoutsAsync(DateTime fromUtc, D
var auth = await GetAuthAsync();
return await $"{BaseUrl}/user/{auth.UserId}/workouts"
.WithCookie("peloton_session_id", auth.SessionId)
.WithCommonHeaders()
.SetQueryParams(new
{
from = fromUtc.ToString("o", CultureInfo.InvariantCulture),
Expand All @@ -124,6 +126,7 @@ public async Task<JObject> GetWorkoutsAsync(string userId, int numWorkouts, int
var auth = await GetAuthAsync();
return await $"{BaseUrl}/user/{userId}/workouts"
.WithCookie("peloton_session_id", auth.SessionId)
.WithCommonHeaders()
.SetQueryParams(new
{
limit = numWorkouts,
Expand All @@ -140,6 +143,7 @@ public async Task<UserData> GetUserDataAsync()
var auth = await GetAuthAsync();
return await $"{BaseUrl}/me"
.WithCookie("peloton_session_id", auth.SessionId)
.WithCommonHeaders()
.StripSensitiveDataFromLogging(auth.Email, auth.Password)
.GetJsonAsync<UserData>();
}
Expand All @@ -149,6 +153,7 @@ public async Task<JObject> GetWorkoutByIdAsync(string id)
var auth = await GetAuthAsync();
return await $"{BaseUrl}/workout/{id}"
.WithCookie("peloton_session_id", auth.SessionId)
.WithCommonHeaders()
.SetQueryParams(new
{
joins = "ride,ride.instructor"
Expand All @@ -162,6 +167,7 @@ public async Task<JObject> GetWorkoutSamplesByIdAsync(string id)
var auth = await GetAuthAsync();
return await $"{BaseUrl}/workout/{id}/performance_graph"
.WithCookie("peloton_session_id", auth.SessionId)
.WithCommonHeaders()
.SetQueryParams(new
{
every_n=1
Expand Down
9 changes: 8 additions & 1 deletion src/Peloton/Extensions.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using Flurl.Http;
using System;

namespace Peloton;

Expand All @@ -12,4 +13,10 @@ public static void EnsurePelotonCredentialsAreProvided(this Common.Peloton setti
if (string.IsNullOrEmpty(settings.Password))
throw new ArgumentException("Peloton Password must be set.", nameof(settings.Password));
}

public static IFlurlRequest WithCommonHeaders(this IFlurlRequest request)
{
return request
.WithHeader("Peloton-Platform", "web");
}
}
2 changes: 1 addition & 1 deletion src/Peloton/PelotonService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -258,7 +258,7 @@ public async Task<P2GWorkout[]> GetWorkoutDetailsAsync(ICollection<Workout> work
return results.ToArray();
}

private async Task<P2GWorkout> GetWorkoutDetailsAsync(string workoutId)
public async Task<P2GWorkout> GetWorkoutDetailsAsync(string workoutId)
{
using var tracing = Tracing.Trace($"{nameof(PelotonService)}.{nameof(GetWorkoutDetailsAsync)}.Item")
.WithWorkoutId(workoutId);
Expand Down
85 changes: 60 additions & 25 deletions src/UnitTests/AdHocTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ public void Setup()
// var email = "";
// var password = "";

// var workoutId = "";
// var workoutId = "13afceebe0f74a338f60bf9d70f657ef";
// var userId = "";

// var settings = new Settings()
Expand All @@ -93,11 +93,44 @@ public void Setup()
// var client = new ApiClient(settingMock.Object);

// //var recentWorkouts = await client.GetWorkoutsAsync(userId, 5, 0);
// var workoutSamples = await client.GetWorkoutsAsync(System.DateTime.UtcNow.AddDays(-1), System.DateTime.UtcNow);
// await client.GetUserDataAsync();
// var workouts = await client.GetWorkoutsAsync(System.DateTime.UtcNow.AddDays(-1), System.DateTime.UtcNow);
// var workoutSamples = await client.GetWorkoutSamplesByIdAsync(workoutId);

// Log.Debug(workoutSamples.ToString());
// //SaveRawData(workoutSamples, workoutId, DataDirectory);
// //await client.GetUserDataAsync();

// //Log.Debug(workoutSamples.ToString());
// SaveRawData(workoutSamples, workoutId, DataDirectory);
//}

//[Test]
//public async Task DownloadAndSaveP2GWorkoutDetails()
//{
// var email = "";
// var password = "";

// var workoutId = "13afceebe0f74a338f60bf9d70f657ef";
// var userId = "";

// var settings = new Settings()
// {
// Peloton = new()
// {
// Email = email,
// Password = password,
// }
// };

// var autoMocker = new AutoMocker();
// var settingMock = autoMocker.GetMock<ISettingsService>();
// settingMock.Setup(s => s.GetSettingsAsync()).ReturnsAsync(settings);

// var fileHandler = autoMocker.GetMock<IFileHandling>();

// var client = new ApiClient(settingMock.Object);
// var service = new PelotonService(settingMock.Object, client, fileHandler.Object);

// var p2gWorkout = await service.GetWorkoutDetailsAsync(workoutId);
// SaveData(p2gWorkout, workoutId, DataDirectory);
//}

//[Test]
Expand All @@ -108,38 +141,40 @@ public void Setup()
// var workout = _fileHandler.DeserializeJson<RecentWorkouts>(file);
//}

//[Test]
//public async Task Convert()
//[Test]
//public async Task Convert_From_File()
//{
// var file = Path.Join(DataDirectory, "rower_workout.json");
// var file = Path.Join(DataDirectory, "just_walk_outdoor.json");
// //var file = Path.Join(DataDirectory, "cycling_target_metrics.json");
// //var file = Path.Join(DataDirectory, "tread_run_workout.json");

// var autoMocker = new AutoMocker();
// var autoMocker = new AutoMocker();
// var settingsService = autoMocker.GetMock<SettingsService>();


// var settings = new Settings()
// {
// Format = new Format()
// {
// Rowing = new Rowing() { PreferredLapType = PreferredLapType.Class_Segments }
// }
// };
// };
// var fileHandler = new IOWrapper();

// settingsService.SetReturnsDefault(settings);

// var fitConverter = new ConverterInstance(settingsService.Object, fileHandler);
// var messages = await fitConverter.Convert(file, settings);

// var output = Path.Join(DataDirectory, "output.fit");

// fitConverter.Save(messages, output);
//}

// settingsService.SetReturnsDefault(settings);

// var fitConverter = new ConverterInstance(settingsService.Object, fileHandler);
// var messages = await fitConverter.Convert(file, settings);

// var output = Path.Join(DataDirectory, "output.fit");

// fitConverter.Save(messages, output);
//}

private void SaveRawData(dynamic data, string workoutId, string path)
{
System.IO.File.WriteAllText(Path.Join(path, $"{workoutId}_workout.json"), data.ToString());
}

private void SaveData(object data, string fileName, string path)
{
var serializedData = JsonSerializer.Serialize(data, new JsonSerializerOptions() { PropertyNameCaseInsensitive = true, WriteIndented = true });
System.IO.File.WriteAllText(Path.Join(path, $"{fileName}_workout.json"), serializedData.ToString());
}

private async Task<JObject> GetRecentWorkoutsAsync(string userId, int numWorkouts = 3)
Expand Down
Loading

0 comments on commit ecd2bf2

Please sign in to comment.