-
-
Notifications
You must be signed in to change notification settings - Fork 4
Add collector for East Devon District Council #114
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 4 commits into
main
from
collector/EastDevonDistrictCouncil-issue-24-1768472613
Feb 7, 2026
+263
−0
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
7f4634d
Add collector for EastDevonDistrictCouncil
49da6c3
Auto-format code with dotnet format
8dffaed
Address PR review comments for EastDevonDistrictCouncil
github-actions[bot] 640e9bb
Use ParseDateInferringYear for more robust date parsing
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
227 changes: 227 additions & 0 deletions
227
BinDays.Api.Collectors/Collectors/Councils/EastDevonDistrictCouncil.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,227 @@ | ||
| 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.Net; | ||
| using System.Text.Json; | ||
| using System.Text.RegularExpressions; | ||
|
|
||
| /// <summary> | ||
| /// Collector implementation for East Devon District Council. | ||
| /// </summary> | ||
| internal sealed partial class EastDevonDistrictCouncil : GovUkCollectorBase, ICollector | ||
| { | ||
| /// <inheritdoc/> | ||
| public string Name => "East Devon District Council"; | ||
|
|
||
| /// <inheritdoc/> | ||
| public Uri WebsiteUrl => new("https://eastdevon.gov.uk/"); | ||
|
|
||
| /// <inheritdoc/> | ||
| public override string GovUkId => "east-devon"; | ||
|
|
||
| /// <summary> | ||
| /// The list of bin types for this collector. | ||
| /// </summary> | ||
| private readonly IReadOnlyCollection<Bin> _binTypes = | ||
| [ | ||
| new() | ||
| { | ||
| Name = "General Waste", | ||
| Colour = BinColour.Black, | ||
| Keys = [ "Rubbish" ], | ||
| }, | ||
| new() | ||
| { | ||
| Name = "Paper, Glass & Cardboard Recycling", | ||
| Colour = BinColour.Green, | ||
| Keys = [ "Recycling and food waste" ], | ||
| Type = BinType.Box, | ||
| }, | ||
| new() | ||
| { | ||
| Name = "Plastics & Tins Recycling", | ||
| Colour = BinColour.Green, | ||
| Keys = [ "Recycling and food waste" ], | ||
| Type = BinType.Sack, | ||
| }, | ||
| new() | ||
| { | ||
| Name = "Food Waste", | ||
| Colour = BinColour.Blue, | ||
| Keys = [ "Recycling and food waste" ], | ||
| Type = BinType.Caddy, | ||
| }, | ||
| new() | ||
| { | ||
| Name = "Garden Waste", | ||
| Colour = BinColour.Green, | ||
| Keys = [ "Green waste" ], | ||
| }, | ||
| ]; | ||
BadgerHobbs marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| /// <summary> | ||
| /// Regex for parsing month headers and collection entries from the calendar. | ||
| /// </summary> | ||
| [GeneratedRegex(@"<li class=""eventmonth""[^>]*><h2>(?<month>[A-Za-z]+)(?:\s+\d{4})?</h2></li>|<li><span class=""collectiondate[^""]*"">(?<date>[^<]+)</span>(?<bins>.*?)</li>", RegexOptions.IgnoreCase | RegexOptions.Singleline)] | ||
| private static partial Regex CollectionEntryRegex(); | ||
|
|
||
| /// <summary> | ||
| /// Regex for extracting bin names from a collection entry. | ||
| /// </summary> | ||
| [GeneratedRegex(@"<span class=""collection-[^""]+"">(?<bin>[^<]+)</span>", RegexOptions.IgnoreCase)] | ||
| private static partial Regex BinNameRegex(); | ||
|
|
||
| /// <summary> | ||
| /// Regex for extracting the day number from a date string. | ||
| /// </summary> | ||
| [GeneratedRegex(@"\d+")] | ||
| private static partial Regex DayNumberRegex(); | ||
|
|
||
| /// <inheritdoc/> | ||
| public GetAddressesResponse GetAddresses(string postcode, ClientSideResponse? clientSideResponse) | ||
| { | ||
| // Prepare client-side request for getting addresses | ||
| if (clientSideResponse == null) | ||
| { | ||
| var clientSideRequest = new ClientSideRequest | ||
| { | ||
| RequestId = 1, | ||
| Url = $"https://eastdevon.gov.uk/addressfinder?qtype=bins&term={postcode}", | ||
| Method = "GET", | ||
| Headers = new() | ||
| { | ||
| {"User-Agent", Constants.UserAgent}, | ||
| {"X-Requested-With", "XMLHttpRequest"}, | ||
BadgerHobbs marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| }, | ||
| }; | ||
|
|
||
| var getAddressesResponse = new GetAddressesResponse | ||
| { | ||
| NextClientSideRequest = clientSideRequest | ||
| }; | ||
|
|
||
| return getAddressesResponse; | ||
| } | ||
| // Process addresses from response | ||
| else if (clientSideResponse.RequestId == 1) | ||
| { | ||
| using var jsonDoc = JsonDocument.Parse(clientSideResponse.Content); | ||
| var addresses = new List<Address>(); | ||
|
|
||
| // Iterate through each address, and create a new address object | ||
| foreach (var element in jsonDoc.RootElement.EnumerateArray()) | ||
| { | ||
| var property = element.GetProperty("label").GetString()!.Trim(); | ||
| var uprn = element.GetProperty("UPRN").GetString()!.Trim(); | ||
|
|
||
| if (string.IsNullOrWhiteSpace(uprn)) | ||
| { | ||
| continue; | ||
| } | ||
|
|
||
| var address = new Address | ||
| { | ||
| Property = property, | ||
| Postcode = postcode, | ||
| Uid = uprn, | ||
| }; | ||
|
|
||
| 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) | ||
| { | ||
| var clientSideRequest = new ClientSideRequest | ||
| { | ||
| RequestId = 1, | ||
| Url = $"https://eastdevon.gov.uk/recycling-and-waste/recycling-waste-information/when-is-my-bin-collected/future-collections-calendar/?UPRN={address.Uid}", | ||
| Method = "GET", | ||
| Headers = new() | ||
| { | ||
| {"User-Agent", Constants.UserAgent}, | ||
BadgerHobbs marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| }, | ||
| }; | ||
|
|
||
| var getBinDaysResponse = new GetBinDaysResponse | ||
| { | ||
| NextClientSideRequest = clientSideRequest | ||
| }; | ||
|
|
||
| return getBinDaysResponse; | ||
| } | ||
| // Process bin days from response | ||
| else if (clientSideResponse.RequestId == 1) | ||
| { | ||
| var currentMonth = string.Empty; | ||
| var binDays = new List<BinDay>(); | ||
|
|
||
| // Iterate through each calendar entry, and build bin day objects | ||
| foreach (Match collectionEntry in CollectionEntryRegex().Matches(clientSideResponse.Content)!) | ||
| { | ||
| var month = collectionEntry.Groups["month"].Value; | ||
| if (!string.IsNullOrWhiteSpace(month)) | ||
| { | ||
| currentMonth = WebUtility.HtmlDecode(month).Trim(); | ||
| continue; | ||
| } | ||
|
|
||
| var dateText = WebUtility.HtmlDecode(collectionEntry.Groups["date"].Value).Trim(); | ||
| var day = DayNumberRegex().Match(dateText).Value; | ||
|
|
||
| var date = $"{day} {currentMonth}".ParseDateInferringYear("d MMMM"); | ||
|
|
||
| var binsHtml = WebUtility.HtmlDecode(collectionEntry.Groups["bins"].Value); | ||
| var bins = new List<Bin>(); | ||
|
|
||
| // Iterate through each bin, and map to configured bin types | ||
| foreach (Match binMatch in BinNameRegex().Matches(binsHtml)!) | ||
| { | ||
| var binName = binMatch.Groups["bin"].Value.Trim(); | ||
| var matchedBins = ProcessingUtilities.GetMatchingBins(_binTypes, binName); | ||
|
|
||
| bins.AddRange(matchedBins); | ||
| } | ||
|
|
||
| var binDay = new BinDay | ||
| { | ||
| Date = date, | ||
| Address = address, | ||
| Bins = [.. bins], | ||
| }; | ||
|
|
||
| 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/EastDevonDistrictCouncilTests.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 EastDevonDistrictCouncilTests | ||
| { | ||
| private readonly IntegrationTestClient _client; | ||
| private static readonly ICollector _collector = new EastDevonDistrictCouncil(); | ||
| private readonly CollectorService _collectorService = new([_collector]); | ||
| private readonly ITestOutputHelper _outputHelper; | ||
|
|
||
| public EastDevonDistrictCouncilTests(ITestOutputHelper outputHelper) | ||
| { | ||
| _outputHelper = outputHelper; | ||
| _client = new IntegrationTestClient(outputHelper); | ||
| } | ||
|
|
||
| [Theory] | ||
| [InlineData("EX10 8UB")] | ||
| 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.