-
-
Notifications
You must be signed in to change notification settings - Fork 4
Add collector for Wakefield Council #111
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
moley-bot
wants to merge
6
commits into
main
Choose a base branch
from
collector/WakefieldCouncil-issue-86-1768213716
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
6 commits
Select commit
Hold shift + click to select a range
4c62dba
Add collector for WakefieldCouncil
916a40d
Auto-format code with dotnet format
9c5ac32
Refactor WakefieldCouncil collector based on PR review feedback
github-actions[bot] f60f979
Change WakefieldCouncil test postcode to WF5 0AF
github-actions[bot] 4a22c7d
Fix WakefieldCouncil PR review comments
github-actions[bot] cd23189
Auto-format code with dotnet format
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
233 changes: 233 additions & 0 deletions
233
BinDays.Api.Collectors/Collectors/Councils/WakefieldCouncil.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,233 @@ | ||
| 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; | ||
| using System.Web; | ||
|
|
||
| /// <summary> | ||
| /// Collector implementation for Wakefield Council. | ||
| /// </summary> | ||
| internal sealed partial class WakefieldCouncil : GovUkCollectorBase, ICollector | ||
| { | ||
| /// <inheritdoc/> | ||
| public string Name => "Wakefield Council"; | ||
|
|
||
| /// <inheritdoc/> | ||
| public Uri WebsiteUrl => new("https://www.wakefield.gov.uk/"); | ||
|
|
||
| /// <inheritdoc/> | ||
| public override string GovUkId => "wakefield"; | ||
|
|
||
| /// <summary> | ||
| /// The list of bin types for this collector. | ||
| /// </summary> | ||
| private readonly IReadOnlyCollection<Bin> _binTypes = [ | ||
| new() | ||
| { | ||
| Name = "General Waste", | ||
| Colour = BinColour.Green, | ||
| Keys = [ "Household waste", "General waste", "Refuse" ], | ||
| }, | ||
| new() | ||
| { | ||
| Name = "Recycling", | ||
| Colour = BinColour.Brown, | ||
| Keys = [ "Mixed recycling", "Recycling" ], | ||
| }, | ||
| new() | ||
| { | ||
| Name = "Garden Waste", | ||
| Colour = BinColour.Brown, | ||
| Keys = [ "Garden waste" ], | ||
| }, | ||
| ]; | ||
|
|
||
| /// <summary> | ||
| /// Regex for the addresses from the address picker links. | ||
| /// </summary> | ||
| [GeneratedRegex(@"<a[^>]*?href=""[^""]*?where-i-live\?uprn=(?<uprn>[^&""]+)(?:&a=(?<property>[^&""]+))?[^""]*""[^>]*>(?<label>[^<]+)<\/a>")] | ||
| private static partial Regex AddressRegex(); | ||
|
|
||
| /// <summary> | ||
| /// Regex for the bin day panels on the property page. | ||
| /// </summary> | ||
| [GeneratedRegex(@"<div class=""u-mb-4""><strong>(?<service>[^<]+)<\/strong><\/div>[\s\S]*?Next collection - (?<next>[^<]+)<\/div>[\s\S]*?<ul class=""u-mt-4"">(?<future>[\s\S]*?)<\/ul>")] | ||
| private static partial Regex BinDaysRegex(); | ||
|
|
||
| /// <summary> | ||
| /// Regex for dates within the bin day panels. | ||
| /// </summary> | ||
| [GeneratedRegex(@"(?<date>[A-Za-z]+,\s+\d{1,2}\s+[A-Za-z]+\s+\d{4})")] | ||
| private static partial Regex DateRegex(); | ||
|
|
||
| /// <inheritdoc/> | ||
| public GetAddressesResponse GetAddresses(string postcode, ClientSideResponse? clientSideResponse) | ||
| { | ||
| // Prepare client-side request for base page (sets affinity cookies) | ||
| if (clientSideResponse == null) | ||
| { | ||
| var clientSideRequest = new ClientSideRequest | ||
| { | ||
| RequestId = 1, | ||
| Url = "https://www.wakefield.gov.uk/where-i-live", | ||
| Method = "GET", | ||
| Headers = new() | ||
| { | ||
| { "user-agent", Constants.UserAgent }, | ||
| }, | ||
| }; | ||
|
|
||
| return new GetAddressesResponse | ||
| { | ||
| NextClientSideRequest = clientSideRequest | ||
| }; | ||
| } | ||
| // Prepare client-side request for address list | ||
| else if (clientSideResponse.RequestId == 1) | ||
| { | ||
| var requestCookies = ProcessingUtilities.ParseSetCookieHeaderForRequestCookie( | ||
| clientSideResponse.Headers["set-cookie"] | ||
| ); | ||
|
|
||
| var clientSideRequest = new ClientSideRequest | ||
BadgerHobbs marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| { | ||
| RequestId = 2, | ||
| Url = $"https://www.wakefield.gov.uk/pick-your-address?where-i-live={postcode}", | ||
| Method = "GET", | ||
| Headers = new() | ||
| { | ||
| { "user-agent", Constants.UserAgent }, | ||
| { "cookie", requestCookies }, | ||
| }, | ||
| }; | ||
|
|
||
| return new GetAddressesResponse | ||
| { | ||
| NextClientSideRequest = clientSideRequest | ||
| }; | ||
| } | ||
| // 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>(); | ||
BadgerHobbs marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| foreach (Match rawAddress in rawAddresses) | ||
BadgerHobbs marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| { | ||
| var uprn = HttpUtility.UrlDecode(rawAddress.Groups["uprn"].Value).Trim(); | ||
|
|
||
| string? property; | ||
| if (rawAddress.Groups["property"].Success) | ||
| { | ||
| property = HttpUtility.UrlDecode(rawAddress.Groups["property"].Value).Trim(); | ||
| } | ||
| else | ||
| { | ||
| property = rawAddress.Groups["label"].Value.Trim(); | ||
| } | ||
|
|
||
| var address = new Address | ||
| { | ||
| Property = property, | ||
| Postcode = postcode, | ||
| Uid = uprn, | ||
| }; | ||
|
|
||
| addresses.Add(address); | ||
| } | ||
|
|
||
| return new GetAddressesResponse | ||
| { | ||
| Addresses = [.. addresses], | ||
| }; | ||
| } | ||
|
|
||
| throw new InvalidOperationException("Invalid client-side request."); | ||
| } | ||
|
|
||
| /// <inheritdoc/> | ||
| public GetBinDaysResponse GetBinDays(Address address, ClientSideResponse? clientSideResponse) | ||
| { | ||
| // Prepare client-side request for bin collections page | ||
| if (clientSideResponse == null) | ||
| { | ||
| var clientSideRequest = new ClientSideRequest | ||
| { | ||
| RequestId = 1, | ||
| Url = $"https://www.wakefield.gov.uk/where-i-live?uprn={address.Uid}&a={Uri.EscapeDataString(address.Property!)}", | ||
| Method = "GET", | ||
| Headers = new() | ||
| { | ||
| { "user-agent", Constants.UserAgent }, | ||
| }, | ||
| }; | ||
|
|
||
| return new GetBinDaysResponse | ||
| { | ||
| NextClientSideRequest = clientSideRequest | ||
| }; | ||
| } | ||
| // Process bin days from response | ||
| else if (clientSideResponse.RequestId == 1) | ||
| { | ||
| var rawBinDays = BinDaysRegex().Matches(clientSideResponse.Content)!; | ||
|
|
||
| // Iterate through each bin day, and create a new bin day object | ||
| var binDays = new List<BinDay>(); | ||
BadgerHobbs marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| foreach (Match rawBinDay in rawBinDays) | ||
| { | ||
| var service = rawBinDay.Groups["service"].Value.Trim(); | ||
| var nextCollection = rawBinDay.Groups["next"].Value.Trim(); | ||
BadgerHobbs marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| var futureCollections = rawBinDay.Groups["future"].Value.Trim(); | ||
|
|
||
| var matchingBins = ProcessingUtilities.GetMatchingBins(_binTypes, service); | ||
| var collectionDates = new HashSet<DateOnly>(); | ||
|
|
||
| if (!nextCollection.Contains("n/a", StringComparison.OrdinalIgnoreCase)) | ||
| { | ||
| var date = DateOnly.ParseExact( | ||
| nextCollection, | ||
| "dddd, d MMMM yyyy", | ||
| CultureInfo.InvariantCulture, | ||
| DateTimeStyles.None | ||
| ); | ||
| collectionDates.Add(date); | ||
| } | ||
|
|
||
| foreach (Match dateMatch in DateRegex().Matches(futureCollections)) | ||
| { | ||
| var date = DateOnly.ParseExact( | ||
| dateMatch.Groups["date"].Value, | ||
| "dddd, d MMMM yyyy", | ||
| CultureInfo.InvariantCulture, | ||
| DateTimeStyles.None | ||
| ); | ||
| collectionDates.Add(date); | ||
| } | ||
|
|
||
| foreach (var collectionDate in collectionDates) | ||
| { | ||
| binDays.Add(new BinDay | ||
| { | ||
| Address = address, | ||
| Date = collectionDate, | ||
| Bins = matchingBins, | ||
| }); | ||
| } | ||
| } | ||
|
|
||
| return new GetBinDaysResponse | ||
| { | ||
| BinDays = ProcessingUtilities.ProcessBinDays(binDays), | ||
| }; | ||
| } | ||
|
|
||
| throw new InvalidOperationException("Invalid client-side request."); | ||
| } | ||
| } | ||
36 changes: 36 additions & 0 deletions
36
BinDays.Api.IntegrationTests/Collectors/Councils/WakefieldCouncilTests.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 WakefieldCouncilTests | ||
| { | ||
| private readonly IntegrationTestClient _client; | ||
| private static readonly ICollector _collector = new WakefieldCouncil(); | ||
| private readonly CollectorService _collectorService = new([_collector]); | ||
| private readonly ITestOutputHelper _outputHelper; | ||
|
|
||
| public WakefieldCouncilTests(ITestOutputHelper outputHelper) | ||
| { | ||
| _outputHelper = outputHelper; | ||
| _client = new IntegrationTestClient(outputHelper); | ||
| } | ||
|
|
||
| [Theory] | ||
| [InlineData("WF5 0AF")] | ||
| 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.