diff --git a/README.md b/README.md index 565fc48de..ac9098f34 100644 --- a/README.md +++ b/README.md @@ -390,6 +390,7 @@ Waste collection schedules in the following formats and countries are supported. - [South Norfolk and Broadland Council](/doc/source/south_norfolk_and_broadland_gov_uk.md) / area.southnorfolkandbroadland.gov.uk - [Southampton City Council](/doc/source/southampton_gov_uk.md) / southampton.gov.uk - [Stevenage Borough Council](/doc/source/stevenage_gov_uk.md) / stevenage.gov.uk +- [Stockport Council](/doc/source/stockport_gov_uk.md) / stockport.gov.uk - [Telford and Wrekin Council](/doc/source/telford_gov_uk.md) / telford.gov.uk - [Tewkesbury Borough Council](/doc/source/tewkesbury_gov_uk.md) / tewkesbury.gov.uk - [The Royal Borough of Kingston Council](/doc/source/kingston_gov_uk.md) / kingston.gov.uk diff --git a/custom_components/waste_collection_schedule/waste_collection_schedule/source/stockport_gov_uk.py b/custom_components/waste_collection_schedule/waste_collection_schedule/source/stockport_gov_uk.py new file mode 100644 index 000000000..841272f5a --- /dev/null +++ b/custom_components/waste_collection_schedule/waste_collection_schedule/source/stockport_gov_uk.py @@ -0,0 +1,65 @@ +import logging +import re +from datetime import datetime + +import requests +from bs4 import BeautifulSoup +from waste_collection_schedule import Collection # type: ignore[attr-defined] + +TITLE = "Stockport Council" +DESCRIPTION = "Source for bin collection services for Stockport Council, UK.\n Refactored with thanks from the Manchester equivalent" +URL = "https://stockport.gov.uk" +TEST_CASES = { + "domestic": {"uprn": "100011460157"}, +} + +ICON_MAP = { + "Black bin": "mdi:trash-can", + "Blue bin": "mdi:recycle", + "Brown bin": "mdi:glass-fragile", + "Green bin": "mdi:leaf", +} + +_LOGGER = logging.getLogger(__name__) + + +class Source: + def __init__(self, uprn): + self._uprn = uprn + + def fetch(self): + + r = requests.get( + f"https://myaccount.stockport.gov.uk/bin-collections/show/{self._uprn}" + ) + + soup = BeautifulSoup(r.text, features="html.parser") + + bins = soup.find_all( + "div", {"class": re.compile("service-item service-item-.*")} + ) + + bins = str(bins) + header_string = "

" + bin_name_start_position = bins.find(header_string) + + entries = [] + while bin_name_start_position != -1: + bin_name_start_position += len(header_string) + bin_name_end_position = bins.find(" bin", bin_name_start_position) + 4 + bin_name = bins[bin_name_start_position:bin_name_end_position] + bin_date_pos = bins.find("

", bin_name_start_position) + bin_date_exc_day_of_week_pos = bins.find(", ", bin_date_pos) + 2 + bin_date_end_pos = bins.find("

", bin_date_exc_day_of_week_pos) + bin_date_string = bins[bin_date_exc_day_of_week_pos:bin_date_end_pos] + bin_date = datetime.strptime(bin_date_string, "%d %B %Y").date() + entries.append( + Collection( + date=bin_date, + t=bin_name, + icon=ICON_MAP.get(bin_name), + ) + ) + bin_name_start_position = bins.find(header_string, bin_date_end_pos) + + return entries diff --git a/doc/source/stockport_gov_uk.md b/doc/source/stockport_gov_uk.md new file mode 100644 index 000000000..5b27cb7b7 --- /dev/null +++ b/doc/source/stockport_gov_uk.md @@ -0,0 +1,49 @@ +# Stockport Metropolitan Borough Council + +Support for schedules provided by [Stockport Metropolitan Borough +Council](https://www.manchester.gov.uk/bincollections/), serving the +area of Stockport, UK. + +With thanks to the creator of the schedule for Manchester, UK, from +whom some of the code is re-factored. + +## Configuration via configuration.yaml + +```yaml +waste_collection_schedule: + sources: + - name: stockport_gov_uk + args: + uprn: UPRN_CODE +``` + +### Configuration Variables + +**uprn**
+*(string) (required)* + +## Example + +```yaml +waste_collection_schedule: + sources: + - name: stockport_gov_uk + args: + uprn: "10090543805" +``` + +## How to get the source argument + +The UPRN code can be found in the page by entering your postcode on the +[Stockport Bin Collections page +](https://www.stockport.gov.uk/find-your-collection-day/). + +Select your house from the drop-down, then the UPRN is the final part of +the URL that you are re-directed to. + +For example, for post-code SK6 3AA, house number 25, you are re-directed to + + +https://myaccount.stockport.gov.uk/bin-collections/show/10090543805 + +The UPRN is 10090543805 diff --git a/info.md b/info.md index e159f7998..41e911db2 100644 --- a/info.md +++ b/info.md @@ -28,7 +28,7 @@ Waste collection schedules from service provider web sites are updated daily, de | Poland | Ecoharmonogram, Warsaw | | Sweden | Landskrona - Svalövs Renhållning, Lerum Vatten och Avlopp, Ronneby Miljöteknik, SRV Återvinning, SSAM, Sysav Sophämntning, 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, Rehetobel, Rorschach, Rorschacherberg, Schwellbrunn, Schönengrund, Speicher, Stein, Steinach, Teufen, Thal, Trogen, Tübach, Untereggen, Urnäsch, Wald, Waldkirch, Waldstatt, Wittenbach, Wolfhalden | -| United Kingdom | Ashfield District Council, Bracknell Forest Council, Bradford Metropolitan District Council, Braintree District Council, Breckland Council, Cambridge City Council, Canterbury City Council, Central Bedfordshire Council, Cheshire East Council, Chesterfield Borough Council, City of York Council, Colchester City Council, Cornwall Council, Derby City Council, East Cambridgeshire District Council, East Herts Council, Eastbourne Borough Council, Elmbridge Borough Council, Environment First, FCC Environment, Guildford Borough Council, Harborough District Council, Harlow Council, Horsham District Council, Huntingdonshire District Council, Lewes District Council, London Borough of Lewisham, Manchester City Council, Middlesbrough Council, Newcastle City Council, Newcastle Under Lyme Borough Council, Newport City Council, North Somerset Council, Nottingham City Council, Peterborough City Council, Richmondshire District Council, Rushmoor Borough Council, Salford City Council, Sheffield City Council, South Cambridgeshire District Council, South Hams District Council, South Norfolk and Broadland Council, Southampton City Council, Stevenage Borough Council, Telford and Wrekin Council, Tewkesbury Borough Council, The Royal Borough of Kingston Council, Walsall Council, Waverley Borough Council, West Berkshire Council, West Devon Borough Council, Wiltshire Council, Wyre Forest District Council | +| United Kingdom | Ashfield District Council, Bracknell Forest Council, Bradford Metropolitan District Council, Braintree District Council, Breckland Council, Cambridge City Council, Canterbury City Council, Central Bedfordshire Council, Cheshire East Council, Chesterfield Borough Council, City of York Council, Colchester City Council, Cornwall Council, Derby City Council, East Cambridgeshire District Council, East Herts Council, Eastbourne Borough Council, Elmbridge Borough Council, Environment First, FCC Environment, Guildford Borough Council, Harborough District Council, Harlow Council, Horsham District Council, Huntingdonshire District Council, Lewes District Council, London Borough of Lewisham, Manchester City Council, Middlesbrough Council, Newcastle City Council, Newcastle Under Lyme Borough Council, Newport City Council, North Somerset Council, Nottingham City Council, Peterborough City Council, Richmondshire District Council, Rushmoor Borough Council, Salford City Council, Sheffield City Council, South Cambridgeshire District Council, South Hams District Council, South Norfolk and Broadland Council, Southampton City Council, Stevenage Borough Council, Stockport Council, Telford and Wrekin Council, Tewkesbury Borough Council, The Royal Borough of Kingston Council, Walsall Council, Waverley Borough Council, West Berkshire Council, West Devon Borough Council, Wiltshire Council, Wyre Forest District Council | | United States of America | City of Pittsburgh, Republic Services, Seattle Public Utilities |