-
-
Notifications
You must be signed in to change notification settings - Fork 4
Add collector for Mansfield #108
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
Merged
+277
−0
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
679b1a2
Add collector for Mansfield
github-actions[bot] 5e8d1e9
Auto-format code with dotnet format
actions-user ecff286
Address PR review comments for Mansfield collector
github-actions[bot] c77eff2
Address final PR review comments
github-actions[bot] 99d160d
Use direct header access with null-forgiving operator for set-cookie
github-actions[bot] 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
241 changes: 241 additions & 0 deletions
241
BinDays.Api.Collectors/Collectors/Councils/Mansfield.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,241 @@ | ||
| 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.Json; | ||
| using System.Text.RegularExpressions; | ||
|
|
||
| /// <summary> | ||
| /// Collector implementation for Mansfield. | ||
| /// </summary> | ||
| internal sealed partial class Mansfield : GovUkCollectorBase, ICollector | ||
| { | ||
| /// <inheritdoc/> | ||
| public string Name => "Mansfield"; | ||
|
|
||
| /// <inheritdoc/> | ||
| public Uri WebsiteUrl => new("https://www.mansfield.gov.uk/"); | ||
|
|
||
| /// <inheritdoc/> | ||
| public override string GovUkId => "mansfield"; | ||
|
|
||
| /// <summary> | ||
| /// The list of bin types for this collector. | ||
| /// </summary> | ||
| private readonly IReadOnlyCollection<Bin> _binTypes = [ | ||
| new() | ||
| { | ||
| Name = "General Waste", | ||
| Colour = BinColour.Green, | ||
| Keys = [ "General Waste Collection Service" ], | ||
| }, | ||
| new() | ||
| { | ||
| Name = "Recycling", | ||
| Colour = BinColour.Blue, | ||
| Keys = [ "Recycling Waste Collection Service" ], | ||
| }, | ||
| new() | ||
| { | ||
| Name = "Garden Waste", | ||
| Colour = BinColour.Brown, | ||
| Keys = [ "Garden Waste Collection Service" ], | ||
| }, | ||
| new() | ||
| { | ||
| Name = "Glass", | ||
| Colour = BinColour.Black, | ||
| Keys = [ "Glass Waste Collection Service" ], | ||
| }, | ||
| ]; | ||
|
|
||
| /// <summary> | ||
| /// The form URL for the collector. | ||
| /// </summary> | ||
| private const string _formUrl = "https://www.mansfield.gov.uk/xfp/form/1339"; | ||
BadgerHobbs marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| /// <summary> | ||
| /// Regex for the token from the form. | ||
| /// </summary> | ||
| [GeneratedRegex(@"name=""__token"" value=""(?<token>[^""]+)""")] | ||
| private static partial Regex TokenRegex(); | ||
|
|
||
| /// <summary> | ||
| /// Regex for the addresses from the data. | ||
| /// </summary> | ||
| [GeneratedRegex(@"<option\s+value=""(?<uid>\d+)""[^>]*>\s*(?<address>[^<]+?)\s*</option>")] | ||
| private static partial Regex AddressRegex(); | ||
|
|
||
| /// <inheritdoc/> | ||
| public GetAddressesResponse GetAddresses(string postcode, ClientSideResponse? clientSideResponse) | ||
| { | ||
| // Prepare client-side request for getting token | ||
| if (clientSideResponse == null) | ||
| { | ||
| var clientSideRequest = new ClientSideRequest | ||
| { | ||
| RequestId = 1, | ||
| Url = _formUrl, | ||
| Method = "GET", | ||
| Headers = new() { | ||
| {"user-agent", Constants.UserAgent}, | ||
| }, | ||
| }; | ||
|
|
||
| var getAddressesResponse = new GetAddressesResponse | ||
| { | ||
| NextClientSideRequest = clientSideRequest, | ||
| }; | ||
|
|
||
| return getAddressesResponse; | ||
| } | ||
| // Prepare client-side request for getting addresses | ||
| else if (clientSideResponse.RequestId == 1) | ||
| { | ||
| var token = TokenRegex().Match(clientSideResponse.Content).Groups["token"].Value; | ||
| var setCookieHeader = clientSideResponse.Headers["set-cookie"]!; | ||
| var requestCookies = ProcessingUtilities.ParseSetCookieHeaderForRequestCookie(setCookieHeader); | ||
|
|
||
| var requestBody = ProcessingUtilities.ConvertDictionaryToFormData(new() | ||
| { | ||
| {"__token", token}, | ||
| {"page", "2615"}, | ||
| {"locale", "en_GB"}, | ||
| {"injectedParams", "{\"formID\":\"1339\"}"}, | ||
| {"q3fc8e993e4e89b244317c1f13b6d65c0b0ef1ad2_0_0", postcode}, | ||
| {"callback", "{\"action\":\"ic\",\"element\":\"q3fc8e993e4e89b244317c1f13b6d65c0b0ef1ad2\",\"data\":0,\"tableRow\":-1}"}, | ||
|
Owner
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. the value being escaped json string isn't great for readability. |
||
| {"q177fee160e3d7694451f7d047342e9c0e3ce01c9", string.Empty}, | ||
| }); | ||
|
|
||
| var clientSideRequest = new ClientSideRequest | ||
| { | ||
| RequestId = 2, | ||
| Url = _formUrl, | ||
| Method = "POST", | ||
| Headers = new() { | ||
| {"user-agent", Constants.UserAgent}, | ||
| {"content-type", "application/x-www-form-urlencoded"}, | ||
| {"cookie", requestCookies}, | ||
| }, | ||
| Body = requestBody, | ||
| }; | ||
|
|
||
| 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) | ||
BadgerHobbs marked this conversation as resolved.
Show resolved
Hide resolved
BadgerHobbs marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| { | ||
| var uid = rawAddress.Groups["uid"].Value; | ||
|
|
||
| if (string.IsNullOrWhiteSpace(uid) || uid == "0") | ||
| { | ||
| continue; | ||
| } | ||
|
|
||
| var address = new Address | ||
| { | ||
| Property = rawAddress.Groups["address"].Value.Trim(), | ||
| Postcode = postcode, | ||
| Uid = uid, | ||
| }; | ||
|
|
||
| addresses.Add(address); | ||
| } | ||
|
|
||
| var getAddressesResponse = new GetAddressesResponse | ||
| { | ||
| Addresses = [.. addresses], | ||
| }; | ||
|
|
||
| return getAddressesResponse; | ||
| } | ||
|
|
||
| throw new InvalidOperationException("Invalid client-side request."); | ||
| } | ||
|
|
||
| /// <inheritdoc/> | ||
| public GetBinDaysResponse GetBinDays(Address address, ClientSideResponse? clientSideResponse) | ||
| { | ||
| // Prepare client-side request for getting bin days | ||
| if (clientSideResponse == null) | ||
| { | ||
| var fromDate = DateOnly.FromDateTime(DateTime.Now); | ||
| var toDate = fromDate.AddDays(364); | ||
BadgerHobbs marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| var requestUrl = $"https://portal.mansfield.gov.uk/mdcwhitespacewebservice/WhiteSpaceWS.asmx/GetCollectionByUPRNAndDatePlus?&apiKey=mDc-wN3-B0f-f4P&UPRN={address.Uid}&ColFromDate={fromDate:yyyy-MM-dd}&ColToDate={toDate:yyyy-MM-dd}"; | ||
|
|
||
| var clientSideRequest = new ClientSideRequest | ||
| { | ||
| RequestId = 1, | ||
| Url = requestUrl, | ||
| Method = "GET", | ||
| Headers = new() { | ||
| {"user-agent", Constants.UserAgent}, | ||
| }, | ||
| }; | ||
|
|
||
| var getBinDaysResponse = new GetBinDaysResponse | ||
| { | ||
| NextClientSideRequest = clientSideRequest, | ||
| }; | ||
|
|
||
| return getBinDaysResponse; | ||
| } | ||
| // Process bin days from response | ||
| else if (clientSideResponse.RequestId == 1) | ||
| { | ||
| using var jsonDoc = JsonDocument.Parse(clientSideResponse.Content); | ||
| var collections = jsonDoc.RootElement.GetProperty("Collections"); | ||
|
|
||
| // Iterate through each bin day, and create a new bin day object | ||
| var binDays = new List<BinDay>(); | ||
| foreach (var collection in collections.EnumerateArray()) | ||
BadgerHobbs marked this conversation as resolved.
Show resolved
Hide resolved
BadgerHobbs marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| { | ||
| var service = collection.GetProperty("Service").GetString()!; | ||
| var dateString = collection.GetProperty("Date").GetString()!; | ||
|
|
||
| var collectionDate = DateTime.ParseExact( | ||
| dateString, | ||
| "dd/MM/yyyy HH:mm:ss", | ||
| CultureInfo.InvariantCulture, | ||
| DateTimeStyles.None | ||
| ); | ||
BadgerHobbs marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| var matchedBinTypes = ProcessingUtilities.GetMatchingBins(_binTypes, service); | ||
|
|
||
| var binDay = new BinDay | ||
| { | ||
| Date = DateOnly.FromDateTime(collectionDate), | ||
| Address = address, | ||
| Bins = matchedBinTypes, | ||
| }; | ||
|
|
||
| binDays.Add(binDay); | ||
| } | ||
|
|
||
| var getBinDaysResponse = new GetBinDaysResponse | ||
| { | ||
| BinDays = ProcessingUtilities.ProcessBinDays(binDays), | ||
| }; | ||
|
|
||
| return getBinDaysResponse; | ||
| } | ||
|
|
||
| throw new InvalidOperationException("Invalid client-side request."); | ||
| } | ||
| } | ||
36 changes: 36 additions & 0 deletions
36
BinDays.Api.IntegrationTests/Collectors/Councils/MansfieldTests.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 MansfieldTests | ||
| { | ||
| private readonly IntegrationTestClient _client; | ||
| private static readonly ICollector _collector = new Mansfield(); | ||
| private readonly CollectorService _collectorService = new([_collector]); | ||
| private readonly ITestOutputHelper _outputHelper; | ||
|
|
||
| public MansfieldTests(ITestOutputHelper outputHelper) | ||
| { | ||
| _outputHelper = outputHelper; | ||
| _client = new IntegrationTestClient(outputHelper); | ||
| } | ||
|
|
||
| [Theory] | ||
| [InlineData("NG19 8PL")] | ||
| 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.