Repository navigation
Expand file tree
/
Copy pathProgram.cs
More file actions
190 lines (148 loc) · 6.6 KB
/
Copy pathProgram.cs
File metadata and controls
190 lines (148 loc) · 6.6 KB
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
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using Newtonsoft.Json;
using RuntasticExportProcessor.Enums;
using RuntasticExportProcessor.Types;
namespace RuntasticExportProcessor
{
public static class Program
{
public static void Main(string[] args)
{
var folderPaths = GetFolderPaths(args);
var list = ReadDataFromFiles(folderPaths);
OutputDataToConsole(list);
CreateResultFiles(list, folderPaths.Result);
Console.WriteLine("Done!");
Console.ReadLine();
}
private static FolderPathsData GetFolderPaths(string[] args)
{
var rootPath = args.FirstOrDefault();
if (string.IsNullOrWhiteSpace(rootPath))
{
Console.Write("Enter the path to the folder with exported data (leave empty to use current folder): ");
rootPath = Console.ReadLine();
}
if (string.IsNullOrWhiteSpace(rootPath))
{
rootPath = Environment.CurrentDirectory;
}
Console.WriteLine($"Root folder path: {rootPath}");
var sportSessionsFolderPath = Path.Combine(rootPath, "Sport-sessions");
var gpsDataFolderPath = Path.Combine(sportSessionsFolderPath, "GPS-data");
var isGpsDataFolderExists = Directory.Exists(gpsDataFolderPath);
if (!isGpsDataFolderExists)
{
throw new ApplicationException("'Sport-sessions\\GPS-data' folder is not found.");
}
var resultFolderPath = Path.Combine(rootPath, $"_Processed {DateTime.Now:yyyy-MM-dd HH-mm-ss}");
Directory.CreateDirectory(resultFolderPath);
Console.WriteLine($"Result folder path: {resultFolderPath}");
Console.WriteLine();
return new FolderPathsData
{
Root = rootPath,
SportSessions = sportSessionsFolderPath,
GpsData = gpsDataFolderPath,
Result = resultFolderPath,
};
}
private static List<SportSessionData> ReadDataFromFiles(FolderPathsData folderPaths)
{
var sportSessionFilePaths = Directory.GetFiles(folderPaths.SportSessions, "*.json");
var results = new List<SportSessionData>();
foreach (var sportSessionFilePath in sportSessionFilePaths)
{
var rawJson = File.ReadAllText(sportSessionFilePath);
var parsedJson = JsonConvert.DeserializeObject<ParsedJsonData>(rawJson);
var sportSessionFileName = Path.GetFileName(sportSessionFilePath);
var gpsDataFileName = Path.ChangeExtension(sportSessionFileName, ".gpx");
var gpsDataFilePath = Path.Combine(folderPaths.GpsData, gpsDataFileName);
var hasGpsRoute = File.Exists(gpsDataFilePath);
var sportTypeId = Enum.Parse<ActivityTypeId>(parsedJson.sport_type_id);
var startTime = UnixTimeStampToDateTime(parsedJson.start_time / 1000.0);
var timeZone = parsedJson.start_time_timezone_offset / 60 / 60 / 1000;
var session = new SportSessionData
{
ParsedJson = parsedJson,
Json = rawJson,
GpsDataFilePath = gpsDataFilePath,
HasGpsData = hasGpsRoute,
ActivityTypeId = sportTypeId,
StartTime = startTime,
TimeZone = (int) timeZone,
};
results.Add(session);
}
return results;
}
private static void OutputDataToConsole(List<SportSessionData> list)
{
foreach (var item in list.OrderByDescending(x => x.StartTime))
{
var gpsMessage = string.Empty;
if (!item.HasGpsData)
{
gpsMessage = "(no GPS)";
}
Console.WriteLine($"{item.StartTime:yyyy-MM-dd HH:mm} {item.TimeZone:+0;-#}: {item.ActivityTypeId} {gpsMessage}");
}
Console.WriteLine();
}
private static void CreateResultFiles(List<SportSessionData> list, string resultFolderPath)
{
// list = dictionary[sportType][year]
var dictionary = list
.GroupBy(x => x.ActivityTypeId)
.ToDictionary(
g => g.Key,
g => g
.GroupBy(x => x.StartTime.Year)
.ToDictionary(
g2 => g2.Key,
g2 => g2.OrderBy(x => x.StartTime).ToList()));
foreach (var sportType in dictionary.Keys)
{
foreach (var year in dictionary[sportType].Keys)
{
var items = dictionary[sportType][year];
if (items.All(x => x.HasGpsData))
{
var resultFileContentBuilder = new StringBuilder();
foreach (var item in items)
{
var gpsDataFileContent = File.ReadAllText(item.GpsDataFilePath);
resultFileContentBuilder.Append(gpsDataFileContent);
}
var resultFileContent = Regex.Replace(resultFileContentBuilder.ToString(), "</trk>(.|\r|\n)+?<trk>", "</trk>\r\n<trk>");
var resultFileName = $"{year} {sportType}.gpx";
var resultPath = Path.Combine(resultFolderPath, resultFileName);
File.WriteAllText(resultPath, resultFileContent);
Console.WriteLine($"File created: {resultFileName}");
}
else if (items.All(x => !x.HasGpsData))
{
continue;
}
else
{
throw new Exception($"{sportType} sometimes does have GPS data and sometimes doesn't, wtf?");
}
}
}
Console.WriteLine();
}
private static DateTime UnixTimeStampToDateTime(double unixTimeStamp)
{
// Unix timestamp is seconds past epoch
var epochStart = new DateTime(1970, 1, 1, 0, 0, 0, 0, DateTimeKind.Utc);
var result = epochStart.AddSeconds(unixTimeStamp).ToLocalTime();
return result;
}
}
}