-
Notifications
You must be signed in to change notification settings - Fork 0
/
Program.cs
368 lines (313 loc) · 14.8 KB
/
Program.cs
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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
/*
* Runs on MS Windows only
* Built by Visual Studio 2022 Community
* Target framework = .NET 6.0
* Nullable = Disable
* Implicit global usings = false
* Installed Packages:
* Unofficial.Garmin.Connect 0.2.0+
* Microsoft.Extensions.Configuration
* Microsoft.Extensions.Configuration.Json
* Microsoft.Extensions.Configuration.Binder
* Newtonsoft.Json
*/
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Dynamic;
using System.Globalization;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Reflection;
using System.Text;
using System.Threading;
using System.Web;
using Garmin.Connect;
using Garmin.Connect.Auth;
using Garmin.Connect.Models;
using Microsoft.Extensions.Configuration;
using Newtonsoft.Json;
using Newtonsoft.Json.Converters;
using static System.Console;
TimeSpan maxGarminStravaTimeDifference = new( 0, 5, 0 );
Thread.CurrentThread.CurrentCulture = Thread.CurrentThread.CurrentUICulture = CultureInfo.InvariantCulture;
// ----------------------------- Read configuration -----------------------------
Settings settings = new ConfigurationBuilder().AddJsonFile( "appsettings.json" ).Build().GetRequiredSection( "Settings" ).Get<Settings>();
if ( settings.DateAfter == DateTime.MinValue || settings.DateBefore == DateTime.MinValue )
{
DateTime dateTimeNow = DateTime.Now;
settings.DateAfter = new( dateTimeNow.Year, dateTimeNow.Month, dateTimeNow.Day );
settings.DateBefore = settings.DateAfter.AddDays( 1 );
}
else
{
settings.DateAfter = new( settings.DateAfter.Year, settings.DateAfter.Month, settings.DateAfter.Day );
settings.DateBefore = new( settings.DateBefore.Year, settings.DateBefore.Month, settings.DateBefore.Day );
}
WriteLine( $"Time interval = {settings.DateAfter:yyyy-MM-dd} - {settings.DateBefore:yyyy-MM-dd}" );
List<(string, string)> propertiesToDescription = new();
foreach ( string propertyCfg in ( settings.PropertiesToDescription ?? "" ).Split( ';', StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries ) )
{
int formatIndex = propertyCfg.IndexOf( ':' );
if ( formatIndex == -1 )
propertiesToDescription.Add( (propertyCfg, null) );
else
propertiesToDescription.Add( (propertyCfg[..formatIndex], propertyCfg[( formatIndex + 1 )..]) );
}
// ----------------------------- Authorize to Strava -----------------------------
using HttpListener httpListener = new();
string redirectPath = $"/Temporary_Listen_Addresses/{Guid.NewGuid()}/";
httpListener.Prefixes.Add( "http://+:80" + redirectPath );
Process.Start( new ProcessStartInfo
{
FileName = "https://www.strava.com/oauth/authorize?" +
$"client_id={settings.StravaClientId}&" +
$"redirect_uri=http://localhost//{redirectPath}&" +
"response_type=code&" +
"scope=activity:read_all,activity:write,profile:write",
UseShellExecute = true
} );
httpListener.Start();
WriteLine( "Waiting for Strava authentication..." );
HttpListenerContext context = httpListener.GetContext();
string stravaCode = context.Request.QueryString["code"] ?? throw new( "! Strava 'code' missing in the http redirect query." );
HttpListenerResponse response = context.Response;
byte[] buffer = Encoding.UTF8.GetBytes( "<html><body><h1>Authorization successful!</h1></body></html>" );
response.ContentLength64 = buffer.Length;
response.OutputStream.Write( buffer, 0, buffer.Length );
response.OutputStream.Close();
WriteLine( $"Strava code = {stravaCode}" );
httpListener.Stop();
string[] stravaApiUsages = null, stravaApiLimits = null;
try
{
// ----------------------------- Get Strava access token -----------------------------
using HttpClient stravaHttpClient = new()
{
BaseAddress = new Uri( "https://www.strava.com" )
};
string authUrl = "/oauth/token?" +
$"client_id={settings.StravaClientId}&" +
$"client_secret={settings.StravaSecret}&" +
$"code={stravaCode}&" +
"grant_type=authorization_code";
HttpResponseMessage stravaResponse = await stravaHttpClient.PostAsync( authUrl, null );
string stravaAccessToken = ( await stravaResponse.Content.ReadAsStringAsync() ).JsonDeserialize().access_token;
WriteLine( $"Strava access token = {stravaAccessToken}" );
// ----------------------------- Connect to Garmin -----------------------------
BasicAuthParameters authParameters = new( settings.GarminLogin, settings.GarminPassword );
using HttpClient httpClient = new();
GarminConnectClient client = new( new GarminConnectContext( httpClient, authParameters ) );
// ----------------------------- Update weight -----------------------------
if ( settings.UpdateWeight )
{
GarminUserSettings garminUserSettings = await client.GetUserSettings();
stravaResponse = await stravaHttpClient.PutAsync(
$"/api/v3/athlete?" +
$"weight={garminUserSettings.UserData.Weight / 1000}&" +
$"access_token={stravaAccessToken}", null );
if ( stravaResponse.StatusCode != HttpStatusCode.OK )
throw new( "! Error updating weight {resultPut.StatusCode}." );
WriteLine( $"Athlete weight updated to {( garminUserSettings.UserData.Weight / 1000 ):0.0}" );
checkStravaApiLimits();
}
// ----------------------------- Read Strava activities -----------------------------
WriteLine( "Reading Strava activities, please wait..." );
List<dynamic> stravaActivities = new();
for ( int stravaActivitiesPage = 1; ; stravaActivitiesPage++ )
{
string getActivitiesUrl = "/api/v3/athlete/activities?" +
$"before={settings.DateBefore.DateTimeToUnixTimestamp()}&" +
$"after={settings.DateAfter.DateTimeToUnixTimestamp()}&" +
$"page={stravaActivitiesPage}&" +
"per_page=200&" +
$"access_token={stravaAccessToken}";
stravaResponse = await stravaHttpClient.GetAsync( getActivitiesUrl );
if ( stravaResponse.StatusCode != HttpStatusCode.OK )
throw new( $"! Error {stravaResponse.StatusCode} when reading Strava activities." );
checkStravaApiLimits();
List<ExpandoObject> newActivities = ( await stravaResponse.Content.ReadAsStringAsync() ).JsonDeserializeList();
if ( !newActivities.Any() )
break;
foreach ( dynamic stravaActivity in newActivities )
WriteLine(
$"\t{stravaActivity.type}\t" +
$"{stravaActivity.start_date_local}\t" +
$"{stravaActivity.name}" );
stravaActivities.AddRange( newActivities );
}
if ( stravaActivities.Count == 0 )
{
WriteLine( $"No Strava activities" );
return;
}
// ----------------------------- Read Garmin activities -----------------------------
WriteLine( "Reading Garmin activities, please wait..." );
GarminActivity[] garminActivities = await client.GetActivitiesByDate( settings.DateAfter, settings.DateBefore.AddDays( -1 ), null );
if ( garminActivities.Length == 0 )
{
Write( $"No Garmin activities" );
return;
}
// ----------------------------- Synchronize Name and/or Description from Garmin to Strava -----------------------------
foreach ( GarminActivity garminActivity in garminActivities )
{
WriteLine(
$"\t{garminActivity.ActivityType.TypeKey}\t" +
$"{garminActivity.StartTimeLocal}\t" +
$"{garminActivity.ActivityName}" );
var foundGarminInStrava =
from dynamic stravaActivity
in stravaActivities
where ( garminActivity.StartTimeGmt - stravaActivity.start_date ).Duration() < maxGarminStravaTimeDifference ||
( garminActivity.StartTimeLocal - stravaActivity.start_date_local ).Duration() < maxGarminStravaTimeDifference
select stravaActivity;
if ( foundGarminInStrava.Count() != 1 )
WriteLine( $"\t! Garmin activity not found in Strava!" );
else
{
dynamic stravaActivity = foundGarminInStrava.First();
string updateName = "";
if ( settings.UpdateName && garminActivity.ActivityName != stravaActivity.name )
updateName = $"&name={HttpUtility.UrlEncode( garminActivity.ActivityName )}";
string updateDescription = "";
if ( settings.UpdateDescription && ( !string.IsNullOrEmpty( garminActivity.Description ) || settings.GearsToDescription || propertiesToDescription.Any() ) )
{
StringBuilder garminActivityDescription = new( garminActivity.Description ?? "" );
if ( propertiesToDescription.Any() )
{
foreach ( (string propertyName, string propertyFormat) in propertiesToDescription )
{
PropertyInfo propertyInfo = garminActivity.GetType().GetProperty( propertyName ) ?? throw new( $"! Activity property '{propertyName}' does not exist!" );
object propertyValue = propertyInfo.GetValue( garminActivity );
if ( propertyValue != null && ( propertyValue.GetType().IsValueType
? !propertyValue.Equals( Activator.CreateInstance( propertyValue.GetType() ) )
: propertyValue != null ) )
garminActivityDescription
.AppendLine()
.Append( $"{propertyName}=" )
.Append( propertyFormat == null
? propertyValue.ToString()
: string.Format( $"{{0:{propertyFormat}}}", propertyValue ) );
}
}
if ( settings.GearsToDescription )
{
GarminGear[] gears = await client.GetActivityGears( garminActivity.ActivityId );
if ( gears.Length != 0 )
{
garminActivityDescription
.AppendLine()
.AppendLine()
.Append( "Gears:" );
foreach ( GarminGear gear in gears )
garminActivityDescription
.AppendLine()
.Append( "* " )
.Append( gear.DisplayName );
}
}
string getActivitysUrl = $"api/v3/activities/{stravaActivity.id}?" +
$"access_token={stravaAccessToken}";
stravaResponse = await stravaHttpClient.GetAsync( getActivitysUrl );
if ( stravaResponse.StatusCode != HttpStatusCode.OK )
throw new( $"! Error {stravaResponse.StatusCode} when reading Strava activity." );
checkStravaApiLimits();
dynamic stravaActivityDetail = ( await stravaResponse.Content.ReadAsStringAsync() ).JsonDeserialize();
if ( string.Compare( garminActivityDescription.ToString(), stravaActivityDetail.description ) != 0 )
updateDescription = $"&description={HttpUtility.UrlEncode( garminActivityDescription.ToString() )}";
}
if ( updateName != "" || updateDescription != "" )
{
stravaResponse = await stravaHttpClient.PutAsync(
$"/api/v3/activities/{stravaActivity.id}?" +
$"&access_token={stravaAccessToken}" +
$"{updateName}" +
$"{updateDescription}", null );
WriteLine( stravaResponse.StatusCode != HttpStatusCode.OK ?
$"\t! Error updating Strava activity {stravaResponse.StatusCode}!" :
"\tStrava activity updated OK" );
checkStravaApiLimits();
}
}
}
void checkStravaApiLimits ()
{
if ( stravaResponse.Headers.TryGetValues( "X-Ratelimit-Usage", out var headersUsage ) &&
stravaResponse.Headers.TryGetValues( "X-Ratelimit-Limit", out var headersLimit ) )
{
stravaApiUsages = headersUsage.First().Split( ',' );
stravaApiLimits = headersLimit.First().Split( ',' );
if ( int.Parse( stravaApiUsages[0] ) >= int.Parse( stravaApiLimits[0] ) )
throw new( $"! 15-minute Strava API limit {stravaApiLimits[0]} has been exhausted." );
if ( int.Parse( stravaApiUsages[1] ) >= int.Parse( stravaApiLimits[1] ) )
throw new( $"! Daily Strava API limit {stravaApiLimits[1]} has been exhausted." );
}
}
}
finally
{
if ( stravaApiUsages != null && stravaApiLimits != null )
WriteLine( $"Strava API usage: 15-minute {stravaApiUsages[0]}/{stravaApiLimits[0]}, daily {stravaApiUsages[1]}/{stravaApiLimits[1]}" );
}
internal class Settings
{
/// <summary>
/// Strava Client ID obtained by registering your API application at https://www.strava.com/settings/api
/// </summary>
public int StravaClientId { get; set; }
/// <summary>
/// Strava Client Secret obtained by registering your API application at https://www.strava.com/settings/api
/// </summary>
public string StravaSecret { get; set; }
/// <summary>
/// Garmin account login email
/// </summary>
public string GarminLogin { get; set; }
/// <summary>
/// Garmin account password
/// </summary>
public string GarminPassword { get; set; }
/// <summary>
/// true to update Strava activity name when the Garmin activity name is different
/// </summary>
public bool UpdateName { get; set; }
/// <summary>
/// true to update Strava activity description when the Garmin activity description is not empty and different
/// if true than Strava activity description is also updated when <see cref="PropertiesToDescription"/> or <see cref="GearsToDescription"/> are set.
/// </summary>
public bool UpdateDescription { get; set; }
/// <summary>
/// true to append specified Garmin activity properties to the Strava activity description. Requires UpdateName = true.
/// The value of this configuration item is the list of properties separated by semicolons. Optional formatting string follows the colon after the property name.
/// Example: "VO2MaxValue;MaxHr;AvgStrideLength:0.0"
/// List of Garmin activity properties: https://github.com/sealbro/dotnet.garmin.connect/blob/main/Garmin.Connect/Models/GarminActivity.cs
/// Custom numeric format strings: https://docs.microsoft.com/en-us/dotnet/standard/base-types/custom-numeric-format-strings
/// </summary>
public string PropertiesToDescription { get; set; }
/// <summary>
/// true to append used gears to the description. Requires UpdateName = true.
/// </summary>
public bool GearsToDescription { get; set; }
/// <summary>
/// true to update Strava athlete weight from Garmin.
/// </summary>
public bool UpdateWeight { get; set; }
/// <summary>
/// Update activities that have taken place after a certain date.
/// If this or the following property is missing in the configuration file today is used.
/// </summary>
public DateTime DateAfter { get; set; }
/// <summary>
/// Update activities that have taken place before a certain date.
/// If this or the previous property is missing in the configuration file tomorrow is used.
/// </summary>
public DateTime DateBefore { get; set; }
}
internal static class Extensions
{
public static dynamic JsonDeserialize ( this string json ) => JsonConvert.DeserializeObject<ExpandoObject>( json, new ExpandoObjectConverter() );
public static List<ExpandoObject> JsonDeserializeList ( this string json ) => JsonConvert.DeserializeObject<List<ExpandoObject>>( json, new ExpandoObjectConverter() );
public static double DateTimeToUnixTimestamp ( this DateTime dateTime ) => ( TimeZoneInfo.ConvertTimeToUtc( dateTime ) - DateTime.UnixEpoch ).TotalSeconds;
}