From c67e9d5350e7dffe16eb43aa4d21d87f22e777a0 Mon Sep 17 00:00:00 2001 From: Sean Sabbage Date: Mon, 14 Aug 2023 11:41:05 +0100 Subject: [PATCH] Adding cardiff_gov_uk (#1147) * Adding source cardiff_gov_uk * Run black on cardiff source * reformatting + fixing md headline (@5ila5) --- README.md | 1 + .../source/cardiff_gov_uk.py | 108 ++++++++++++++++++ doc/source/cardiff_gov_uk.md | 35 ++++++ info.md | 2 +- 4 files changed, 145 insertions(+), 1 deletion(-) create mode 100644 custom_components/waste_collection_schedule/waste_collection_schedule/source/cardiff_gov_uk.py create mode 100644 doc/source/cardiff_gov_uk.md diff --git a/README.md b/README.md index 6f53d82c..5070b8a4 100644 --- a/README.md +++ b/README.md @@ -741,6 +741,7 @@ Waste collection schedules in the following formats and countries are supported. - [Burnley Council](/doc/source/burnley_gov_uk.md) / burnley.gov.uk - [Cambridge City Council](/doc/source/cambridge_gov_uk.md) / cambridge.gov.uk - [Canterbury City Council](/doc/source/canterbury_gov_uk.md) / canterbury.gov.uk +- [Cardiff Council](/doc/source/cardiff_gov_uk.md) / cardiff.gov.uk - [Central Bedfordshire Council](/doc/source/centralbedfordshire_gov_uk.md) / centralbedfordshire.gov.uk - [Cherwell District Council](/doc/source/cherwell_gov_uk.md) / cherwell.gov.uk - [Cheshire East Council](/doc/source/cheshire_east_gov_uk.md) / cheshireeast.gov.uk diff --git a/custom_components/waste_collection_schedule/waste_collection_schedule/source/cardiff_gov_uk.py b/custom_components/waste_collection_schedule/waste_collection_schedule/source/cardiff_gov_uk.py new file mode 100644 index 00000000..b65f2e76 --- /dev/null +++ b/custom_components/waste_collection_schedule/waste_collection_schedule/source/cardiff_gov_uk.py @@ -0,0 +1,108 @@ +import datetime +import json +import xml.etree.ElementTree as ET + +import requests +from waste_collection_schedule import Collection + +TITLE = "Cardiff Council" +DESCRIPTION = "Source script for cardiff.gov.uk" +URL = "https://cardiff.gov.uk" +TEST_CASES = { + "Glass": {"uprn": "100100124569"}, + "NoGlass": {"uprn": "100100127440"}, +} + +ICON_MAP = { + "General": "mdi:trash-can", + "Recycling": "mdi:recycle", + "Garden": "mdi:leaf", + "Food": "mdi:food", + "Glass": "mdi:glass-fragile", +} + +PAYLOAD_GET_JWT = ( + "" + "" + "" + "" + "" + "" +) + +URL_COLLECTIONS = "https://api.cardiff.gov.uk/WasteManagement/api/WasteCollection" +URL_GET_JWT = ( + "https://authwebservice.cardiff.gov.uk/AuthenticationWebService.asmx?op=GetJWT" +) + + +def get_headers() -> dict[str, str]: + """Return common headers that every request to the Cardiff API requires.""" + return { + "Origin": "https://www.cardiff.gov.uk", + "Referer": "https://www.cardiff.gov.uk/", + "User-Agent": "Mozilla/5.0", + } + + +def get_token() -> str: + """Get an access token.""" + headers = get_headers() + headers.update( + { + "Content-Type": 'text/xml; charset="UTF-8"', + } + ) + r = requests.post(URL_GET_JWT, headers=headers, data=PAYLOAD_GET_JWT) + r.raise_for_status() + + tree = ET.fromstring(r.text) + + jwt_result_element = tree.find( + ".//GetJWTResult", namespaces={"": "http://tempuri.org/"} + ) + + if jwt_result_element is None or jwt_result_element.text is None: + raise Exception("could not find Token") + jwt_result = jwt_result_element.text + + token = json.loads(jwt_result)["access_token"] + return token + + +class Source: + def __init__(self, uprn=None): + self._uprn = uprn + + def fetch(self): + payload_waste_collections = { + "systemReference": "web", + "language": "eng", + "uprn": self._uprn, + } + + entries = [] + + jwt = get_token() + client = requests.Session() + headers = get_headers() + headers.update({"Authorization": f"Bearer {jwt}"}) + + r = client.post( + URL_COLLECTIONS, headers=headers, json=payload_waste_collections + ) + r.raise_for_status() + collections = r.json() + + for week in collections["collectionWeeks"]: + for bin in week["bins"]: + entries.append( + Collection( + date=datetime.datetime.fromisoformat(week["date"]).date(), + t=bin["type"], + icon=ICON_MAP.get(bin["type"]), + ) + ) + return entries diff --git a/doc/source/cardiff_gov_uk.md b/doc/source/cardiff_gov_uk.md new file mode 100644 index 00000000..2fff4921 --- /dev/null +++ b/doc/source/cardiff_gov_uk.md @@ -0,0 +1,35 @@ +# Cardiff Council + +Support for schedules provided by [Cardiff Council](https://www.cardiff.gov.uk/ENG/resident/Rubbish-and-recycling/When-are-my-bins-collected/Pages/default.aspx), serving Cardiff (UK). + +With many thanks to [Tom Brien](https://github.com/TomBrien) whose [existing work](https://github.com/TomBrien/cardiffwastepy) was used as the base for this. + +## Configuration via configuration.yaml + +```yaml +waste_collection_schedule: + sources: + - name: cardiff_gov_uk + args: + uprn: UPRN + +``` + +### Configuration Variables + +**UPRN** +*(string) (required)* + +## Example + +```yaml +waste_collection_schedule: + sources: + - name: cardiff_gov_uk + args: + uprn: "100100124569" +``` + +## How to find your `UPRN` + +An easy way to discover your Unique Property Reference Number (UPRN) is by going to https://www.findmyaddress.co.uk/ and entering in your address details. \ No newline at end of file diff --git a/info.md b/info.md index 223406b3..67fd65fb 100644 --- a/info.md +++ b/info.md @@ -31,7 +31,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, 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, Andwil, Appenzell, Berg, Bühler, Eggersriet, Gais, Gaiserwald, Goldach, Grosswangen, Grub, Heiden, Herisau, Horn, Hundwil, Häggenschwil, Lindau, Lutzenberg, Muolen, Mörschwil, Münchenstein, 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, Amber Valley Borough Council, Ashfield District Council, Ashford Borough Council, Basildon Council, Basingstoke and Deane Borough Council, Bath & North East Somerset Council, Bedford Borough Council, Binzone, Blackburn with Darwen Borough 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, Central Bedfordshire Council, Cherwell District Council, Cheshire East Council, Chesterfield Borough Council, Chichester District Council, City of Doncaster Council, City of York Council, Colchester City Council, Cornwall Council, Croydon Council, Derby City Council, East Cambridgeshire District Council, East Herts Council, East Northamptonshire and Wellingborough, East Riding of Yorkshire Council, Eastbourne Borough Council, Elmbridge Borough Council, Environment First, Exeter City Council, Fareham Council, FCC Environment, Fenland District Council, Fife Council, Gateshead Council, Glasgow City Council, Guildford Borough Council, Harborough District Council, Harlow Council, Herefordshire City Council, Highland, Horsham District Council, Huntingdonshire District Council, iTouchVision, Joint Waste Solutions, Kirklees 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 Lewisham, London Borough of Merton, Maidstone Borough Council, Maldon District Council, Manchester City Council, Mendip District Council, Mid-Sussex District Council, Middlesbrough 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, Nottingham City Council, Oxford City Council, Peterborough City Council, Portsmouth City Council, Reading Council, Redbridge Council, Reigate & Banstead 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, Stratford District Council, Surrey Heath Borough Council, Swindon Borough Council, Telford and Wrekin Council, Test Valley Borough Council, Tewkesbury Borough Council, The Royal Borough of Kingston Council, Tonbridge and Malling Borough Council, Uttlesford District Council, Vale of White Horse District Council, Walsall Council, Warrington Borough 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, Wyre Forest District Council | +| United Kingdom | Aberdeenshire Council, Amber Valley Borough Council, Ashfield District Council, Ashford Borough Council, Basildon Council, Basingstoke and Deane Borough Council, Bath & North East Somerset Council, Bedford Borough Council, Binzone, Blackburn with Darwen Borough 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, Chesterfield Borough Council, Chichester District Council, City of Doncaster Council, City of York Council, Colchester City Council, Cornwall Council, Croydon Council, Derby City Council, East Cambridgeshire District Council, East Herts Council, East Northamptonshire and Wellingborough, East Riding of Yorkshire Council, Eastbourne Borough Council, Elmbridge Borough Council, Environment First, Exeter City Council, Fareham Council, FCC Environment, Fenland District Council, Fife Council, Gateshead Council, Glasgow City Council, Guildford Borough Council, Harborough District Council, Harlow Council, Herefordshire City Council, Highland, Horsham District Council, Huntingdonshire District Council, iTouchVision, Joint Waste Solutions, Kirklees 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 Lewisham, London Borough of Merton, Maidstone Borough Council, Maldon District Council, Manchester City Council, Mendip District Council, Mid-Sussex District Council, Middlesbrough 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, Nottingham City Council, Oxford City Council, Peterborough City Council, Portsmouth City Council, Reading Council, Redbridge Council, Reigate & Banstead 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, Stratford District Council, Surrey Heath Borough Council, Swindon Borough Council, Telford and Wrekin Council, Test Valley Borough Council, Tewkesbury Borough Council, The Royal Borough of Kingston Council, Tonbridge and Malling Borough Council, Uttlesford District Council, Vale of White Horse District Council, Walsall Council, Warrington Borough 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, Wyre Forest District Council | | United States of America | Albuquerque, New Mexico, USA, 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 |