diff options
| author | Ariel Costas Guerrero <ariel@costas.dev> | 2025-12-29 00:41:52 +0100 |
|---|---|---|
| committer | Ariel Costas Guerrero <ariel@costas.dev> | 2025-12-29 00:41:52 +0100 |
| commit | a304c24b32c0327436bbd8c2853e60668e161b42 (patch) | |
| tree | 08f65c05daca134cf4d2e4f779bd15d98fd66370 /src/Enmarcha.Sources.TranviasCoruna/CorunaRealtimeEstimatesProvider.cs | |
| parent | 120a3c6bddd0fb8d9fa05df4763596956554c025 (diff) | |
Rename a lot of stuff, add Santiago real time
Diffstat (limited to 'src/Enmarcha.Sources.TranviasCoruna/CorunaRealtimeEstimatesProvider.cs')
| -rw-r--r-- | src/Enmarcha.Sources.TranviasCoruna/CorunaRealtimeEstimatesProvider.cs | 50 |
1 files changed, 50 insertions, 0 deletions
diff --git a/src/Enmarcha.Sources.TranviasCoruna/CorunaRealtimeEstimatesProvider.cs b/src/Enmarcha.Sources.TranviasCoruna/CorunaRealtimeEstimatesProvider.cs new file mode 100644 index 0000000..70449ce --- /dev/null +++ b/src/Enmarcha.Sources.TranviasCoruna/CorunaRealtimeEstimatesProvider.cs @@ -0,0 +1,50 @@ +using System.Net.Http.Json; + +namespace Enmarcha.Sources.TranviasCoruna; + +public class CorunaRealtimeEstimatesProvider +{ + private HttpClient _http; + + public CorunaRealtimeEstimatesProvider(HttpClient http) + { + _http = http; + } + + public async Task<List<CorunaEstimate>> GetEstimatesForStop(int stopId) + { + var url = GetRequestUrl(stopId.ToString()); + + var response = await _http.GetAsync(url); + var queryitrResponse = await response.Content.ReadFromJsonAsync<QueryitrResponse>(); + + if (queryitrResponse is null) + { + var responseString = await response.Content.ReadAsStringAsync(); + throw new Exception("Error parsing queryitr_v3 response: " + responseString); + } + + return queryitrResponse.ArrivalInfo.Routes.SelectMany(r => + { + return r.Arrivals.Select(arrival => + { + var minutes = arrival.Minutes == "<1" ? 0 : int.Parse(arrival.Minutes); + + return new CorunaEstimate + ( + r.RouteId.ToString(), + minutes, + int.Parse(arrival.Metres), + arrival.VehicleNumber.ToString() + ); + }).ToList(); + }).OrderBy(a => a.Minutes).ToList(); + } + + private static string GetRequestUrl(string stopId) + { + return $"https://itranvias.com/queryitr_v3.php?&func=0&dato={stopId}"; + } +} + +public record CorunaEstimate(string RouteId, int Minutes, int Metres, string VehicleNumber); |
