-
-
Notifications
You must be signed in to change notification settings - Fork 4
Add collector for Cheshire East Council #95
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
BadgerHobbs
wants to merge
2
commits into
main
Choose a base branch
from
collector/CheshireEastCouncil-issue-83-1767482447
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
244 changes: 244 additions & 0 deletions
244
BinDays.Api.Collectors/Collectors/Councils/CheshireEastCouncil.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,244 @@ | ||
| namespace BinDays.Api.Collectors.Collectors.Councils; | ||
|
|
||
| using BinDays.Api.Collectors.Collectors.Vendors; | ||
| using BinDays.Api.Collectors.Models; | ||
| using BinDays.Api.Collectors.Utilities; | ||
| using System; | ||
| using System.Collections.Generic; | ||
| using System.Globalization; | ||
| using System.Text.RegularExpressions; | ||
|
|
||
| /// <summary> | ||
| /// Collector implementation for Cheshire East Council. | ||
| /// </summary> | ||
| internal sealed partial class CheshireEastCouncil : GovUkCollectorBase, ICollector | ||
| { | ||
| /// <inheritdoc/> | ||
| public string Name => "Cheshire East Council"; | ||
|
|
||
| /// <inheritdoc/> | ||
| public Uri WebsiteUrl => new("https://online.cheshireeast.gov.uk/"); | ||
|
|
||
| /// <inheritdoc/> | ||
| public override string GovUkId => "cheshire-east"; | ||
|
|
||
| /// <summary> | ||
| /// The list of bin types for this collector. | ||
| /// </summary> | ||
| private readonly IReadOnlyCollection<Bin> _binTypes = | ||
| [ | ||
| new() | ||
| { | ||
| Name = "General waste", | ||
| Colour = BinColour.Black, | ||
| Keys = [ "General Waste" ], | ||
| Type = BinType.Bin, | ||
| }, | ||
| new() | ||
| { | ||
| Name = "Recycling", | ||
| Colour = BinColour.Grey, | ||
| Keys = [ "Mixed Recycling" ], | ||
| Type = BinType.Bin, | ||
| }, | ||
| new() | ||
| { | ||
| Name = "Garden waste", | ||
| Colour = BinColour.Green, | ||
| Keys = [ "Garden Waste" ], | ||
| Type = BinType.Bin, | ||
| }, | ||
| ]; | ||
|
|
||
| /// <summary> | ||
| /// Regex for parsing addresses from the search response. | ||
| /// </summary> | ||
| [GeneratedRegex(@"<a class=""select-csv-address""[^>]*data-uprn=""(?<uprn>[^""]+)""[^>]*>(?<address>[^<]+)</a>", RegexOptions.IgnoreCase)] | ||
| private static partial Regex AddressRegex(); | ||
|
|
||
| /// <summary> | ||
| /// Regex for parsing bin day rows from the job list. | ||
| /// </summary> | ||
| [GeneratedRegex(@"BartecSimplifiedJobList_(?<index>\d+)__Name""[^>]*value=""(?<name>[^""]+)"" .*?BartecSimplifiedJobList_\k<index>__ScheduledStart""[^>]*value=""(?<date>[^""]+)""", RegexOptions.Singleline)] | ||
| private static partial Regex BinDayRegex(); | ||
|
|
||
| /// <inheritdoc/> | ||
| public GetAddressesResponse GetAddresses(string postcode, ClientSideResponse? clientSideResponse) | ||
| { | ||
| // Prepare client-side request for getting session cookies | ||
| if (clientSideResponse == null) | ||
| { | ||
| var clientSideRequest = new ClientSideRequest | ||
| { | ||
| RequestId = 1, | ||
| Url = "https://online.cheshireeast.gov.uk/MyCollectionDay/", | ||
| Method = "GET", | ||
| Headers = new() | ||
| { | ||
| { "User-Agent", Constants.UserAgent }, | ||
| }, | ||
| }; | ||
BadgerHobbs marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| var getAddressesResponse = new GetAddressesResponse | ||
| { | ||
| NextClientSideRequest = clientSideRequest, | ||
| }; | ||
BadgerHobbs marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| return getAddressesResponse; | ||
| } | ||
| // Prepare client-side request for getting addresses | ||
| else if (clientSideResponse.RequestId == 1) | ||
| { | ||
| var setCookie = clientSideResponse.Headers["set-cookie"]; | ||
| var requestCookies = ProcessingUtilities.ParseSetCookieHeaderForRequestCookie(setCookie); | ||
| var requestUrl = $"https://online.cheshireeast.gov.uk/MyCollectionDay/SearchByAjax/Search?postcode={postcode}&propertyname="; | ||
|
|
||
| var clientSideRequest = new ClientSideRequest | ||
| { | ||
| RequestId = 2, | ||
| Url = requestUrl, | ||
| Method = "GET", | ||
| Headers = new() | ||
| { | ||
| { "User-Agent", Constants.UserAgent }, | ||
| { "x-requested-with", "XMLHttpRequest" }, | ||
| { "cookie", requestCookies }, | ||
| }, | ||
| }; | ||
|
|
||
| var getAddressesResponse = new GetAddressesResponse | ||
| { | ||
| NextClientSideRequest = clientSideRequest, | ||
| }; | ||
|
|
||
| return getAddressesResponse; | ||
| } | ||
| // Process addresses from response | ||
| else if (clientSideResponse.RequestId == 2) | ||
| { | ||
| var rawAddresses = AddressRegex().Matches(clientSideResponse.Content)!; | ||
|
|
||
| // Iterate through each address, and create a new address object | ||
| var addresses = new List<Address>(); | ||
| foreach (Match rawAddress in rawAddresses) | ||
| { | ||
| var addressText = rawAddress.Groups["address"].Value.Trim(); | ||
|
|
||
| var address = new Address | ||
| { | ||
| Property = addressText, | ||
| Postcode = postcode, | ||
| Uid = rawAddress.Groups["uprn"].Value, | ||
| }; | ||
BadgerHobbs marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| addresses.Add(address); | ||
| } | ||
|
|
||
| var getAddressesResponse = new GetAddressesResponse | ||
| { | ||
| Addresses = [.. addresses], | ||
| }; | ||
|
|
||
| return getAddressesResponse; | ||
| } | ||
|
|
||
| // Throw exception for invalid request | ||
| throw new InvalidOperationException("Invalid client-side request."); | ||
| } | ||
BadgerHobbs marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| /// <inheritdoc/> | ||
| public GetBinDaysResponse GetBinDays(Address address, ClientSideResponse? clientSideResponse) | ||
| { | ||
| // Prepare client-side request for getting session cookies | ||
| if (clientSideResponse == null) | ||
| { | ||
| var clientSideRequest = new ClientSideRequest | ||
| { | ||
| RequestId = 1, | ||
| Url = "https://online.cheshireeast.gov.uk/MyCollectionDay/", | ||
| Method = "GET", | ||
| Headers = new() | ||
| { | ||
| { "User-Agent", Constants.UserAgent }, | ||
| }, | ||
| }; | ||
|
|
||
| var getBinDaysResponse = new GetBinDaysResponse | ||
| { | ||
| NextClientSideRequest = clientSideRequest, | ||
| }; | ||
|
|
||
| return getBinDaysResponse; | ||
| } | ||
| // Prepare client-side request for getting bin days | ||
| else if (clientSideResponse.RequestId == 1) | ||
| { | ||
| // Build the full address string for the API request | ||
| var onelineAddress = $"{address.Property}, {address.Postcode}"; | ||
|
|
||
| var setCookie = clientSideResponse.Headers["set-cookie"]; | ||
| var requestCookies = ProcessingUtilities.ParseSetCookieHeaderForRequestCookie(setCookie); | ||
| var requestUrl = $"https://online.cheshireeast.gov.uk/MyCollectionDay/SearchByAjax/GetBartecJobList?uprn={address.Uid}&onelineaddress={onelineAddress}"; | ||
|
|
||
| var clientSideRequest = new ClientSideRequest | ||
| { | ||
| RequestId = 2, | ||
| Url = requestUrl, | ||
| Method = "GET", | ||
| Headers = new() | ||
| { | ||
| { "User-Agent", Constants.UserAgent }, | ||
| { "x-requested-with", "XMLHttpRequest" }, | ||
| { "cookie", requestCookies }, | ||
| }, | ||
| }; | ||
|
|
||
| var getBinDaysResponse = new GetBinDaysResponse | ||
| { | ||
| NextClientSideRequest = clientSideRequest, | ||
| }; | ||
|
|
||
| return getBinDaysResponse; | ||
| } | ||
| // Process bin days from response | ||
| else if (clientSideResponse.RequestId == 2) | ||
| { | ||
| var rawBinDays = BinDayRegex().Matches(clientSideResponse.Content)!; | ||
|
|
||
| // Iterate through each bin day, and create a new bin day object | ||
| var binDays = new List<BinDay>(); | ||
| foreach (Match rawBinDay in rawBinDays) | ||
| { | ||
| var binTypeStr = rawBinDay.Groups["name"].Value.Trim(); | ||
| var dateStr = rawBinDay.Groups["date"].Value.Trim(); | ||
|
|
||
| // Parse date string (e.g. "13/01/2026 07:00:00") | ||
| var dateTime = DateTime.ParseExact( | ||
| dateStr, | ||
| "dd/MM/yyyy HH:mm:ss", | ||
| CultureInfo.InvariantCulture, | ||
| DateTimeStyles.None | ||
| ); | ||
|
|
||
| var binDay = new BinDay | ||
| { | ||
| Date = DateOnly.FromDateTime(dateTime), | ||
| Address = address, | ||
| Bins = ProcessingUtilities.GetMatchingBins(_binTypes, binTypeStr), | ||
| }; | ||
BadgerHobbs marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| binDays.Add(binDay); | ||
| } | ||
|
|
||
| var getBinDaysResponse = new GetBinDaysResponse | ||
| { | ||
| BinDays = ProcessingUtilities.ProcessBinDays(binDays), | ||
| }; | ||
|
|
||
| return getBinDaysResponse; | ||
| } | ||
|
|
||
| // Throw exception for invalid request | ||
| throw new InvalidOperationException("Invalid client-side request."); | ||
| } | ||
| } | ||
36 changes: 36 additions & 0 deletions
36
BinDays.Api.IntegrationTests/Collectors/Councils/CheshireEastCouncilTests.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,36 @@ | ||
| namespace BinDays.Api.IntegrationTests.Collectors.Councils; | ||
|
|
||
| using BinDays.Api.Collectors.Collectors; | ||
| using BinDays.Api.Collectors.Collectors.Councils; | ||
| using BinDays.Api.Collectors.Services; | ||
| using BinDays.Api.IntegrationTests.Helpers; | ||
| using System.Threading.Tasks; | ||
| using Xunit; | ||
| using Xunit.Abstractions; | ||
|
|
||
| public class CheshireEastCouncilTests | ||
| { | ||
| private readonly IntegrationTestClient _client; | ||
| private static readonly ICollector _collector = new CheshireEastCouncil(); | ||
| private readonly CollectorService _collectorService = new([_collector]); | ||
| private readonly ITestOutputHelper _outputHelper; | ||
|
|
||
| public CheshireEastCouncilTests(ITestOutputHelper outputHelper) | ||
| { | ||
| _outputHelper = outputHelper; | ||
| _client = new IntegrationTestClient(outputHelper); | ||
| } | ||
BadgerHobbs marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| [Theory] | ||
| [InlineData("CW1 2AY")] | ||
| public async Task GetBinDaysTest(string postcode) | ||
| { | ||
| await TestSteps.EndToEnd( | ||
| _client, | ||
| _collectorService, | ||
| _collector, | ||
| postcode, | ||
| _outputHelper | ||
| ); | ||
| } | ||
| } | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.