AacpsBusAlert/BinaryDad.AacpsBusAlert.Services/BusRouteService/HtmlScrapeBusRouteService.cs

44 lines
1.4 KiB
C#
Raw Permalink Normal View History

2022-09-14 20:26:19 +00:00
using BinaryDad.Extensions;
using Newtonsoft.Json;
using System.Text.RegularExpressions;
namespace BinaryDad.AacpsBusAlert.Services
{
2022-10-05 02:23:14 +00:00
public class HtmlScrapeBusRouteService : IBusRouteService
2022-09-14 20:26:19 +00:00
{
private readonly HttpClient httpClient;
2022-10-05 02:23:14 +00:00
public HtmlScrapeBusRouteService(IHttpClientFactory httpClientFactory)
2022-09-14 20:26:19 +00:00
{
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");
}
}
}