-
-
Notifications
You must be signed in to change notification settings - Fork 4
Add collector for Harborough District Council #171
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
BadgerHobbs
merged 3 commits into
main
from
collector/HarboroughDistrictCouncil-issue-133-1771756458
Feb 22, 2026
Merged
Changes from all commits
Commits
Show all changes
3 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
234 changes: 234 additions & 0 deletions
234
BinDays.Api.Collectors/Collectors/Councils/HarboroughDistrictCouncil.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,234 @@ | ||
| 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 Harborough District Council. | ||
| /// </summary> | ||
| internal sealed partial class HarboroughDistrictCouncil : GovUkCollectorBase, ICollector | ||
| { | ||
| /// <inheritdoc/> | ||
| public string Name => "Harborough District Council"; | ||
|
|
||
| /// <inheritdoc/> | ||
| public Uri WebsiteUrl => new("https://www.harborough.gov.uk/"); | ||
|
|
||
| /// <inheritdoc/> | ||
| public override string GovUkId => "harborough"; | ||
|
|
||
| /// <summary> | ||
| /// The list of bin types for this collector. | ||
| /// </summary> | ||
| private readonly IReadOnlyCollection<Bin> _binTypes = | ||
| [ | ||
| new() | ||
| { | ||
| Name = "General Waste", | ||
| Colour = BinColour.Black, | ||
| Keys = [ "Non-recyclable waste" ], | ||
| }, | ||
| new() | ||
| { | ||
| Name = "Mixed Recycling", | ||
| Colour = BinColour.Blue, | ||
| Keys = [ "Recycling collection" ], | ||
| }, | ||
| new() | ||
| { | ||
| Name = "Garden Waste", | ||
| Colour = BinColour.Green, | ||
| Keys = [ "Garden waste" ], | ||
| }, | ||
| ]; | ||
|
|
||
| /// <summary> | ||
| /// The base URL for the FCC Environment API. | ||
| /// </summary> | ||
| private const string _baseUrl = "https://harborough.fccenvironment.co.uk/"; | ||
|
|
||
| /// <summary> | ||
| /// The value for the x-forwarded-proto header. | ||
| /// </summary> | ||
| private const string _forwardedProtoHeaderValue = "https"; | ||
|
|
||
| /// <summary> | ||
| /// Regex for the bin days list items. | ||
| /// </summary> | ||
| [GeneratedRegex(@"<li>\s*(?<service>[^<]+?)\s*<span[^>]*>\s*(?<date>[^<]+)\s*</span>\s*</li>", RegexOptions.IgnoreCase)] | ||
| private static partial Regex BinDaysRegex(); | ||
|
|
||
| /// <summary> | ||
| /// Regex for the next scheduled bin collection block. | ||
| /// </summary> | ||
| [GeneratedRegex(@"block-your-next-scheduled-bin-collection-days"".*?(?<content><ul>.*?</ul>)", RegexOptions.Singleline)] | ||
| private static partial Regex BinDaysSectionRegex(); | ||
|
|
||
| /// <inheritdoc/> | ||
| public GetAddressesResponse GetAddresses(string postcode, ClientSideResponse? clientSideResponse) | ||
| { | ||
| // Prepare client-side request for getting addresses | ||
| if (clientSideResponse == null) | ||
| { | ||
| var requestBody = JsonSerializer.Serialize(new { Postcode = postcode }); | ||
|
|
||
| var clientSideRequest = new ClientSideRequest | ||
| { | ||
| RequestId = 1, | ||
| Url = $"{_baseUrl}getAddress", | ||
| Method = "POST", | ||
| Headers = new() | ||
| { | ||
| { "user-agent", Constants.UserAgent }, | ||
| { "content-type", "application/json" }, | ||
| { "x-forwarded-proto", _forwardedProtoHeaderValue }, | ||
| }, | ||
| Body = requestBody, | ||
| Options = new ClientSideOptions | ||
| { | ||
| FollowRedirects = false, | ||
| }, | ||
| }; | ||
|
|
||
| var getAddressesResponse = new GetAddressesResponse | ||
| { | ||
| NextClientSideRequest = clientSideRequest, | ||
| }; | ||
|
|
||
| return getAddressesResponse; | ||
| } | ||
| // Process addresses from response | ||
| else if (clientSideResponse.RequestId == 1) | ||
| { | ||
| using var jsonDocument = JsonDocument.Parse(clientSideResponse.Content); | ||
| var addressElements = jsonDocument.RootElement.GetProperty("datas").EnumerateArray(); | ||
|
|
||
| // Iterate through each address, and create a new address object | ||
| var addresses = new List<Address>(); | ||
| foreach (var addressElement in addressElements) | ||
| { | ||
| var uprn = addressElement.GetProperty("AccountSiteUprn").GetString()!; | ||
|
|
||
| if (string.IsNullOrWhiteSpace(uprn)) | ||
| { | ||
| continue; | ||
| } | ||
|
|
||
| var property = addressElement.GetProperty("SiteShortAddress").GetString()!.Trim(); | ||
| var addressLabel = addressElement.GetProperty("SiteShortAddressLabel").GetString()!.Trim(); | ||
|
|
||
| // Uid format: "{uprn};{addressLabel}" | ||
| var address = new Address | ||
| { | ||
| Property = property, | ||
| Postcode = postcode, | ||
| Uid = $"{uprn};{addressLabel}", | ||
| }; | ||
|
|
||
| addresses.Add(address); | ||
| } | ||
|
|
||
| var getAddressesResponse = new GetAddressesResponse | ||
| { | ||
| Addresses = [.. addresses], | ||
| }; | ||
|
|
||
| return getAddressesResponse; | ||
| } | ||
|
|
||
| // Throw exception for invalid request | ||
| 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) | ||
| { | ||
| // Uid format: "{uprn};{addressLabel}" | ||
| var addressParts = address.Uid!.Split(';', 2); | ||
| var uprn = addressParts[0]; | ||
| var addressLabel = addressParts[1]; | ||
|
|
||
| var requestBody = ProcessingUtilities.ConvertDictionaryToFormData(new() | ||
| { | ||
| { "Uprn", uprn }, | ||
| { "hiddenAddressLabel", addressLabel }, | ||
| }); | ||
|
|
||
| var clientSideRequest = new ClientSideRequest | ||
| { | ||
| RequestId = 1, | ||
| Url = $"{_baseUrl}detail-address", | ||
| Method = "POST", | ||
| Headers = new() | ||
| { | ||
| { "user-agent", Constants.UserAgent }, | ||
| { "content-type", "application/x-www-form-urlencoded; charset=UTF-8" }, | ||
| { "x-forwarded-proto", _forwardedProtoHeaderValue }, | ||
| }, | ||
| Body = requestBody, | ||
| Options = new ClientSideOptions | ||
| { | ||
| FollowRedirects = false, | ||
| }, | ||
| }; | ||
|
|
||
| var getBinDaysResponse = new GetBinDaysResponse | ||
| { | ||
| NextClientSideRequest = clientSideRequest, | ||
| }; | ||
|
|
||
| return getBinDaysResponse; | ||
| } | ||
| // Process bin days from response | ||
| else if (clientSideResponse.RequestId == 1) | ||
| { | ||
| var binDaysContent = BinDaysSectionRegex().Match(clientSideResponse.Content).Groups["content"].Value; | ||
| var rawBinDays = BinDaysRegex().Matches(binDaysContent)!; | ||
|
|
||
| // Iterate through each bin day, and create a new bin day object | ||
| var binDays = new List<BinDay>(); | ||
| foreach (Match rawBinDay in rawBinDays) | ||
| { | ||
| var service = rawBinDay.Groups["service"].Value.Trim(); | ||
| var collectionDate = rawBinDay.Groups["date"].Value.Trim(); | ||
|
|
||
| var date = DateOnly.ParseExact( | ||
| collectionDate, | ||
| "d MMMM yyyy", | ||
| CultureInfo.InvariantCulture, | ||
| DateTimeStyles.None | ||
| ); | ||
|
|
||
| var matchedBins = ProcessingUtilities.GetMatchingBins(_binTypes, service); | ||
|
|
||
| var binDay = new BinDay | ||
| { | ||
| Date = date, | ||
| Address = address, | ||
| Bins = matchedBins, | ||
| }; | ||
|
|
||
| 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."); | ||
| } | ||
| } | ||
32 changes: 32 additions & 0 deletions
32
BinDays.Api.IntegrationTests/Collectors/Councils/HarboroughDistrictCouncilTests.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,32 @@ | ||
| namespace BinDays.Api.IntegrationTests.Collectors.Councils; | ||
|
|
||
| using BinDays.Api.Collectors.Collectors.Councils; | ||
| using BinDays.Api.IntegrationTests.Helpers; | ||
| using System.Threading.Tasks; | ||
| using Xunit; | ||
| using Xunit.Abstractions; | ||
|
|
||
| public class HarboroughDistrictCouncilTests | ||
| { | ||
| private readonly IntegrationTestClient _client; | ||
| private readonly ITestOutputHelper _outputHelper; | ||
| private static readonly string _govUkId = new HarboroughDistrictCouncil().GovUkId; | ||
|
|
||
| public HarboroughDistrictCouncilTests(ITestOutputHelper outputHelper) | ||
| { | ||
| _outputHelper = outputHelper; | ||
| _client = new IntegrationTestClient(outputHelper); | ||
| } | ||
|
|
||
| [Theory] | ||
| [InlineData("LE17 5EG")] | ||
| public async Task GetBinDaysTest(string postcode) | ||
| { | ||
| await TestSteps.EndToEnd( | ||
| _client, | ||
| postcode, | ||
| _govUkId, | ||
| _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.