Repository navigation
fix: switch canterbury to the council's new bin dates endpoint #2308
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
InertiaUK
wants to merge
1
commit into
robbrad:master
Choose a base branch
from
InertiaUK:fix/canterbury-new-endpoint
base: master
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
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
87 changes: 45 additions & 42 deletions
87
uk_bin_collection/uk_bin_collection/councils/CanterburyCityCouncil.py
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 |
|---|---|---|
| @@ -1,72 +1,75 @@ | ||
| import time | ||
| import json | ||
| from datetime import datetime | ||
|
|
||
| import requests | ||
|
|
||
| from uk_bin_collection.uk_bin_collection.common import * | ||
| from uk_bin_collection.uk_bin_collection.common import check_uprn, date_format | ||
| from uk_bin_collection.uk_bin_collection.get_bin_data import AbstractGetBinDataClass | ||
|
|
||
|
|
||
| # import the wonderful Beautiful Soup and the URL grabber | ||
| class CouncilClass(AbstractGetBinDataClass): | ||
| """ | ||
| Concrete classes have to implement all abstract operations of the | ||
| base class. They can also override some operations with a default | ||
| implementation. | ||
| """ | ||
|
|
||
| def parse_data(self, page: str, **kwargs) -> dict: | ||
| # Endpoint used by the bins widget on | ||
| # https://www.canterbury.gov.uk/bins-and-waste/find-your-bin-collection-dates | ||
| # (the old /Beta/get-bin-dates endpoint now returns 403 for every request). | ||
| API_URL = "https://n6ljrw455m.execute-api.eu-west-2.amazonaws.com/prod/get-bin-dates" | ||
|
|
||
| BIN_TYPES = { | ||
| "blackBinDay": "General", | ||
| "recyclingBinDay": "Recycling", | ||
| "redRecyclingBinDay": "Red Recycling", | ||
| "blueRecyclingBinDay": "Blue Recycling", | ||
| "foodBinDay": "Food", | ||
| "communalFoodBinDay": "Communal Food", | ||
| "gardenBinDay": "Garden", | ||
| } | ||
|
|
||
| def parse_data(self, page: str, **kwargs) -> dict: | ||
| user_uprn = kwargs.get("uprn") | ||
| check_uprn(user_uprn) | ||
| bindata = {"bins": []} | ||
|
|
||
| data = {"uprn": user_uprn, "usrn": "1"} | ||
| # The API keys on the UPRN; the USRN is required in the payload but | ||
| # its value is not used for the lookup. | ||
| data = {"uprn": str(user_uprn), "usrn": "1"} | ||
|
|
||
| # The API Gateway rejects requests without a browser-like User-Agent. | ||
| headers = { | ||
| "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64)", | ||
| "Accept": "application/json", | ||
| "Origin": "https://www.canterbury.gov.uk", | ||
| "Referer": "https://www.canterbury.gov.uk/", | ||
| } | ||
|
|
||
| URI = ( | ||
| "https://zbr7r13ke2.execute-api.eu-west-2.amazonaws.com/Beta/get-bin-dates" | ||
| ) | ||
|
|
||
| # Make the GET request | ||
| response = requests.post(URI, json=data, headers=headers) | ||
| if response.status_code == 403: | ||
| # The council's site itself now calls this API server-side | ||
| # (as part of a page redirect) rather than directly from the | ||
| # browser, and even their own live site currently gets stuck | ||
| # on a permanent loading spinner - the API returns a bare 403 | ||
| # regardless of the uprn/usrn payload sent. This looks like an | ||
| # outage or an access restriction on the council's end, not | ||
| # something fixable by changing what we send. | ||
| raise ConnectionError( | ||
| "Canterbury's bin collection API is returning 403 Forbidden " | ||
| "- this looks like an outage or access restriction on the " | ||
| "council's end, not this scraper. Try again later." | ||
| ) | ||
| response = requests.post(self.API_URL, json=data, headers=headers, timeout=30) | ||
| response.raise_for_status() | ||
|
|
||
| # Parse the JSON response | ||
| bin_collection = json.loads(response.json()["dates"]) | ||
| collections = { | ||
| "General": bin_collection["blackBinDay"], | ||
| "Recycling": bin_collection["recyclingBinDay"], | ||
| "Food": bin_collection["foodBinDay"], | ||
| "Garden": bin_collection["gardenBinDay"], | ||
| } | ||
| # Loop through each collection in bin_collection | ||
| for collection in collections: | ||
| print(collection) | ||
| dates = response.json().get("dates") | ||
| # Older versions of the API returned the dates as a JSON string | ||
| if isinstance(dates, str): | ||
| dates = json.loads(dates) | ||
| if not dates: | ||
| raise ValueError(f"No collection data returned for UPRN {user_uprn}") | ||
|
|
||
| if len(collections[collection]) <= 0: | ||
| continue | ||
| for date in collections[collection]: | ||
| date = ( | ||
| datetime.strptime(date, "%Y-%m-%dT%H:%M:%S").strftime("%d/%m/%Y"), | ||
| for key, bin_type in self.BIN_TYPES.items(): | ||
| for date in dates.get(key) or []: | ||
| collection_date = datetime.strptime(date, "%Y-%m-%dT%H:%M:%S") | ||
| bindata["bins"].append( | ||
| { | ||
| "type": bin_type, | ||
| "collectionDate": collection_date.strftime(date_format), | ||
| } | ||
| ) | ||
| dict_data = {"type": collection, "collectionDate": date[0]} | ||
| bindata["bins"].append(dict_data) | ||
|
|
||
| if not bindata["bins"]: | ||
| raise ValueError(f"No collections found for UPRN {user_uprn}") | ||
|
|
||
| bindata["bins"].sort( | ||
| key=lambda x: datetime.strptime(x["collectionDate"], date_format) | ||
| ) | ||
| return bindata | ||
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🔎 Supported by static analysis
Script executed:
Repository: robbrad/UKBinCollectionData
Length of output: 3479
Validate the response shape before reading
dates.If a 200 response body is a JSON array,
response.json().get("dates")on line 52 raisesAttributeError.raise_for_status()does not check the body, so no earlier guard prevents this. Ifdatesis a JSON array, or a JSON string that decodes to an array,dates.get(key)on line 60 raisesAttributeErrortoo. In both cases, the parser fails withAttributeErrorinstead of theValueErrorused for other malformed data. Check that the top-level payload anddatesare mappings, and raiseValueErrorotherwise.🐛 Suggested fix
Source: Learnings