diff --git a/README.md b/README.md index 1420c4a1a..f46d0ab2a 100644 --- a/README.md +++ b/README.md @@ -169,6 +169,7 @@ Currently the following service providers are supported: - [Lewes District Council - lewes-eastbourne.gov.uk](./doc/source/environmentfirst_co_uk.md) - [Harborough District Council - www.harborough.gov.uk](./doc/source/fccenvironment_co_uk.md) - [Huntingdonshire District Council - huntingdonshire.gov.uk](./doc/source/huntingdonshire_gov_uk.md) +- [Manchester City Council - manchester.gov.uk](./doc/source/manchester_uk.md) - [North Somerset Council - n-somerset.gov.uk](./doc/source/nsomerset_gov_uk.md) - [Nottingham City Council - nottinghamcity.gov.uk](./doc/source/nottingham_city_gov_uk.md) - [Peterborough City Council - peterborough.gov.uk](./doc/source/peterborough_gov_uk.md) diff --git a/custom_components/waste_collection_schedule/waste_collection_schedule/source/manchester_uk.py b/custom_components/waste_collection_schedule/waste_collection_schedule/source/manchester_uk.py new file mode 100644 index 000000000..a6c935e6d --- /dev/null +++ b/custom_components/waste_collection_schedule/waste_collection_schedule/source/manchester_uk.py @@ -0,0 +1,73 @@ +from datetime import datetime + +import requests +from waste_collection_schedule import Collection # type: ignore[attr-defined] + +from bs4 import BeautifulSoup +from urllib.parse import urlsplit, parse_qs +import logging + +TITLE = "manchester.gov.uk" +DESCRIPTION = "Source for bin collection services for Manchester City Council, UK." +URL = "https://www.manchester.gov.uk/bincollections/" +TEST_CASES = { + "domestic": {'uprn': '000077065560'}, +} + +ICONS = { + "Black / Grey 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: int = None + ): + self._uprn = uprn + if not self._uprn: + _LOGGER.error( + "uprn must be provided in config" + ) + self._session = requests.Session() + + def fetch(self): + entries = [] + + r = requests.post( + URL, + data={ + "mcc_bin_dates_uprn": self._uprn, + "mcc_bin_dates_submit": "Go" + }, + ) + + soup = BeautifulSoup(r.text, features="html.parser") + results = soup.find_all("div", {"class": "collection"}) + + for result in results: + date = result.find("p", {"class": "caption"}) + dates = [] + dates.append(str(date.text).replace("Next collection ", "", 1)) + for date in result.find_all('li'): + dates.append(date.text) + img_tag = result.find("img") + collection_type = img_tag["alt"] + for current_date in dates: + try: + date = datetime.strptime(current_date, "%A %d %b %Y").date() + entries.append( + Collection( + date=date, + t=collection_type, + icon=ICONS[collection_type], + ) + ) + except ValueError: + _LOGGER.error(f"Skipped {current_date} as it does not match time format") + + return entries diff --git a/doc/source/manchester_uk.md b/doc/source/manchester_uk.md new file mode 100644 index 000000000..0fdf99b63 --- /dev/null +++ b/doc/source/manchester_uk.md @@ -0,0 +1,38 @@ +# Manchester City Council + +Support for schedules provided by [Manchester City +Council](https://www.manchester.gov.uk/bincollections/), serving the +city of Manchester, UK. + +## Configuration via configuration.yaml + +```yaml +waste_collection_schedule: + sources: + - name: manchester_uk + args: + uprn: UPRN_CODE +``` + +### Configuration Variables + +**uprn**
+*(string) (required)* + +## Example + +```yaml +waste_collection_schedule: + sources: + - name: manchester_uk + args: + uprn: "100031540175" +``` + +## How to get the source argument + +The UPRN code can be found in the page by entering your postcode on the +[Manchester City Council Bin Collections page +](https://www.manchester.gov.uk/bincollections/). When on the address list, +View the source code for the page, and look for your address, the uprn will be +shown as the value. diff --git a/info.md b/info.md index 2515cc817..a1ee1a3e2 100644 --- a/info.md +++ b/info.md @@ -155,6 +155,7 @@ Currently the following service providers are supported: - [Lewes District Council - lewes-eastbourne.gov.uk](https://github.com/mampfes/hacs_waste_collection_schedule/blob/master/doc/source/environmentfirst_co_uk.md) - [Harborough District Council - www.harborough.gov.uk](https://github.com/mampfes/hacs_waste_collection_schedule/blob/master/doc/source/fccenvironment_co_uk.md) - [Huntingdonshire District Council - huntingdonshire.gov.uk](https://github.com/mampfes/hacs_waste_collection_schedule/blob/master/doc/source/huntingdonshire_gov_uk.md) +- [Manchester City Council - manchester.gov.uk](https://github.com/mampfes/hacs_waste_collection_schedule/blob/master/doc/source/manchester_uk.md) - [North Somerset Council - n-somerset.gov.uk](https://github.com/mampfes/hacs_waste_collection_schedule/blob/master/doc/source/nsomerset_gov_uk.md) - [Nottingham City Council - nottinghamcity.gov.uk](https://github.com/mampfes/hacs_waste_collection_schedule/blob/master/doc/source/nottingham_city_gov_uk.md) - [Peterborough City Council - peterborough.gov.uk](https://github.com/mampfes/hacs_waste_collection_schedule/blob/master/doc/source/peterborough_gov_uk.md)