From 8f8a6416500f9f58e77f6760389141ca694a6576 Mon Sep 17 00:00:00 2001 From: SimonLeigh <7461695+SimonLeigh@users.noreply.github.com> Date: Wed, 22 May 2024 20:22:56 +0100 Subject: [PATCH] Add support for Borough of Broxbourne Council (UK), broxbourne.gov.uk. (#2054) * creating copy of oxford for broxbourne collection days form * Partially working Broxbourne implementation, broken where not all services available * Working Broxbourne implementation * Adding comments that explain Broxbourne's table structure * Adding markdown for Broxbourne * Added README and info.md updates, fixed handling of the current year for Broxbourne API * Adding an integer UPRN test case per recent comments on Birmingham PR * remove leap year error + reformatting --------- Co-authored-by: Simon Leigh Co-authored-by: 5ila5 <5ila5@users.noreply.github.com> Co-authored-by: 5ila5 <38183212+5ila5@users.noreply.github.com> --- README.md | 1 + .../source/broxbourne_gov_uk.py | 107 ++++++++++++++++++ doc/source/broxbourne_gov_uk.md | 61 ++++++++++ info.md | 2 +- 4 files changed, 170 insertions(+), 1 deletion(-) create mode 100644 custom_components/waste_collection_schedule/waste_collection_schedule/source/broxbourne_gov_uk.py create mode 100644 doc/source/broxbourne_gov_uk.md diff --git a/README.md b/README.md index 93a2ecc2..58ac2fc7 100644 --- a/README.md +++ b/README.md @@ -1216,6 +1216,7 @@ Waste collection schedules in the following formats and countries are supported. - [Blackburn with Darwen Borough Council](/doc/source/blackburn_gov_uk.md) / blackburn.gov.uk - [Blackpool Council](/doc/source/blackpool_gov_uk.md) / blackpool.gov.uk - [Borough Council of King's Lynn & West Norfolk](/doc/source/west_norfolk_gov_uk.md) / west-norfolk.gov.uk +- [Borough of Broxbourne Council](/doc/source/broxbourne_gov_uk.md) / broxbourne.gov.uk - [Bracknell Forest Council](/doc/source/bracknell_forest_gov_uk.md) / selfservice.mybfc.bracknell-forest.gov.uk - [Bradford Metropolitan District Council](/doc/source/bradford_gov_uk.md) / bradford.gov.uk - [Braintree District Council](/doc/source/braintree_gov_uk.md) / braintree.gov.uk diff --git a/custom_components/waste_collection_schedule/waste_collection_schedule/source/broxbourne_gov_uk.py b/custom_components/waste_collection_schedule/waste_collection_schedule/source/broxbourne_gov_uk.py new file mode 100644 index 00000000..208047d9 --- /dev/null +++ b/custom_components/waste_collection_schedule/waste_collection_schedule/source/broxbourne_gov_uk.py @@ -0,0 +1,107 @@ +import datetime +import logging + +import requests +from bs4 import BeautifulSoup +from waste_collection_schedule import Collection # type: ignore[attr-defined] + +TITLE = "Borough of Broxbourne Council" +DESCRIPTION = "Source for broxbourne.gov.uk services for Broxbourne, UK." +URL = "https://www.broxbourne.gov.uk" +TEST_CASES = { + "Old School Cottage (Domestic Waste Only)": { + "uprn": "148040092", + "postcode": "EN10 7PX", + }, + "11 Park Road (All Services)": {"uprn": "148028240", "postcode": "EN11 8PU"}, + "11 Pulham Avenue (All Services)": {"uprn": 148024643, "postcode": "EN10 7TA"}, +} + +API_URLS = { + "get_session": "https://www.broxbourne.gov.uk/bin-collection-date", + "collection": "https://www.broxbourne.gov.uk/xfp/form/205", +} + +LOGGER = logging.getLogger(__name__) + +ICON_MAP = { + "Domestic": "mdi:trash-can", + "Recycling": "mdi:recycle", + "Green Waste": "mdi:leaf", + "Food": "mdi:food-apple", +} + + +class Source: + def __init__(self, uprn: str, postcode: str): + self._uprn = uprn + self._postcode = postcode + + def fetch(self): + entries: list[Collection] = [] + session = requests.Session() + + token_response = session.get(API_URLS["get_session"]) + soup = BeautifulSoup(token_response.text, "html.parser") + token = soup.find("input", {"name": "__token"}).attrs["value"] + if not token: + raise ValueError( + "Could not parse CSRF Token from initial response. Won't be able to proceed." + ) + + form_data = { + "__token": token, + "page": "490", + "locale": "en_GB", + "qacf7e570cf99fae4cb3a2e14d5a75fd0d6561058_0_0": self._postcode, + "qacf7e570cf99fae4cb3a2e14d5a75fd0d6561058_1_0": self._uprn, + "next": "Next", + } + + collection_response = session.post(API_URLS["collection"], data=form_data) + + collection_soup = BeautifulSoup(collection_response.text, "html.parser") + tr = collection_soup.findAll("tr") + + # The council API returns no year for the collections + # and so it needs to be calculated to format the date correctly + + today = datetime.date.today() + year = today.year + + for item in tr[1:]: # Ignore table header row + td = item.findAll("td") + waste_type = td[1].text.rstrip() + + # We need to replace characters due to encoding in form + collection_date_text = ( + td[0].text.split(" ")[0].replace("\xa0", " ") + " " + str(year) + ) + + try: + # Broxbourne give an empty date field where there is no collection + collection_date = datetime.datetime.strptime( + collection_date_text, "%a %d %B %Y" + ).date() + + except ValueError as e: + LOGGER.warning( + f"No date found for wastetype: {waste_type}. The date field in the table is empty or corrupted. Failed with error: {e}" + ) + continue + + # Calculate the year. As we only get collections a week in advance we can assume the current + # year unless the month is January in December where it will be next year + + if (collection_date.month == 1) and (today.month == 12): + collection_date = collection_date.replace(year=year + 1) + + entries.append( + Collection( + date=collection_date, + t=waste_type, + icon=ICON_MAP.get(waste_type), + ) + ) + + return entries diff --git a/doc/source/broxbourne_gov_uk.md b/doc/source/broxbourne_gov_uk.md new file mode 100644 index 00000000..5fc683d1 --- /dev/null +++ b/doc/source/broxbourne_gov_uk.md @@ -0,0 +1,61 @@ +# Borough of Broxbourne Council + +Support for schedules provided by [Borough of Broxbourne Council](https://www.broxbourne.gov.uk/), in the UK. + +## Configuration via configuration.yaml + +```yaml +waste_collection_schedule: + sources: + - name: broxbourne_gov_uk + args: + uprn: UNIQUE_PROPERTY_REFERENCE_NUMBER + postcode: POSTCODE +``` + +### Configuration Variables + +**uprn** +*(string)* + +The "Unique Property Reference Number" for your address. You can find it by searching for your address at . + +**postcode** +*(string)* + +The Post Code for your address. This needs to match the postcode corresponding to your UPRN. + +## Example + +```yaml +waste_collection_schedule: + sources: + - name: broxbourne_gov_uk + args: + uprn: 148028240 + postcode: EN11 8PU +``` + +## Returned Collections + +This source will return the next collection date for each container type serviced at your address. +If you don't subscribe to a garden waste bin, we don't return data for it. + +## Returned collection types + +### Domestic + +Black bin for general waste + +### Recycling + +Black recycling box for mixed recycling + +### Green Waste + +Green Bin for garden waste. +If you don't pay for a garden waste bin, it won't be included. + +### Food + +Green or Brown Caddy for food waste. diff --git a/info.md b/info.md index 626b5255..591e289e 100644 --- a/info.md +++ b/info.md @@ -36,7 +36,7 @@ Waste collection schedules from service provider web sites are updated daily, de | Slovenia | Moji odpadki, Ljubljana | | Sweden | Affärsverken, Gästrike Återvinnare, Jönköping - June Avfall & Miljö, Landskrona - Svalövs Renhållning, Lerum Vatten och Avlopp, Linköping - Tekniska Verken, Lund Waste Collection, North / Middle Bohuslän - Rambo AB, Region Gotland, Ronneby Miljöteknik, Samverkan Återvinning Miljö (SÅM), SRV Återvinning, SSAM, Sysav Sophämntning, Uppsala Vatten och Avfall AB, VA Syd Sophämntning, VIVAB Sophämtning | | Switzerland | A-Region, Alchenstorf, Andwil, Appenzell, Berg, Bühler, Canton of Zürich, Eggersriet, Gais, Gaiserwald, Goldach, Grosswangen, Grub, Heiden, Herisau, Horn, Hundwil, Häggenschwil, Lindau, Lutzenberg, Muolen, Mörschwil, Münchenstein, Münsingen BE, Switzerland, Real Luzern, Rehetobel, Rorschach, Rorschacherberg, Schwellbrunn, Schönengrund, Speicher, Stein, Steinach, Teufen, Thal, Trogen, Tübach, Untereggen, Urnäsch, Wald, Waldkirch, Waldstatt, Wittenbach, Wolfhalden | -| United Kingdom | Aberdeenshire Council, Adur & Worthing Councils, Allerdale Borough Council, Amber Valley Borough Council, Anglesey, Ashfield District Council, Ashford Borough Council, Aylesbury Vale District Council, Barnsley Metropolitan Borough Council, Basildon Council, Basingstoke and Deane Borough Council, Bath & North East Somerset Council, BCP Council, Bedford Borough Council, Binzone, Birmingham City Council, Blackburn with Darwen Borough Council, Blackpool Council, Borough Council of King's Lynn & West Norfolk, Bracknell Forest Council, Bradford Metropolitan District Council, Braintree District Council, Breckland Council, Bristol City Council, Broadland District Council, Broxtowe Borough Council, Buckinghamshire Waste Collection - Former Chiltern, South Bucks or Wycombe areas, Burnley Council, Bury Council, Cambridge City Council, Canterbury City Council, Cardiff Council, Central Bedfordshire Council, Cherwell District Council, Cheshire East Council, Cheshire West and Chester Council, Chesterfield Borough Council, Chichester District Council, City of Doncaster Council, City Of Lincoln Council, City of York Council, Colchester City Council, Conwy County Borough Council, Cornwall Council, Crawley Borough Council (myCrawley), Croydon Council, Denbighshire County Council, Derby City Council, Dudley Metropolitan Borough Council, Durham County Council, East Ayrshire Council, East Cambridgeshire District Council, East Devon District Council, East Herts Council, East Northamptonshire and Wellingborough, East Renfrewshire Council, East Riding of Yorkshire Council, Eastbourne Borough Council, Eastleigh Borough Council, Elmbridge Borough Council, Environment First, Exeter City Council, Falkirk, Fareham Council, FCC Environment, Fenland District Council, Fife Council, Flintshire, Fylde Council, Gateshead Council, Glasgow City Council, Guildford Borough Council, Gwynedd, Harborough District Council, Haringey Council, Harlow Council, Hart District Council, Herefordshire City Council, Highland, Horsham District Council, Huntingdonshire District Council, iTouchVision, Joint Waste Solutions, Kirklees Council, Lancaster City Council, Leicester City Council, Lewes District Council, Lichfield District Council, Lisburn and Castlereagh City Council, Liverpool City Council, London Borough of Barking and Dagenham, London Borough of Bexley, London Borough of Bromley, London Borough of Camden, London Borough of Harrow, London Borough of Hounslow, London Borough of Lewisham, London Borough of Merton, London Borough of Newham, Maidstone Borough Council, Maldon District Council, Manchester City Council, Mansfield District Council, Mendip District Council, Mid-Sussex District Council, Middlesbrough Council, Milton Keynes council, Moray Council, Newcastle City Council, Newcastle Under Lyme Borough Council, Newport City Council, North Ayrshire Council, North Herts Council, North Kesteven District Council, North Lincolnshire Council, North Northamptonshire council, North Somerset Council, North West Leicestershire District Council, North Yorkshire Council - Hambleton, North Yorkshire Council - Harrogate, North Yorkshire Council - Scarborough, North Yorkshire Council - Selby, Nottingham City Council, Oxford City Council, Peterborough City Council, Portsmouth City Council, Reading Council, Redbridge Council, Reigate & Banstead Borough Council, Renfrewshire Council, Rhondda Cynon Taf County Borough Council, Richmondshire District Council, Rotherham Metropolitan Borough Council, Runnymede Borough Council, Rushcliffe Brough Council, Rushmoor Borough Council, Salford City Council, Sedgemoor District Council, Sheffield City Council, Shropshire Council, Somerset Council, Somerset County Council, Somerset West & Taunton District Council, South Cambridgeshire District Council, South Derbyshire District Council, South Gloucestershire Council, South Hams District Council, South Holland District Council, South Norfolk Council, South Oxfordshire District Council, South Somerset District Council, South Tyneside Council, Southampton City Council, Stafford Borough Council, Stevenage Borough Council, Stirling.gov.uk, Stockport Council, Stockton-on-Tees Borough Council, Stoke-on-Trent, Stratford District Council, Surrey Heath Borough Council, Sutton Council, London, Swansea Council, Swindon Borough Council, Tameside Metropolitan Borough Council, Telford and Wrekin Council, Test Valley Borough Council, Tewkesbury Borough Council, The Royal Borough of Kingston Council, Tonbridge and Malling Borough Council, Tunbridge Wells, UK Bin Collection Schedule (UKBCD) project, Uttlesford District Council, Vale of White Horse District Council, Walsall Council, Warrington Borough Council, Warwick District Council, Waverley Borough Council, Wealden District Council, Welwyn Hatfield Borough Council, West Berkshire Council, West Devon Borough Council, West Dunbartonshire Council, West Northamptonshire council, West Suffolk Council, Westmorland & Furness Council, Barrow area, Westmorland & Furness Council, South Lakeland area, Wigan Council, Wiltshire Council, Windsor and Maidenhead, Wirral Council, Woking Borough Council, Wokingham Borough Council, Wyre Forest District Council | +| United Kingdom | Aberdeenshire Council, Adur & Worthing Councils, Allerdale Borough Council, Amber Valley Borough Council, Anglesey, Ashfield District Council, Ashford Borough Council, Aylesbury Vale District Council, Barnsley Metropolitan Borough Council, Basildon Council, Basingstoke and Deane Borough Council, Bath & North East Somerset Council, BCP Council, Bedford Borough Council, Binzone, Birmingham City Council, Blackburn with Darwen Borough Council, Blackpool Council, Borough Council of King's Lynn & West Norfolk, Borough of Broxbourne Council, Bracknell Forest Council, Bradford Metropolitan District Council, Braintree District Council, Breckland Council, Bristol City Council, Broadland District Council, Broxtowe Borough Council, Buckinghamshire Waste Collection - Former Chiltern, South Bucks or Wycombe areas, Burnley Council, Bury Council, Cambridge City Council, Canterbury City Council, Cardiff Council, Central Bedfordshire Council, Cherwell District Council, Cheshire East Council, Cheshire West and Chester Council, Chesterfield Borough Council, Chichester District Council, City of Doncaster Council, City Of Lincoln Council, City of York Council, Colchester City Council, Conwy County Borough Council, Cornwall Council, Crawley Borough Council (myCrawley), Croydon Council, Denbighshire County Council, Derby City Council, Dudley Metropolitan Borough Council, Durham County Council, East Ayrshire Council, East Cambridgeshire District Council, East Devon District Council, East Herts Council, East Northamptonshire and Wellingborough, East Renfrewshire Council, East Riding of Yorkshire Council, Eastbourne Borough Council, Eastleigh Borough Council, Elmbridge Borough Council, Environment First, Exeter City Council, Falkirk, Fareham Council, FCC Environment, Fenland District Council, Fife Council, Flintshire, Fylde Council, Gateshead Council, Glasgow City Council, Guildford Borough Council, Gwynedd, Harborough District Council, Haringey Council, Harlow Council, Hart District Council, Herefordshire City Council, Highland, Horsham District Council, Huntingdonshire District Council, iTouchVision, Joint Waste Solutions, Kirklees Council, Lancaster City Council, Leicester City Council, Lewes District Council, Lichfield District Council, Lisburn and Castlereagh City Council, Liverpool City Council, London Borough of Barking and Dagenham, London Borough of Bexley, London Borough of Bromley, London Borough of Camden, London Borough of Harrow, London Borough of Hounslow, London Borough of Lewisham, London Borough of Merton, London Borough of Newham, Maidstone Borough Council, Maldon District Council, Manchester City Council, Mansfield District Council, Mendip District Council, Mid-Sussex District Council, Middlesbrough Council, Milton Keynes council, Moray Council, Newcastle City Council, Newcastle Under Lyme Borough Council, Newport City Council, North Ayrshire Council, North Herts Council, North Kesteven District Council, North Lincolnshire Council, North Northamptonshire council, North Somerset Council, North West Leicestershire District Council, North Yorkshire Council - Hambleton, North Yorkshire Council - Harrogate, North Yorkshire Council - Scarborough, North Yorkshire Council - Selby, Nottingham City Council, Oxford City Council, Peterborough City Council, Portsmouth City Council, Reading Council, Redbridge Council, Reigate & Banstead Borough Council, Renfrewshire Council, Rhondda Cynon Taf County Borough Council, Richmondshire District Council, Rotherham Metropolitan Borough Council, Runnymede Borough Council, Rushcliffe Brough Council, Rushmoor Borough Council, Salford City Council, Sedgemoor District Council, Sheffield City Council, Shropshire Council, Somerset Council, Somerset County Council, Somerset West & Taunton District Council, South Cambridgeshire District Council, South Derbyshire District Council, South Gloucestershire Council, South Hams District Council, South Holland District Council, South Norfolk Council, South Oxfordshire District Council, South Somerset District Council, South Tyneside Council, Southampton City Council, Stafford Borough Council, Stevenage Borough Council, Stirling.gov.uk, Stockport Council, Stockton-on-Tees Borough Council, Stoke-on-Trent, Stratford District Council, Surrey Heath Borough Council, Sutton Council, London, Swansea Council, Swindon Borough Council, Tameside Metropolitan Borough Council, Telford and Wrekin Council, Test Valley Borough Council, Tewkesbury Borough Council, The Royal Borough of Kingston Council, Tonbridge and Malling Borough Council, Tunbridge Wells, UK Bin Collection Schedule (UKBCD) project, Uttlesford District Council, Vale of White Horse District Council, Walsall Council, Warrington Borough Council, Warwick District Council, Waverley Borough Council, Wealden District Council, Welwyn Hatfield Borough Council, West Berkshire Council, West Devon Borough Council, West Dunbartonshire Council, West Northamptonshire council, West Suffolk Council, Westmorland & Furness Council, Barrow area, Westmorland & Furness Council, South Lakeland area, Wigan Council, Wiltshire Council, Windsor and Maidenhead, Wirral Council, Woking Borough Council, Wokingham Borough Council, Wyre Forest District Council | | United States of America | Albuquerque, New Mexico, USA, City of Austin, TX, City of Bloomington, City of Cambridge, City of Gastonia, NC, City of Georgetown, TX, City of Oklahoma City, City of Pittsburgh, Louisville, Kentucky, USA, Newark, Delaware, USA, Olympia, Washington, USA, ReCollect, Recycle Coach, Republic Services, Seattle Public Utilities, Tucson, Arizona, USA |