aboutsummaryrefslogtreecommitdiff
path: root/src/Enmarcha.Sources.Tussa
diff options
context:
space:
mode:
authorAriel Costas Guerrero <ariel@costas.dev>2025-12-29 00:41:52 +0100
committerAriel Costas Guerrero <ariel@costas.dev>2025-12-29 00:41:52 +0100
commita304c24b32c0327436bbd8c2853e60668e161b42 (patch)
tree08f65c05daca134cf4d2e4f779bd15d98fd66370 /src/Enmarcha.Sources.Tussa
parent120a3c6bddd0fb8d9fa05df4763596956554c025 (diff)
Rename a lot of stuff, add Santiago real time
Diffstat (limited to 'src/Enmarcha.Sources.Tussa')
-rw-r--r--src/Enmarcha.Sources.Tussa/Enmarcha.Sources.Tussa.csproj10
-rw-r--r--src/Enmarcha.Sources.Tussa/Response.cs34
-rw-r--r--src/Enmarcha.Sources.Tussa/SantiagoRealtimeEstimatesProvider.cs40
3 files changed, 84 insertions, 0 deletions
diff --git a/src/Enmarcha.Sources.Tussa/Enmarcha.Sources.Tussa.csproj b/src/Enmarcha.Sources.Tussa/Enmarcha.Sources.Tussa.csproj
new file mode 100644
index 0000000..0db72ca
--- /dev/null
+++ b/src/Enmarcha.Sources.Tussa/Enmarcha.Sources.Tussa.csproj
@@ -0,0 +1,10 @@
+<Project Sdk="Microsoft.NET.Sdk">
+
+ <PropertyGroup>
+ <TargetFramework>net10.0</TargetFramework>
+ <ImplicitUsings>enable</ImplicitUsings>
+ <Nullable>enable</Nullable>
+ <RootNamespace>Enmarcha.Sources.Tussa</RootNamespace>
+ </PropertyGroup>
+
+</Project>
diff --git a/src/Enmarcha.Sources.Tussa/Response.cs b/src/Enmarcha.Sources.Tussa/Response.cs
new file mode 100644
index 0000000..df941b9
--- /dev/null
+++ b/src/Enmarcha.Sources.Tussa/Response.cs
@@ -0,0 +1,34 @@
+using System.Text.Json.Serialization;
+
+namespace Enmarcha.Sources.Tussa;
+
+public class MaisbusResponse
+{
+ [JsonPropertyName("id")] public string Id { get; set; }
+ [JsonPropertyName("codigo")] public string Code { get; set; }
+ [JsonPropertyName("nombre")] public string Name { get; set; }
+ [JsonPropertyName("coordenadas")] public Coordinates Coordinates { get; set; }
+ [JsonPropertyName("lineas")] public Route[] Routes { get; set; }
+}
+
+public class Coordinates
+{
+ [JsonPropertyName("latitud")]
+ public double Latitude { get; set; }
+ [JsonPropertyName("longitud")]
+ public double Longitude { get; set; }
+}
+
+public class Route
+{
+ [JsonPropertyName("id")] public string Id { get; set; }
+ [JsonPropertyName("sinoptico")] public string Sinoptico { get; set; }
+ [JsonPropertyName("nombre")] public string Name { get; set; }
+ [JsonPropertyName("estilo")] public string Colour { get; set; }
+ /// <example>
+ /// 2025-12-28 23:57
+ /// </example>
+ [JsonPropertyName("proximoPaso")] public string NextArrival { get; set; }
+ [JsonPropertyName("minutosProximoPaso")] public int MinutesToArrive { get; set; }
+}
+
diff --git a/src/Enmarcha.Sources.Tussa/SantiagoRealtimeEstimatesProvider.cs b/src/Enmarcha.Sources.Tussa/SantiagoRealtimeEstimatesProvider.cs
new file mode 100644
index 0000000..7437a05
--- /dev/null
+++ b/src/Enmarcha.Sources.Tussa/SantiagoRealtimeEstimatesProvider.cs
@@ -0,0 +1,40 @@
+using System.Net.Http.Json;
+
+namespace Enmarcha.Sources.Tussa;
+
+public class SantiagoRealtimeEstimatesProvider
+{
+ private HttpClient _http;
+
+ public SantiagoRealtimeEstimatesProvider(HttpClient http)
+ {
+ _http = http;
+ }
+
+ public async Task<List<SantiagoEstimate>> GetEstimatesForStop(int stopId)
+ {
+ var url = GetRequestUrl(stopId.ToString());
+
+ var response = await _http.GetAsync(url);
+ var maisbusResponse = await response.Content.ReadFromJsonAsync<MaisbusResponse>();
+
+ if (maisbusResponse is null)
+ {
+ var responseString = await response.Content.ReadAsStringAsync();
+ throw new Exception("Error parsing maisbus response: " + responseString);
+ }
+
+ return maisbusResponse.Routes.Select(r => new SantiagoEstimate
+ (
+ r.Id.ToString(),
+ r.MinutesToArrive
+ )).OrderBy(a => a.Minutes).ToList();
+ }
+
+ private static string GetRequestUrl(string stopId)
+ {
+ return $"https://tussa.gal/maisbus/api/stop/{stopId}";
+ }
+}
+
+public record SantiagoEstimate(string RouteId, int Minutes);