Skip to content

Commit

Permalink
Add Swale Borough Council (mampfes#3064)
Browse files Browse the repository at this point in the history
* Add Swale Borough Council

* reformatting

---------

Co-authored-by: 5ila5 <[email protected]>
  • Loading branch information
jamesonuk and 5ila5 authored Nov 21, 2024
1 parent 0cd8361 commit ffd9de0
Show file tree
Hide file tree
Showing 8 changed files with 260 additions and 1 deletion.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2166,6 +2166,7 @@ If your service provider is not listed, feel free to open a [source request issu
- [Sunderland City Council](/doc/source/sunderland_gov_uk.md) / sunderland.gov.uk
- [Surrey Heath Borough Council](/doc/source/jointwastesolutions_org.md) / surreyheath.gov.uk
- [Sutton Council, London](/doc/source/sutton_gov_uk.md) / sutton.gov.uk
- [Swale Borough Council](/doc/source/swale_gov_uk.md) / swale.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
Expand Down
6 changes: 6 additions & 0 deletions custom_components/waste_collection_schedule/sources.json
Original file line number Diff line number Diff line change
Expand Up @@ -13722,6 +13722,12 @@
"default_params": {},
"id": "sutton_gov_uk"
},
{
"title": "Swale Borough Council",
"module": "swale_gov_uk",
"default_params": {},
"id": "swale_gov_uk"
},
{
"title": "Swansea Council",
"module": "swansea_gov_uk",
Expand Down
25 changes: 25 additions & 0 deletions custom_components/waste_collection_schedule/translations/de.json
Original file line number Diff line number Diff line change
Expand Up @@ -15700,6 +15700,31 @@
},
"data_description": {}
},
"args_swale_gov_uk": {
"title": "Quelle konfigurieren",
"description": "Konfiguriere deinen Service Provider. \n\nYou can find your UPRN by visiting https://www.findmyaddress.co.uk/ and entering in your address details.\n\nMehr details: https://github.com/mampfes/hacs_waste_collection_schedule/blob/master/doc/source/swale_gov_uk.md",
"data": {
"calendar_title": "Kalender Titel",
"postcode": "PLZ",
"uprn": "UPRN"
},
"data_description": {
"calendar_title": "Ein lesbarerer oder benutzerfreundlicherer Name für den Müllkalender. Wenn nichts angegeben wird, wird der Name der Quelle verwendet.",
"uprn": "Eine einfache Möglichkeit, Ihre Unique Property Reference Number (UPRN) zu finden, besteht darin, auf https://www.findmyaddress.co.uk/ zu gehen und Ihre Adressdaten einzugeben."
}
},
"reconfigure_swale_gov_uk": {
"title": "Quelle Neu Konfigurieren",
"description": "Konfiguriere deinen Service Provider. \n\nYou can find your UPRN by visiting https://www.findmyaddress.co.uk/ and entering in your address details.\n\nMehr details: https://github.com/mampfes/hacs_waste_collection_schedule/blob/master/doc/source/swale_gov_uk.md",
"data": {
"calendar_title": "Kalender Titel",
"postcode": "PLZ",
"uprn": "UPRN"
},
"data_description": {
"uprn": "Eine einfache Möglichkeit, Ihre Unique Property Reference Number (UPRN) zu finden, besteht darin, auf https://www.findmyaddress.co.uk/ zu gehen und Ihre Adressdaten einzugeben."
}
},
"args_swansea_gov_uk": {
"title": "Quelle konfigurieren",
"description": "Konfiguriere deinen Service Provider. Mehr details: https://github.com/mampfes/hacs_waste_collection_schedule/blob/master/doc/source/swansea_gov_uk.md",
Expand Down
27 changes: 27 additions & 0 deletions custom_components/waste_collection_schedule/translations/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -15724,6 +15724,33 @@
},
"data_description": {}
},
"args_swale_gov_uk": {
"title": "Configure Source",
"description": "Configure your service provider. \n\nYou can find your UPRN by visiting https://www.findmyaddress.co.uk/ and entering in your address details.\n\nMore details: https://github.com/mampfes/hacs_waste_collection_schedule/blob/master/doc/source/swale_gov_uk.md.",
"data": {
"calendar_title": "Calendar Title",
"postcode": "Postcode",
"uprn": "UPRN"
},
"data_description": {
"calendar_title": "A more readable, or user-friendly, name for the waste calendar. If nothing is provided, the name returned by the source will be used.",
"postcode": "Postcode of the property",
"uprn": "Unique Property Reference Number (UPRN)"
}
},
"reconfigure_swale_gov_uk": {
"title": "Reconfigure Source",
"description": "Configure your service provider. \n\nYou can find your UPRN by visiting https://www.findmyaddress.co.uk/ and entering in your address details.\n\nMore details: https://github.com/mampfes/hacs_waste_collection_schedule/blob/master/doc/source/swale_gov_uk.md.",
"data": {
"calendar_title": "Calendar Title",
"postcode": "Postcode",
"uprn": "UPRN"
},
"data_description": {
"postcode": "Postcode of the property",
"uprn": "Unique Property Reference Number (UPRN)"
}
},
"args_swansea_gov_uk": {
"title": "Configure Source",
"description": "Configure your service provider. More details: https://github.com/mampfes/hacs_waste_collection_schedule/blob/master/doc/source/swansea_gov_uk.md.",
Expand Down
25 changes: 25 additions & 0 deletions custom_components/waste_collection_schedule/translations/it.json
Original file line number Diff line number Diff line change
Expand Up @@ -15689,6 +15689,31 @@
},
"data_description": {}
},
"args_swale_gov_uk": {
"title": "Configurazione Sorgente",
"description": "Compila i campi per ottenere le informazioni sul tuo servizio di raccolta. \n\nYou can find your UPRN by visiting https://www.findmyaddress.co.uk/ and entering in your address details.\n\nMaggiori informazioni: https://github.com/mampfes/hacs_waste_collection_schedule/blob/master/doc/source/swale_gov_uk.md.",
"data": {
"calendar_title": "Nome Calendario",
"postcode": "Codice Postale CAP",
"uprn": "UPRN"
},
"data_description": {
"calendar_title": "Puoi cambiare il nome del calendario della raccolta dei rifiuti, altrimenti di default verra' utilizzato il nome del tuo fornitore di servizi.",
"uprn": "Un modo facile per scoprire il tuo Numero di Riferimento Proprietà Unica (UPRN) è andare su https://www.findmyaddress.co.uk/ e inserire i dettagli del tuo indirizzo."
}
},
"reconfigure_swale_gov_uk": {
"title": "Riconfigurazione Sorgente",
"description": "Compila i campi per ottenere le informazioni sul tuo servizio di raccolta. \n\nYou can find your UPRN by visiting https://www.findmyaddress.co.uk/ and entering in your address details.\n\nPer maggiori informazioni: https://github.com/mampfes/hacs_waste_collection_schedule/blob/master/doc/source/swale_gov_uk.md.",
"data": {
"calendar_title": "Nome Calendario",
"postcode": "Codice Postale CAP",
"uprn": "UPRN"
},
"data_description": {
"uprn": "Un modo facile per scoprire il tuo Numero di Riferimento Proprietà Unica (UPRN) è andare su https://www.findmyaddress.co.uk/ e inserire i dettagli del tuo indirizzo."
}
},
"args_swansea_gov_uk": {
"title": "Configurazione Sorgente",
"description": "Compila i campi per ottenere le informazioni sul tuo servizio di raccolta. Maggiori informazioni: https://github.com/mampfes/hacs_waste_collection_schedule/blob/master/doc/source/swansea_gov_uk.md.",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
import json
import re

import requests
from bs4 import BeautifulSoup
from dateutil.parser import parse
from waste_collection_schedule import Collection # type: ignore[attr-defined]

TITLE = "Swale Borough Council"
DESCRIPTION = "Source for swale.gov.uk services for Swale, UK."
URL = "https://swale.gov.uk"
TEST_CASES = {
"Swale House": {"uprn": 100062375927, "postcode": "ME10 3HT"},
"1 Harrier Drive": {"uprn": 100061091726, "postcode": "ME10 4UY"},
}

HEADERS = {
"User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/50.0.2661.102 Safari/537.36",
}

API_URL = (
"https://swale.gov.uk/bins-littering-and-the-environment/bins/my-collection-day"
)

ICON_MAP = {
"Refuse": "mdi:trash-can",
"Recycling": "mdi:recycle",
"Food": "mdi:food-apple",
"Garden": "mdi:leaf",
}

HOW_TO_GET_ARGUMENTS_DESCRIPTION = {
"en": "You can find your UPRN by visiting https://www.findmyaddress.co.uk/ and entering in your address details.",
}
PARAM_DESCRIPTIONS = {
"en": {
"uprn": "Unique Property Reference Number (UPRN)",
"postcode": "Postcode of the property",
},
}

DATE_KEYS = {
"NextDateUTC",
"FollowingDateUTC",
"Following2DateUTC",
"Following3DateUTC",
}


class Source:
def __init__(self, uprn: str, postcode: str):
self._uprn = uprn
self._postcode = postcode

def fetch(self) -> list[Collection]:
entries = []

session = requests.Session()
session.headers.update(HEADERS)

first_form_data = {
"SQ_FORM_462397_PAGE": "1",
"q462406:q1": self._postcode,
"form_email_462397_submit": "Next &#10140;",
}

resp = session.post(API_URL, data=first_form_data)
resp.raise_for_status()

second_form_data = {
"SQ_FORM_462397_PAGE": "2",
"q462407:q3": self._uprn,
"form_email_462397_submit": "Get Bin Days &#10140;",
}

collection_response = session.post(API_URL, data=second_form_data)
collection_response.raise_for_status()

collection_soup = BeautifulSoup(collection_response.text, "html.parser")

section = collection_soup.find("section", id="SBC-YBD_main")
if not section:
raise ValueError(
"Could not find SBC-YBD_main section. Most likely html has changed"
)
script = section.find("script", recursive=False)
if not script:
raise ValueError(
"Could not find script entry. Most likely html has changed"
)

bin_data = re.search(
r"var BIN_DAYS = Object\.entries\(JSON\.parse\('(.+)'\)\);", script.string
)
if not bin_data:
raise ValueError(
"Could not find BIN_DAYS in response. Most likely html has changed"
)
bin_days = json.loads(bin_data.group(1))
for bin in bin_days:
bin_details = bin_days[bin]
if bin_details["Active"] == "Y":
for dateKey in DATE_KEYS:
if dateKey in bin_details:
entries.append(
Collection(
date=parse(bin_details[dateKey]).date(),
t=bin,
icon=ICON_MAP.get(bin),
)
)
if not entries:
raise ValueError(
"Could not get collections for the given combination of UPRN and Postcode."
)
return entries
59 changes: 59 additions & 0 deletions doc/source/swale_gov_uk.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
# Swale Borough Council

Support for schedules provided by [Swale Borough Council](https://swale.gov.uk/), in the UK.

## Configuration via configuration.yaml

```yaml
waste_collection_schedule:
sources:
- name: swale_gov_uk
args:
uprn: UNIQUE_PROPERTY_REFERENCE_NUMBER
postcode: POSTCODE
```

### Configuration Variables

**uprn**
*(string)*

The "Unique Property Reference Number" for your address. You can find it by searching for your address at <https://www.findmyaddress.co.uk/>.

**postcode**
*(string)*

The Post Code for your address. This needs to match the postcode corresponding to your UPRN.

## Example

```yaml
waste_collection_schedule:
sources:
- name: swale_gov_uk
args:
uprn: 100062375927
postcode: ME10 3HT
```

## Returned Collections

This source will return the next collection date for each container type.

## Returned collection types

### Refuse

Green bin is for general waste.

### Recycling

Blue bin is for recycling.

### Garden

Garden Waste.

### Food

Food bin
Loading

0 comments on commit ffd9de0

Please sign in to comment.