diff --git a/README.md b/README.md index 0663011c..2f932ace 100644 --- a/README.md +++ b/README.md @@ -1370,6 +1370,7 @@ If your service provider is not listed, feel free to open a [source request issu - [Allerdale Borough Council](/doc/source/allerdale_gov_uk.md) / allerdale.gov.uk - [Amber Valley Borough Council](/doc/source/ambervalley_gov_uk.md) / ambervalley.gov.uk - [Anglesey](/doc/ics/anglesey_gov_wales.md) / anglesey.gov.wales +- [Antrim and Newtownabbey](/doc/source/antrimandnewtownabbey_gov_uk.md) / antrimandnewtownabbey.gov.uk - [Apps by imactivate](/doc/source/apps_imactivate_com.md) / imactivate.com - [Ards and North Down Borough Council](/doc/source/ardsandnorthdown_gov_uk.md) / ardsandnorthdown.gov.uk - [Arun District Council](/doc/source/arun_gov_uk.md) / arun.gov.uk diff --git a/custom_components/waste_collection_schedule/sources.json b/custom_components/waste_collection_schedule/sources.json index 05718ae3..8f0bae64 100644 --- a/custom_components/waste_collection_schedule/sources.json +++ b/custom_components/waste_collection_schedule/sources.json @@ -7419,6 +7419,11 @@ "module": "ics", "default_params": {} }, + { + "title": "Antrim and Newtownabbey", + "module": "antrimandnewtownabbey_gov_uk", + "default_params": {} + }, { "title": "Apps by imactivate", "module": "apps_imactivate_com", diff --git a/custom_components/waste_collection_schedule/waste_collection_schedule/source/antrimandnewtownabbey_gov_uk.py b/custom_components/waste_collection_schedule/waste_collection_schedule/source/antrimandnewtownabbey_gov_uk.py new file mode 100644 index 00000000..81d70907 --- /dev/null +++ b/custom_components/waste_collection_schedule/waste_collection_schedule/source/antrimandnewtownabbey_gov_uk.py @@ -0,0 +1,209 @@ +import logging +import re +from datetime import date, datetime, timedelta + +import requests +from bs4 import BeautifulSoup, Tag +from dateutil.parser import parse +from waste_collection_schedule import Collection # type: ignore[attr-defined] + +_LOGGER = logging.getLogger(__name__) + +TITLE = "Antrim and Newtownabbey" +DESCRIPTION = "Source for Antrim and Newtownabbey." +URL = "https://antrimandnewtownabbey.gov.uk" +TEST_CASES = { + "uprn + id": {"id": 1456, "uprn": 185354344}, + "uprn": {"uprn": "185405500"}, + "id_str": {"id": "1145"}, +} + + +ICON_MAP = { + "Black bins": "mdi:trash-can", + "Brown bins": "mdi:leaf", + "Kerbside Recycling": "mdi:recycle", +} + + +REGULAR_API_URL = ( + "https://antrimandnewtownabbey.gov.uk/residents/bins-recycling/bins-schedule/" +) +RECYCLING_API_URL = "https://www.brysonrecycling.org/northern-ireland/kerbside-collections/collection-day" +RECYCLING_HOLYDAY_API_URL = "https://www.brysonrecycling.org/northern-ireland/kerbside-collections/bank-holidays/" + +REGEX_REMOVE_TH_ND_RD_ST = r"(?<=\d)(st|nd|rd|th)" + + +FREQUENCY_MAP = { + "every week": 7, + "every fortnight": 14, + "fortnightly": 14, +} + + +class Source: + def __init__(self, id: int | None = None, uprn: str | int | None = None): + self._id: int | None = id + self._uprn: str | int | None = uprn + if id is None and uprn is None: + raise ValueError("This source cannot do anything without an id or uprn") + + self._bank_holday_move: dict[date, date] | None = None + + def fetch(self) -> list[Collection]: + entries = [] + if self._id is not None: + entries = self.fetch_regular() + if self._uprn is not None: + entries += self.fetch_recycling() + return entries + + def fetch_bank_holidays(self): + r = requests.get(RECYCLING_HOLYDAY_API_URL) + r.raise_for_status() + soup = BeautifulSoup(r.text, "html.parser") + table = soup.select_one("table#bank-holidays") + if not table: + raise ValueError("No bank holidays table found") + rows = table.select("tr") + councils = rows[0].select("th")[1:] + council_idx = None + for id, council in enumerate(councils): + if council.text.lower().strip().startswith("antrim and newtownabbey"): + council_idx = id + break + if council_idx is None: + raise ValueError("No council found") + + bank_holidays = {} + for row in rows[1:]: + cols = row.select("td") + date_td = cols[0] + date_ = None + for p in date_td.select("p"): + date_text = p.text.strip() + date_text = re.sub(REGEX_REMOVE_TH_ND_RD_ST, "", date_text) + try: + date_ = parse(date_text, default=datetime.now()).date() + if date_ < datetime.now().date(): + date_.replace(year=date_.year + 1) + break + except ValueError: + pass + if date_ is None: + raise ValueError("No date found") + + replace_date_td = cols[1 + council_idx] + if "No Collection" not in replace_date_td.text: + continue + raplace_date_str = replace_date_td.text.strip().split( + "Alternative collection" + )[1] + + try: + raplace_date = parse(raplace_date_str, default=datetime.now()).date() + if raplace_date < datetime.now().date(): + raplace_date.replace(year=raplace_date.year + 1) + bank_holidays[date_] = raplace_date + except ValueError: + raise + self._bank_holday_move = bank_holidays + + def fetch_recycling(self) -> list[Collection]: + if self._uprn is None: + raise ValueError("No uprn provided") + + if self._bank_holday_move is None: + try: + self.fetch_bank_holidays() + except Exception: + pass + params = { + "uprn": f"NI{self._uprn}", + "district": "newtownabbey", + "submit": "", + } + r = requests.get(RECYCLING_API_URL, params=params) + r.raise_for_status() + soup = BeautifulSoup(r.text, "html.parser") + + divs = soup.select("div.recyling-food") + entries: list[Collection] = [] + for div in divs: + bin_type_tag = div.select_one("h2") + if not bin_type_tag: + continue + bin_type = bin_type_tag.text.strip() + icon = ICON_MAP.get(bin_type) + + frequency_p = div.select_one("p") + frequency_days: int | None = None + if not frequency_p: + continue + frequency_strong = frequency_p.select_one("strong") + frequency = frequency_strong.text.strip() if frequency_strong else None + + if frequency and frequency.lower() in FREQUENCY_MAP: + frequency_days = FREQUENCY_MAP[frequency.lower()] + else: + if frequency: + _LOGGER.warning(f"Unknown frequency: {frequency}") + + next_date_p = frequency_p.find_next_sibling("p") + if not isinstance(next_date_p, Tag): + continue + next_date_strong = next_date_p.select_one("strong") + if not next_date_strong: + continue + next_date_str = next_date_strong.text.strip() + # Tuesday 13th August 2024 + next_date_str = re.sub(REGEX_REMOVE_TH_ND_RD_ST, "", next_date_str) + # Tuesday 13 August 2024 + next_date = datetime.strptime(next_date_str, "%A %d %B %Y").date() + moved_date = (self._bank_holday_move or {}).get(next_date, next_date) + + entries.append(Collection(date=moved_date, t=bin_type, icon=icon)) + if frequency_days: + for i in range(1, 20): + d = next_date + timedelta(days=frequency_days * i) + moved = (self._bank_holday_move or {}).get(d, d) + entries.append(Collection(date=moved, t=bin_type, icon=icon)) + return entries + + def fetch_regular(self) -> list[Collection]: + if self._id is None: + raise ValueError("No id provided") + + args = { + "Id": self._id, + "size": 20, + } + + r = requests.get(REGULAR_API_URL, params=args) + r.raise_for_status() + + soup = BeautifulSoup(r.text, "html.parser") + + collection_divs = soup.select("div.feature-box.bins") + if not collection_divs: + raise Exception("No collections found") + + entries = [] + for collection_div in collection_divs: + date_p = collection_div.select_one("p.date") + if not date_p: + continue + + # Thu 22 Aug, 2024 + date_ = datetime.strptime(date_p.text.strip(), "%a %d %b, %Y").date() + bins = collection_div.select("li") + if not bins: + continue + for bin in bins: + if not bin.text.strip(): + continue + bin_type = bin.text.strip() + icon = ICON_MAP.get(bin_type) + entries.append(Collection(date=date_, t=bin_type, icon=icon)) + return entries diff --git a/doc/source/antrimandnewtownabbey_gov_uk.md b/doc/source/antrimandnewtownabbey_gov_uk.md new file mode 100644 index 00000000..1d39d1aa --- /dev/null +++ b/doc/source/antrimandnewtownabbey_gov_uk.md @@ -0,0 +1,50 @@ +# Antrim and Newtownabbey + +Support for schedules provided by [Antrim and Newtownabbey](https://antrimandnewtownabbey.gov.uk), serving Antrim and Newtownabbey, UK. + +## Configuration via configuration.yaml + +```yaml +waste_collection_schedule: + sources: + - name: antrimandnewtownabbey_gov_uk + args: + id: ID + uprn: UPRN +``` + +### Configuration Variables + +**id** +*(Integer) (optional)* required for regular bin collection + +**uprn** +*(Integer) (optional)* required for recycling bin collection + +## Example + +```yaml +waste_collection_schedule: + sources: + - name: antrimandnewtownabbey_gov_uk + args: + id: 1456 # Required fore regular bin collection + uprn: 185354344 # Required for recycling bin collection + +``` + +## How to get the source argument + +### ID for regular bin collection + +Goto [https://antrimandnewtownabbey.gov.uk/residents/bins-recycling/bins-schedule/](https://antrimandnewtownabbey.gov.uk/residents/bins-recycling/bins-schedule/) and search for your address. Click on `View full bin schedule` you will now see the `id` in the URL. It's the number after `id=`. + +e.g. for the id is `1456`. + +### UPRN for recycling bin collection + +An easy way to discover your Unique Property Reference Number (UPRN) is by going to and entering in your address details. + +alternatively, you can find your uprn in the URL when searching your address here: + +e.g. if the URL is `https://www.brysonrecycling.org/northern-ireland/kerbside-collections/collection-day?district=newtownabbey&uprn=NI185354344&submit=` then the uprn is `185354344`. diff --git a/info.md b/info.md index fd148d53..ddbf9325 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, Boden, Borås Energi och Miljö, EDPEvent - Multi Source, 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, Mölndal, Norrtalje Vatten & Avfall, North / Middle Bohuslän - Rambo AB, Region Gotland, Ronneby Miljöteknik, Samverkan Återvinning Miljö (SÅM), Skellefteå, SRV Återvinning, SSAM (Deprecated), SSAM Södra Smalånds Avfall & Miljö, Sysav Sophämntning, Uppsala Vatten, Uppsala Vatten och Avfall AB (Deprecated), VA Syd Sophämntning, VIVAB Sophämtning, Västervik Miljö & Energi | | 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, Apps by imactivate, Ards and North Down Borough Council, Arun District Council, 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, Brent Council, Bristol City Council, Broadland District Council, Bromsgrove City 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, Charnwood, 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, Darlington Borough 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 Lothian, 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 Borough Council, FCC Environment, Fenland, Fenland District Council, Fife Council, Flintshire, Fylde Council, Gateshead Council, Gedling Borough Council (unofficial), Glasgow City Council, Guildford Borough Council, Gwynedd, Harborough District Council, Haringey Council, Harlow Council, Hart District Council, Herefordshire City Council, High Peak Borough Council, Highland, Horsham District Council, Hull City Council, Huntingdonshire District Council, iTouchVision, Joint Waste Solutions, Kirklees Council, Lancaster City Council, Leeds, 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, Luton, 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, Newark & Sherwood District 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, Rotherham Metropolitan Borough Council, Runnymede Borough Council, Rushcliffe Brough Council, Rushmoor Borough Council, Salford City Council, Sedgemoor District Council, Sheffield City Council, Shropshire Council, Solihull 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 Kesteven District Council, South Norfolk Council, South Oxfordshire District Council, South Somerset District Council, South Tyneside Council, Southampton City Council, St Albans City & District Council, Stafford Borough Council, Stevenage Borough Council, Stirling.gov.uk, Stockport Council, Stockton-on-Tees Borough Council, Stoke-on-Trent, Stratford District Council, Stroud 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 Glamorgan 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, Wychavon District Council, Wyre Forest District Council | +| United Kingdom | Aberdeenshire Council, Adur & Worthing Councils, Allerdale Borough Council, Amber Valley Borough Council, Anglesey, Antrim and Newtownabbey, Apps by imactivate, Ards and North Down Borough Council, Arun District Council, 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, Brent Council, Bristol City Council, Broadland District Council, Bromsgrove City 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, Charnwood, 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, Darlington Borough 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 Lothian, 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 Borough Council, FCC Environment, Fenland, Fenland District Council, Fife Council, Flintshire, Fylde Council, Gateshead Council, Gedling Borough Council (unofficial), Glasgow City Council, Guildford Borough Council, Gwynedd, Harborough District Council, Haringey Council, Harlow Council, Hart District Council, Herefordshire City Council, High Peak Borough Council, Highland, Horsham District Council, Hull City Council, Huntingdonshire District Council, iTouchVision, Joint Waste Solutions, Kirklees Council, Lancaster City Council, Leeds, 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, Luton, 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, Newark & Sherwood District 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, Rotherham Metropolitan Borough Council, Runnymede Borough Council, Rushcliffe Brough Council, Rushmoor Borough Council, Salford City Council, Sedgemoor District Council, Sheffield City Council, Shropshire Council, Solihull 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 Kesteven District Council, South Norfolk Council, South Oxfordshire District Council, South Somerset District Council, South Tyneside Council, Southampton City Council, St Albans City & District Council, Stafford Borough Council, Stevenage Borough Council, Stirling.gov.uk, Stockport Council, Stockton-on-Tees Borough Council, Stoke-on-Trent, Stratford District Council, Stroud 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 Glamorgan 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, Wychavon District 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 McKinney, 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, Waste Connections |