Refactor fccenvironment_co_uk

This commit is contained in:
Chris Pressland
2022-12-27 17:13:42 +00:00
parent 28a8c8c64f
commit 9c350639a5
4 changed files with 132 additions and 53 deletions

View File

@@ -244,12 +244,14 @@ Waste collection schedules in the following formats and countries are supported.
- [Rushmoor Borough Council - rushmoor.gov.uk](/doc/source/rushmoor_gov_uk.md)
- [Sheffield City Council - sheffield.gov.uk](/doc/source/sheffield_gov_uk.md)
- [South Cambridgeshire District Council - scambs.gov.uk](/doc/source/scambs_gov_uk.md)
- [South Hams - southhams.gov.uk](https://southhams.gov.uk/)
- [South Norfolk and Broadland Council - southnorfolkandbroadland.gov.uk](/doc/source/south_norfolk_and_broadland_gov_uk.md)
- [Stevenage Borough Council - stevenage.gov.uk](/doc/source/stevenage_gov_uk.md)
- [Tewkwsbury Borough Council - tewkesbury.gov.uk](/doc/source/tewkesbury_gov_uk.md)
- [City of York Council - york.gov.uk](/doc/source/york_gov_uk.md)
- [Walsall Council - walsall.gov.uk](/doc/source/walsall_gov_uk.md)
- [West Berkshire Council - westberks.gov.uk](/doc/source/westberks_gov_uk.md)
- [West Devon - westdevon.gov.uk](https://www.westdevon.gov.uk/)
- [Wiltshire Council - wiltshire.gov.uk](/doc/source/wiltshire_gov_uk.md)
</p>
</details>

View File

@@ -1,74 +1,127 @@
import logging
import requests
from urllib.parse import urlparse
import requests
from bs4 import BeautifulSoup
from datetime import datetime
from dateutil import parser
from waste_collection_schedule import Collection
TITLE = "fccenvironment.co.uk"
DESCRIPTION = (
"""Consolidated source for waste collection services for ~60 local authorities.
Currently supports:
Market Harborough
"""
)
DESCRIPTION = """
Consolidated source for waste collection services for ~60 local authorities.
Currently supports:
West Devon (Generic Provider)
South Hams (Generic Provider)
Market Harborough (Custom Provider)
"""
URL = "https://fccenvironment.co.uk"
TEST_CASES = {
"Test_001" : {"uprn": "100030491624"},
"Test_002": {"uprn": "100030491614"},
"Test_003": {"uprn": "100030493289"},
"Test_004": {"uprn": "200001136341"}
"14_LE16_9QX": {"uprn": "100030491624"}, # region ommited to test default values
"4_LE16_9QX": {"uprn": "100030491614", "region": "harborough"},
"16_LE16_7NA": {"uprn": "100030493289", "region": "harborough"},
"10_LE16_8ER": {"uprn": "200001136341", "region": "harborough"},
"9_PL20_7SH": {"uprn": "10001326315", "region": "westdevon"},
"3_PL20_7RY": {"uprn": "10001326041", "region": "westdevon"},
"2_PL21_9BN": {"uprn": "100040279446", "region": "southhams"},
"4_SL21_0HZ": {"uprn": "100040281987", "region": "southhams"},
}
ICONS = {
"NON-RECYCLABLE WASTE BIN COLLECTION": "mdi:trash-can",
"RECYCLING COLLECTION": "mdi:recycle",
"GARDEN WASTE COLLECTION": "mdi:leaf",
"Refuse": "mdi:trash-can",
"Recycling": "mdi:recycle",
"Garden": "mdi:leaf",
}
_LOGGER = logging.getLogger(__name__)
class Source:
def __init__(self, uprn=None):
self._uprn = uprn
def __init__(self, uprn: str, region: str = "harborough") -> None:
self.uprn = uprn
self.region = region
def fetch(self):
def getcollectiondetails(self, endpoint: str) -> list[Collection]:
domain = urlparse(endpoint).netloc
session = requests.Session()
cookies = session.get(f"https://{domain}/")
response = session.post(
endpoint,
headers={
"x-requested-with": "XMLHttpRequest",
},
data={
"fcc_session_token": cookies.cookies["fcc_session_cookie"],
"uprn": self.uprn,
},
)
results = {}
for item in response.json()["binCollections"]["tile"]:
try:
soup = BeautifulSoup(item[0], "html.parser")
date = parser.parse(soup.find_all("b")[2].text.split(",")[1].strip()).date()
service = soup.text.split("\n")[0]
except parser._parser.ParserError:
continue
s = requests.Session()
if self._uprn:
# POST request returns schedule for matching uprn
payload = {
"Uprn": self._uprn
}
r = s.post("https://www.fccenvironment.co.uk/harborough/detail-address", data = payload)
responseContent = r.text
"""
Handle duplication before creating the list of Collections
"""
for type in ICONS:
if type in service:
if type in results.keys():
if date < results[type]:
results[type] = date
else:
results[type] = date
entries = []
# Extract waste types and dates from responseContent
soup = BeautifulSoup(responseContent, "html.parser")
services = soup.find("div", attrs={"class": "blocks block-your-next-scheduled-bin-collection-days"})
items = services.find_all("li")
for item in items:
date_text = item.find("span", attrs={"class": "pull-right"}).text.strip()
try:
date = datetime.strptime(date_text, "%d %B %Y").date()
except ValueError:
continue
else:
waste_type = item.text.split(' (')[0]
entries.append(
Collection(
date=date,
t=waste_type,
icon=ICONS.get(waste_type.upper()),
)
for result in results:
entries.append(
Collection(
date=results[result],
t=result,
icon=ICONS[result],
)
)
return entries
def harborough(self) -> list[Collection]:
_icons = {
"NON-RECYCLABLE WASTE BIN COLLECTION": "mdi:trash-can",
"RECYCLING COLLECTION": "mdi:recycle",
"GARDEN WASTE COLLECTION": "mdi:leaf",
} # Custom icons to avoid a breaking change
r = requests.post("https://www.fccenvironment.co.uk/harborough/detail-address", data={"Uprn": self.uprn})
soup = BeautifulSoup(r.text, "html.parser")
services = soup.find("div", attrs={"class": "blocks block-your-next-scheduled-bin-collection-days"}).find_all(
"li"
)
entries = []
for service in services:
for type in _icons:
if type.lower() in service.text.lower():
try:
date = parser.parse(service.find("span", attrs={"class": "pull-right"}).text.strip()).date()
except parser._parser.ParserError:
continue
entries.append(
Collection(
date=date,
t=type,
icon=_icons[type.upper()],
)
)
return entries
def fetch(self) -> list[Collection]:
if self.region == "harborough":
return self.harborough()
elif self.region == "westdevon":
return self.getcollectiondetails(
endpoint="https://westdevon.fccenvironment.co.uk/ajaxprocessor/getcollectiondetails"
)
elif self.region == "southhams":
return self.getcollectiondetails(
endpoint="https://waste.southhams.gov.uk/mycollections/getcollectiondetails"
)

View File

@@ -1,6 +1,9 @@
# FCC Environment
Consolidated support for schedules provided by ~60 local authorities. Currently supports [Harborough District Council](www.harborough.gov.uk)
Consolidated support for schedules provided by ~60 local authorities. Currently supports:
- [Harborough District Council](https://www.harborough.gov.uk/)
- [South Hams](https://southhams.gov.uk/)
- [West Devon](https://www.westdevon.gov.uk/)
## Configuration via configuration.yaml
@@ -10,6 +13,7 @@ waste_collection_schedule:
- name: fccenvironment_co_uk
args:
uprn: UNIQUE_PROPERTY_REFERENCE_NUMBER
region: REGION_NAME
```
### Configuration Variables
@@ -19,6 +23,15 @@ waste_collection_schedule:
This is required to unambiguously identify the property.
**region**<br>
*(string) (optional)*
Defaults to `harborough`, should be one of:
- `harborough`
- `westdevon`
- `southhams`
## Example using UPRN
```yaml
@@ -29,6 +42,17 @@ waste_collection_schedule:
uprn: 100030493289
```
## Example using UPRN and Region
```yaml
waste_collection_schedule:
sources:
- name: fccenvironment_co_uk
args:
uprn: 10001326041
region: westdevon
```
## How to find your `UPRN`
An easy way to find your Unique Property Reference Number (UPRN) is by going to <https://www.findmyaddress.co.uk/> and entering in your address details.

View File

@@ -30,6 +30,6 @@ Waste collection schedules from service provider web sites are updated daily, de
| Sweden | Lerum, Ronneby Miljöteknik, SSAM, Srvatervinning, Sysav, Vasyd |
| Switzerland | A-region, Lindau |
| USA | PGT.ST, Republic Services, Seattle Public Utilities |
| UK | Bracknell Forest Council, Bradford Metropolitan District Council, Braintree District Council, Cambridge City Council, Canterbury City Council, Cheshire East Council, Chesterfield Borough Council, Colchester Borough Council, Cornwall Council, Derby City Council, Eastbourne Borough Council, Elmbridge Borough Council, Guildford Borough Council, Harborough District Council, Huntingdonshire District Council, The Royal Borough of Kingston, Lewes District Council, London Borough of Lewisham, Manchester City Council, Newcastle City Council, North Somerset Council, Nottingham City Council, Peterborough City Council, Richmondshire District Council, Rushmoor Borough Council, Sheffield City Council, South Cambridgeshire District Council, South Norfolk and Broadland Council, Stevenage Borough Council, Tewkesbury Borough Council, City of York Council, Walsall Council, West Berkshire Council, Wiltshire Council |
| UK | Bracknell Forest Council, Bradford Metropolitan District Council, Braintree District Council, Cambridge City Council, Canterbury City Council, Cheshire East Council, Chesterfield Borough Council, Colchester Borough Council, Cornwall Council, Derby City Council, Eastbourne Borough Council, Elmbridge Borough Council, Guildford Borough Council, Harborough District Council, Huntingdonshire District Council, The Royal Borough of Kingston, Lewes District Council, London Borough of Lewisham, Manchester City Council, Newcastle City Council, North Somerset Council, Nottingham City Council, Peterborough City Council, Richmondshire District Council, Rushmoor Borough Council, Sheffield City Council, South Cambridgeshire District Council, South Hams, South Norfolk and Broadland Council, Stevenage Borough Council, Tewkesbury Borough Council, City of York Council, Walsall Council, West Berkshire Council, West Devon, Wiltshire Council |
For full details on supported service providers, and more project details, visit us on [GitHub](https://github.com/mampfes/hacs_waste_collection_schedule)