From 38038b53d0c1ad6391652e2dc9ea175fa018c90d Mon Sep 17 00:00:00 2001 From: Peter Barnes Date: Mon, 18 Dec 2023 17:20:26 +0000 Subject: [PATCH] added swansea_gov_uk source (#1527) * added swansea_gov_uk source * refactoring/reformatting --------- Co-authored-by: 5ila5 <5ila5@users.noreply.github.com> --- README.md | 1 + .../source/swansea_gov_uk.py | 109 ++++++++++++++++++ doc/source/swansea_gov_uk.md | 37 ++++++ info.md | 2 +- 4 files changed, 148 insertions(+), 1 deletion(-) create mode 100644 custom_components/waste_collection_schedule/waste_collection_schedule/source/swansea_gov_uk.py create mode 100644 doc/source/swansea_gov_uk.md diff --git a/README.md b/README.md index 5963f31c..2e9dea7b 100644 --- a/README.md +++ b/README.md @@ -1138,6 +1138,7 @@ Waste collection schedules in the following formats and countries are supported. - [Stoke-on-Trent](/doc/source/stoke_gov_uk.md) / stoke.gov.uk - [Stratford District Council](/doc/source/stratford_gov_uk.md) / stratford.gov.uk - [Surrey Heath Borough Council](/doc/source/jointwastesolutions_org.md) / surreyheath.gov.uk +- [Swansea Council](/doc/source/swansea_gov_uk.md) / swansea.gov.uk - [Swindon Borough Council](/doc/source/swindon_gov_uk.md) / swindon.gov.uk - [Tameside Metropolitan Borough Council](/doc/source/tameside_gov_uk.md) / tameside.gov.uk - [Telford and Wrekin Council](/doc/source/telford_gov_uk.md) / telford.gov.uk diff --git a/custom_components/waste_collection_schedule/waste_collection_schedule/source/swansea_gov_uk.py b/custom_components/waste_collection_schedule/waste_collection_schedule/source/swansea_gov_uk.py new file mode 100644 index 00000000..ae52df48 --- /dev/null +++ b/custom_components/waste_collection_schedule/waste_collection_schedule/source/swansea_gov_uk.py @@ -0,0 +1,109 @@ +import logging +from datetime import datetime + +import requests +from bs4 import BeautifulSoup +from waste_collection_schedule import Collection # type: ignore[attr-defined] + +TITLE = "Swansea Council" +DESCRIPTION = "Source script for swansea.gov.uk" +URL = "https://swansea.gov.uk" +TEST_CASES = { + "Cwmdonkin Drive": {"street_name": "cwmdonkin", "post_code": "SA20RA"}, + "Park Street": {"street_name": "Park Street", "post_code": "sa1 3dj"}, + "High Street": {"street_name": "High", "post_code": "SA1 1PE"}, + "St. Helen's": {"street_name": "St. Helen's", "post_code": "sa14nd"}, +} + +API_URL = "https://www1.swansea.gov.uk/recyclingsearch/" +ICON_MAP = { + "PINK": "mdi:trash-can", + "GREEN": "mdi:recycle", +} + +COLOR_MAP = [ # HTML Colour, Bin Type, Icon Mapping + ("MediumVioletRed", "Pink", "PINK"), + ("ForestGreen", "Green", "GREEN"), +] + +HEADERS = { + "user-agent": "Mozilla/5.0", +} + +_LOGGER = logging.getLogger(__name__) + + +class Source: + def __init__(self, street_name=None, post_code=None): + self._street_name = street_name + self._postcode = post_code + + def get_asp_var(self, broth, id) -> str: + asp_var = broth.find("input", {"id": id}).get("value") + return asp_var + + def fetch(self): + # Initiate a session to generate required ASP variables + session = requests.Session() + session.headers.update(HEADERS) + + response0 = session.get(API_URL) + response0.raise_for_status() + + soup = BeautifulSoup(response0.text, "html.parser") + + data = { + "__VIEWSTATE": self.get_asp_var(soup, "__VIEWSTATE"), + "__VIEWSTATEGENERATOR": self.get_asp_var(soup, "__VIEWSTATEGENERATOR"), + "__VIEWSTATEENCRYPTED": "", + "__EVENTVALIDATION": self.get_asp_var(soup, "__EVENTVALIDATION"), + "txtRoadName": self._street_name, + "txtPostCode": self._postcode, + "btnSearch": "Search", + } + + # Get the collection calendar + response1 = requests.post(API_URL, data=data) + response1.raise_for_status() + + soup = BeautifulSoup(response1.text, features="html.parser") + + tables = soup.find_all("table", {"title": "Calendar"}) + + entries = [] + + if not tables: # no tables in HTML means the address lookup failed + raise Exception("Address lookup failed") + + for table in tables: + month_year_text = ( + table.find("td", width="70%").find("b").text.strip() + ) # Extract month and year + + if not month_year_text: + raise Exception("Cannot find month and year in Calendar") + + for color, bin_type, icon in COLOR_MAP: + for td in table.find_all("td", bgcolor=color): + if not td: + raise Exception("Cannot find " + bin_type + " dates") + + try: + date = datetime.strptime( + f"{td.text.strip()} {month_year_text}", "%d %B %Y" + ).date() + except ValueError: + _LOGGER.warning( + f"Skipped day='{td.text.strip()}', month_day='{month_year_text}'. Unexpected format." + ) + continue + + entries.append( + Collection( + date=date, + t=bin_type, + icon=ICON_MAP.get(icon), + ) + ) + + return entries diff --git a/doc/source/swansea_gov_uk.md b/doc/source/swansea_gov_uk.md new file mode 100644 index 00000000..39ec3ad6 --- /dev/null +++ b/doc/source/swansea_gov_uk.md @@ -0,0 +1,37 @@ +# Swansea Council + +Support for schedules provided by [Swansea Council](https://swansea.gov.uk/), UK. + +If collection data is available for the address provided, it will return household and recycling waste collection dates. + + +## Configuration via configuration.yaml + +```yaml +waste_collection_schedule: + sources: + - name: swansea_gov_uk + args: + street_name: STREET_NAME + post_code: POST_CODE +``` + +### Configuration Variables + +**street_name** _(string) (required)_ : Any part of the road/street name +**post_code** _(string) (required)_ : Full postcode of the property + +## Example: + +```yaml +waste_collection_schedule: + sources: + - name: swansea_gov_uk + args: + street_name: "cwmdonkin" + post_code: "SA20RA" +``` + +## How to verify that your address works + +Visit [swansea.gov.uk/recyclingsearch](https://swansea.gov.uk/recyclingsearch) and search for your street name and postcode. \ No newline at end of file diff --git a/info.md b/info.md index 7fd886c7..be47058b 100644 --- a/info.md +++ b/info.md @@ -33,7 +33,7 @@ Waste collection schedules from service provider web sites are updated daily, de | Slovenia | Moji odpadki, Ljubljana | | Sweden | Affärsverken, 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 | | Switzerland | A-Region, Alchenstorf, Andwil, Appenzell, Berg, Bühler, 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, Basildon Council, Basingstoke and Deane Borough Council, Bath & North East Somerset Council, Bedford Borough Council, Binzone, 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, 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, Derby City Council, Dudley Metropolitan Borough Council, Durham County Council, East Cambridgeshire District Council, East Herts Council, East Northamptonshire and Wellingborough, East Renfrewshire Council, East Riding of Yorkshire Council, Eastbourne Borough Council, Elmbridge Borough Council, Environment First, Exeter City Council, Falkirk, Fareham Council, FCC Environment, Fenland District Council, Fife 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, Lisburn and Castlereagh City Council, Liverpool City Council, London Borough of Bexley, London Borough of Bromley, London Borough of Camden, London Borough of Lewisham, London Borough of Merton, Maidstone Borough Council, Maldon District Council, Manchester City Council, Mansfield District Council, Mendip District Council, Mid-Sussex District Council, Middlesbrough Council, Milton Keynes council, Newcastle City Council, Newcastle Under Lyme Borough Council, Newport City Council, North Herts Council, North Kesteven District Council, North Lincolnshire Council, North Somerset Council, North Yorkshire Council - Hambleton, Nottingham City Council, Oxford City Council, Peterborough City Council, Portsmouth City Council, Reading Council, Redbridge Council, Reigate & Banstead Borough 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, 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, Stevenage Borough Council, Stockport Council, Stockton-on-Tees Borough Council, Stoke-on-Trent, Stratford District Council, Surrey Heath Borough 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, 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, 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, Basildon Council, Basingstoke and Deane Borough Council, Bath & North East Somerset Council, Bedford Borough Council, Binzone, 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, 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, Derby City Council, Dudley Metropolitan Borough Council, Durham County Council, East Cambridgeshire District Council, East Herts Council, East Northamptonshire and Wellingborough, East Renfrewshire Council, East Riding of Yorkshire Council, Eastbourne Borough Council, Elmbridge Borough Council, Environment First, Exeter City Council, Falkirk, Fareham Council, FCC Environment, Fenland District Council, Fife 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, Lisburn and Castlereagh City Council, Liverpool City Council, London Borough of Bexley, London Borough of Bromley, London Borough of Camden, London Borough of Lewisham, London Borough of Merton, Maidstone Borough Council, Maldon District Council, Manchester City Council, Mansfield District Council, Mendip District Council, Mid-Sussex District Council, Middlesbrough Council, Milton Keynes council, Newcastle City Council, Newcastle Under Lyme Borough Council, Newport City Council, North Herts Council, North Kesteven District Council, North Lincolnshire Council, North Somerset Council, North Yorkshire Council - Hambleton, Nottingham City Council, Oxford City Council, Peterborough City Council, Portsmouth City Council, Reading Council, Redbridge Council, Reigate & Banstead Borough 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, 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, Stevenage Borough Council, Stockport Council, Stockton-on-Tees Borough Council, Stoke-on-Trent, Stratford District Council, Surrey Heath Borough Council, 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, 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, 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 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 |