44 lines
1.4 KiB
C#
44 lines
1.4 KiB
C#
using BinaryDad.Extensions;
|
|
using Newtonsoft.Json;
|
|
using System.Text.RegularExpressions;
|
|
|
|
namespace BinaryDad.AacpsBusAlert.Services
|
|
{
|
|
public class HtmlScrapeBusRouteService : IBusRouteService
|
|
{
|
|
private readonly HttpClient httpClient;
|
|
|
|
public HtmlScrapeBusRouteService(IHttpClientFactory httpClientFactory)
|
|
{
|
|
httpClient = httpClientFactory.CreateClient();
|
|
}
|
|
|
|
public async Task<ICollection<BusRoute>> GetBusRoutesAsync()
|
|
{
|
|
var url = "https://busstops.aacps.org/public/BusRouteIssues.aspx";
|
|
var content = await httpClient.GetStringAsync(url);
|
|
|
|
var dataArrayMatch = Regex.Match(content, "var dataArray \\= (.*)\\;");
|
|
|
|
if (dataArrayMatch.Success && dataArrayMatch.Groups.Count == 2)
|
|
{
|
|
var dataArray = dataArrayMatch.Groups[1].Value;
|
|
|
|
var parsedArray = JsonConvert.DeserializeObject<string[][]>(dataArray);
|
|
|
|
return parsedArray
|
|
.Select(r => new BusRoute
|
|
{
|
|
BusNumber = r[0].To<int>(),
|
|
SubBusNumber = r[1].To<int>(),
|
|
Schools = r[2],
|
|
Schedules = r[3],
|
|
Impact = r[4]
|
|
})
|
|
.ToList();
|
|
}
|
|
|
|
throw new ApplicationException("Unable to parse bus route page");
|
|
}
|
|
}
|
|
} |