-
-
Notifications
You must be signed in to change notification settings - Fork 4
Add collector for Stockport Metropolitan Borough Council #121
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
1
commit into
main
Choose a base branch
from
collector/StockportMetropolitanBoroughCouncil-issue-98-1768818548
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.
+419
−0
Open
Changes from all commits
Commits
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
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
387 changes: 387 additions & 0 deletions
387
BinDays.Api.Collectors/Collectors/Councils/StockportMetropolitanBoroughCouncil.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,387 @@ | ||
| 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.Linq; | ||
| using System.Text.RegularExpressions; | ||
|
|
||
| /// <summary> | ||
| /// Collector implementation for Stockport Metropolitan Borough Council. | ||
| /// </summary> | ||
| internal sealed partial class StockportMetropolitanBoroughCouncil : GovUkCollectorBase, ICollector | ||
| { | ||
| /// <inheritdoc/> | ||
| public string Name => "Stockport Metropolitan Borough Council"; | ||
|
|
||
| /// <inheritdoc/> | ||
| public Uri WebsiteUrl => new("https://www.stockport.gov.uk/"); | ||
|
|
||
| /// <inheritdoc/> | ||
| public override string GovUkId => "stockport"; | ||
|
|
||
| /// <summary> | ||
| /// The list of bin types for this collector. | ||
| /// </summary> | ||
| private readonly IReadOnlyCollection<Bin> _binTypes = | ||
| [ | ||
| new() | ||
| { | ||
| Name = "General Waste", | ||
| Colour = BinColour.Black, | ||
BadgerHobbs marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| Keys = [ "Black" ], | ||
| }, | ||
| new() | ||
| { | ||
| Name = "Garden and Food Waste", | ||
| Colour = BinColour.Green, | ||
| Keys = [ "Green" ], | ||
| }, | ||
| new() | ||
| { | ||
| Name = "Waste Paper Recycling", | ||
| Colour = BinColour.Blue, | ||
| Keys = [ "Blue" ], | ||
| }, | ||
| new() | ||
| { | ||
| Name = "Glass, Aluminium and Plastic Recycling", | ||
| Colour = BinColour.Brown, | ||
| Keys = [ "Brown" ], | ||
| }, | ||
| ]; | ||
|
|
||
| /// <summary> | ||
| /// Regex for extracting the request verification token. | ||
| /// </summary> | ||
| [GeneratedRegex(@"__RequestVerificationToken"" type=""hidden"" value=""(?<token>[^""]+)""")] | ||
| private static partial Regex TokenRegex(); | ||
|
|
||
| /// <summary> | ||
| /// Regex for parsing addresses from the select options. | ||
| /// </summary> | ||
| [GeneratedRegex(@"<option value=""(?<uid>[^""]+)"">(?<address>[^<]+)</option>")] | ||
| private static partial Regex AddressRegex(); | ||
|
|
||
| /// <summary> | ||
| /// Regex for extracting the bin collection URL. | ||
| /// </summary> | ||
| [GeneratedRegex(@"href=['""](?<url>https://myaccount\.stockport\.gov\.uk/bin-collections/show/[^'""]+)['""]", RegexOptions.IgnoreCase | RegexOptions.Singleline)] | ||
| private static partial Regex BinCollectionsUrlRegex(); | ||
|
|
||
| /// <summary> | ||
| /// Regex for parsing bin names, descriptions, and dates. | ||
| /// </summary> | ||
| [GeneratedRegex(@"service-item[^>]*>\s*<img[^>]*>\s*<div>\s*<h3>(?<name>[^<]+)</h3>.*?class=""sub-title"">(?<description>[^<]+)</p>\s*<p>\s*(?<date>[^<]+)</p>", RegexOptions.Singleline | RegexOptions.IgnoreCase)] | ||
| private static partial Regex BinDaysRegex(); | ||
|
|
||
| /// <inheritdoc/> | ||
| public GetAddressesResponse GetAddresses(string postcode, ClientSideResponse? clientSideResponse) | ||
| { | ||
| // Use helper for initial form steps (RequestId null, 1, 2, 3) | ||
| if (clientSideResponse == null || clientSideResponse.RequestId <= 3) | ||
| { | ||
| var clientSideRequest = HandleInitialFormSteps(clientSideResponse, postcode, 4); | ||
|
|
||
| if (clientSideRequest != null) | ||
| { | ||
| return new GetAddressesResponse | ||
| { | ||
| NextClientSideRequest = clientSideRequest, | ||
| }; | ||
| } | ||
| } | ||
| // Process addresses from response | ||
| else if (clientSideResponse.RequestId == 4) | ||
| { | ||
| // Get addresses from response | ||
| 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 uid = rawAddress.Groups["uid"].Value.Trim(); | ||
|
|
||
| if (string.IsNullOrWhiteSpace(uid)) | ||
| { | ||
| continue; | ||
| } | ||
|
|
||
| var address = new Address | ||
| { | ||
| Property = rawAddress.Groups["address"].Value.Trim(), | ||
| Postcode = postcode, | ||
| Uid = uid, | ||
| }; | ||
|
|
||
| addresses.Add(address); | ||
| } | ||
|
|
||
| return new GetAddressesResponse | ||
| { | ||
| Addresses = [.. addresses], | ||
| }; | ||
| } | ||
|
|
||
| // Throw exception for invalid request | ||
| throw new InvalidOperationException("Invalid client-side request."); | ||
| } | ||
|
|
||
| /// <inheritdoc/> | ||
| public GetBinDaysResponse GetBinDays(Address address, ClientSideResponse? clientSideResponse) | ||
BadgerHobbs marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| { | ||
| // Use helper for initial form steps (RequestId null, 1, 2, 3) | ||
| if (clientSideResponse == null || clientSideResponse.RequestId <= 3) | ||
| { | ||
| var clientSideRequest = HandleInitialFormSteps(clientSideResponse, address.Postcode!, 4); | ||
|
|
||
| if (clientSideRequest != null) | ||
| { | ||
| return new GetBinDaysResponse | ||
| { | ||
| NextClientSideRequest = clientSideRequest, | ||
| }; | ||
| } | ||
| } | ||
| // Prepare client-side request for posting the selected address | ||
| else if (clientSideResponse.RequestId == 4) | ||
| { | ||
| var token = TokenRegex().Match(clientSideResponse.Content).Groups["token"].Value; | ||
| var cookies = clientSideResponse.Options.Metadata["cookie"]; | ||
|
|
||
| var requestBody = ProcessingUtilities.ConvertDictionaryToFormData(new() | ||
| { | ||
| { "__RequestVerificationToken", token }, | ||
| { "yourAddress-postcode", address.Postcode! }, | ||
| { "yourAddress-address", address.Uid! }, | ||
| { "Path", "address" }, | ||
| }); | ||
|
|
||
| return new GetBinDaysResponse | ||
| { | ||
| NextClientSideRequest = new ClientSideRequest | ||
| { | ||
| RequestId = 5, | ||
| Url = "https://forms.stockport.gov.uk/bin-collections/address/automatic", | ||
| Method = "POST", | ||
| Headers = new() | ||
| { | ||
| { "User-Agent", Constants.UserAgent }, | ||
| { "Content-Type", "application/x-www-form-urlencoded" }, | ||
| { "Cookie", cookies }, | ||
| }, | ||
| Body = requestBody, | ||
| Options = new ClientSideOptions | ||
| { | ||
| Metadata = | ||
| { | ||
| { "cookie", cookies }, | ||
| }, | ||
| }, | ||
| }, | ||
| }; | ||
| } | ||
| // Prepare client-side request for getting the confirmation page | ||
| else if (clientSideResponse.RequestId == 5) | ||
| { | ||
| return new GetBinDaysResponse | ||
| { | ||
| NextClientSideRequest = new ClientSideRequest | ||
| { | ||
| RequestId = 6, | ||
| Url = "https://forms.stockport.gov.uk/bin-collections/bin-collections", | ||
| Method = "GET", | ||
| Headers = new() | ||
| { | ||
| { "User-Agent", Constants.UserAgent }, | ||
| { "Cookie", clientSideResponse.Options.Metadata["cookie"] }, | ||
| }, | ||
| }, | ||
| }; | ||
| } | ||
| // Prepare client-side request for getting the bin collections page | ||
| else if (clientSideResponse.RequestId == 6) | ||
| { | ||
| var binCollectionsUrl = BinCollectionsUrlRegex().Match(clientSideResponse.Content).Groups["url"].Value; | ||
|
|
||
| return new GetBinDaysResponse | ||
| { | ||
| NextClientSideRequest = new ClientSideRequest | ||
| { | ||
| RequestId = 7, | ||
| Url = binCollectionsUrl, | ||
| Method = "GET", | ||
| Headers = new() | ||
| { | ||
| { "User-Agent", Constants.UserAgent }, | ||
| }, | ||
| }, | ||
| }; | ||
| } | ||
| // Process bin days from response | ||
| else if (clientSideResponse.RequestId == 7) | ||
| { | ||
| // Get bin days from response | ||
| var rawBinDays = BinDaysRegex().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 serviceName = rawBinDay.Groups["name"].Value.Trim(); | ||
| var description = rawBinDay.Groups["description"].Value.Trim(); | ||
| var dateString = rawBinDay.Groups["date"].Value.Trim(); | ||
|
|
||
| var date = DateOnly.ParseExact( | ||
| dateString, | ||
| "dddd, d MMMM yyyy", | ||
| CultureInfo.InvariantCulture, | ||
| DateTimeStyles.None | ||
| ); | ||
|
|
||
| var matchedBins = ProcessingUtilities.GetMatchingBins(_binTypes, $"{serviceName} {description}"); | ||
|
|
||
| var binDay = new BinDay | ||
| { | ||
| Date = date, | ||
| Address = address, | ||
| Bins = matchedBins, | ||
| }; | ||
|
|
||
| binDays.Add(binDay); | ||
| } | ||
|
|
||
| return new GetBinDaysResponse | ||
| { | ||
| BinDays = ProcessingUtilities.ProcessBinDays(binDays), | ||
| }; | ||
| } | ||
|
|
||
| // Throw exception for invalid request | ||
| throw new InvalidOperationException("Invalid client-side request."); | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Helper method to handle the initial form steps to acquire session cookies and tokens. | ||
| /// </summary> | ||
| /// <param name="clientSideResponse">The client-side response from the previous request.</param> | ||
| /// <param name="postcode">The postcode to use for the form.</param> | ||
| /// <param name="nextRequestId">The next request ID to use.</param> | ||
| /// <returns>A client-side request for the next step, or null if we're past the initial steps.</returns> | ||
| private static ClientSideRequest? HandleInitialFormSteps( | ||
| ClientSideResponse? clientSideResponse, | ||
| string postcode, | ||
| int nextRequestId) | ||
| { | ||
| // Prepare client-side request for getting initial form cookies | ||
| if (clientSideResponse == null) | ||
| { | ||
| return new ClientSideRequest | ||
| { | ||
| RequestId = 1, | ||
| Url = "https://forms.stockport.gov.uk/bin-collections", | ||
| Method = "GET", | ||
| Headers = new() | ||
| { | ||
| { "User-Agent", Constants.UserAgent }, | ||
| }, | ||
| }; | ||
| } | ||
| // Prepare client-side request for getting the address form | ||
| else if (clientSideResponse.RequestId == 1) | ||
| { | ||
| var setCookieHeader = clientSideResponse.Headers["set-cookie"]; | ||
| var cookies = ProcessingUtilities.ParseSetCookieHeaderForRequestCookie(setCookieHeader!); | ||
|
|
||
| return new ClientSideRequest | ||
| { | ||
| RequestId = 2, | ||
| Url = "https://forms.stockport.gov.uk/bin-collections/address", | ||
| Method = "GET", | ||
| Headers = new() | ||
| { | ||
| { "User-Agent", Constants.UserAgent }, | ||
| { "Cookie", cookies }, | ||
| }, | ||
| Options = new ClientSideOptions | ||
| { | ||
| Metadata = | ||
| { | ||
| { "cookie", cookies }, | ||
| }, | ||
| }, | ||
| }; | ||
| } | ||
| // Prepare client-side request for posting the postcode | ||
| else if (clientSideResponse.RequestId == 2) | ||
| { | ||
| var token = TokenRegex().Match(clientSideResponse.Content).Groups["token"].Value; | ||
| var baseCookie = clientSideResponse.Options.Metadata["cookie"]; | ||
|
|
||
| var cookies = baseCookie; | ||
| if (clientSideResponse.Headers.TryGetValue("set-cookie", out var value)) | ||
| { | ||
| var tokenCookie = ProcessingUtilities.ParseSetCookieHeaderForRequestCookie(value!); | ||
| cookies = string.Join("; ", new[] { baseCookie, tokenCookie }.Where(x => !string.IsNullOrWhiteSpace(x))); | ||
| } | ||
|
|
||
| var requestBody = ProcessingUtilities.ConvertDictionaryToFormData(new() | ||
| { | ||
| { "__RequestVerificationToken", token }, | ||
| { "yourAddress-postcode", postcode }, | ||
| { "Path", "address" }, | ||
| }); | ||
|
|
||
| return new ClientSideRequest | ||
| { | ||
| RequestId = 3, | ||
| Url = "https://forms.stockport.gov.uk/bin-collections/address", | ||
| Method = "POST", | ||
| Headers = new() | ||
| { | ||
| { "User-Agent", Constants.UserAgent }, | ||
| { "Content-Type", "application/x-www-form-urlencoded" }, | ||
| { "Cookie", cookies }, | ||
| }, | ||
| Body = requestBody, | ||
| Options = new ClientSideOptions | ||
| { | ||
| Metadata = | ||
| { | ||
| { "cookie", cookies }, | ||
| }, | ||
| }, | ||
| }; | ||
| } | ||
| // Prepare client-side request for getting the addresses | ||
| else if (clientSideResponse.RequestId == 3) | ||
| { | ||
| var cookies = clientSideResponse.Options.Metadata["cookie"]; | ||
|
|
||
| return new ClientSideRequest | ||
| { | ||
| RequestId = nextRequestId, | ||
| Url = "https://forms.stockport.gov.uk/bin-collections/address/automatic", | ||
| Method = "GET", | ||
| Headers = new() | ||
| { | ||
| { "User-Agent", Constants.UserAgent }, | ||
| { "Cookie", cookies }, | ||
| }, | ||
| Options = new ClientSideOptions | ||
| { | ||
| Metadata = | ||
| { | ||
| { "cookie", cookies }, | ||
| }, | ||
| }, | ||
| }; | ||
| } | ||
|
|
||
| return null; | ||
| } | ||
| } | ||
Oops, something went wrong.
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.