blob: 43debba60ba6aacf912775c19ac9fa56884deed7 (
plain)
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
|
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);
var metres = arrival.Metres == "--" ? 0 : int.Parse(arrival.Metres);
return new CorunaEstimate
(
r.RouteId.ToString(),
minutes,
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);
|