added source rushcliffe_gov_uk

This commit is contained in:
5ila5
2023-06-13 20:05:05 +02:00
parent 009c7b050e
commit 7fa8b09717
4 changed files with 207 additions and 1 deletions

View File

@@ -777,6 +777,7 @@ Waste collection schedules in the following formats and countries are supported.
- [Richmondshire District Council](/doc/source/richmondshire_gov_uk.md) / richmondshire.gov.uk
- [Rotherham Metropolitan Borough Council](/doc/source/rotherham_gov_uk.md) / rotherham.gov.uk
- [Runnymede Borough Council](/doc/source/runnymede_gov_uk.md) / runnymede.gov.uk
- [Rushcliffe Brough Council](/doc/source/rushcliffe_gov_uk.md) / rushcliffe.gov.uk
- [Rushmoor Borough Council](/doc/source/rushmoor_gov_uk.md) / rushmoor.gov.uk
- [Salford City Council](/doc/source/salford_gov_uk.md) / salford.gov.uk
- [Sheffield City Council](/doc/source/sheffield_gov_uk.md) / sheffield.gov.uk

View File

@@ -0,0 +1,166 @@
import requests
from waste_collection_schedule import Collection # type: ignore[attr-defined]
from bs4 import BeautifulSoup
import re
import datetime
TITLE = "Rushcliffe Brough Council"
DESCRIPTION = "Source for Rushcliffe Brough Council."
URL = "https://www.rushcliffe.gov.uk/"
TEST_CASES = {
"NG12 5FE 2 Church Drive, Keyworth, NOTTINGHAM, NG12 5FE": {
"postcode": "NG12 5FE",
"address": "2 Church Drive, Keyworth, NOTTINGHAM, NG12 5FE"
}
}
ICON_MAP = {
"grey": "mdi:trash-can",
"garden waste": "mdi:leaf",
"blue": "mdi:package-variant",
}
API_URL = "https://selfservice.rushcliffe.gov.uk/renderform.aspx?t=1242&k=86BDCD8DE8D868B9E23D10842A7A4FE0F1023CCA"
POST_POST_CODE_KEY = "ctl00$ContentPlaceHolder1$FF3518TB"
POST_POST_UPRN_KEY = "ctl00$ContentPlaceHolder1$FF3518DDL"
POST_ARGS = [
{
"ctl00$ScriptManager1": "ctl00$ContentPlaceHolder1$APUP_3518|ctl00$ContentPlaceHolder1$FF3518BTN",
"__EVENTTARGET": "ctl00$ContentPlaceHolder1$FF3518BTN",
"__EVENTARGUMENT": "",
"__VIEWSTATEGENERATOR": "CA57E1E8",
"ctl00$ContentPlaceHolder1$txtPositionLL": "",
"ctl00$ContentPlaceHolder1$txtPosition": "",
"ctl00$ContentPlaceHolder1$FF3518TB": "", # Will be set later
"__ASYNCPOST": "true",
"": "",
},
{
"ctl00$ScriptManager1": "ctl00$ContentPlaceHolder1$APUP_3518|ctl00$ContentPlaceHolder1$FF3518DDL",
"ctl00$ContentPlaceHolder1$txtPositionLL": "",
"ctl00$ContentPlaceHolder1$txtPosition": "",
"ctl00$ContentPlaceHolder1$FF3518DDL": "", # Will be set later
"__EVENTTARGET": "ctl00$ContentPlaceHolder1$FF3518DDL",
"__EVENTARGUMENT": "",
"__LASTFOCUS": "",
"__VIEWSTATEGENERATOR": "CA57E1E8",
"__ASYNCPOST": "true",
"": "",
},
{
"ctl00$ContentPlaceHolder1$txtPositionLL": "",
"ctl00$ContentPlaceHolder1$txtPosition": "",
"__EVENTTARGET": "ctl00$ContentPlaceHolder1$btnSubmit",
"__EVENTARGUMENT": "",
"__LASTFOCUS": "",
"__VIEWSTATEGENERATOR": "CA57E1E8",
}
]
class Source:
def __init__(self, postcode: str, address: str):
self._postcode: str = postcode
self._address: str = address
def __compare(self, a: str, b: str) -> bool:
a = a.strip().replace(" ", "").replace(",", "")
b = b.strip().replace(" ", "").replace(",", "")
return a.lower() == b.lower() or a.lower().startswith(b.lower()) or b.lower().startswith(a.lower())
def __get_viewstate_and_validation(self, text: str) -> tuple[str, str]:
if "__VIEWSTATE" not in text or "__EVENTVALIDATION" not in text:
raise Exception("Invalid response")
text_arr = text.split("|")
viewstate_index = text_arr.index("__VIEWSTATE")
event_val_index = text_arr.index("__EVENTVALIDATION")
return (text_arr[viewstate_index+1], text_arr[event_val_index+1])
def fetch(self):
s = requests.Session()
header = {"User-Agent": "Mozilla/5.0",
"Host": "selfservice.rushcliffe.gov.uk"}
s.headers.update(header)
r = s.get(API_URL)
r.raise_for_status()
args: dict[str, str] = POST_ARGS[0]
args[POST_POST_CODE_KEY] = self._postcode
soup = BeautifulSoup(r.text, "html.parser")
viewstate = soup.find("input", {"name": "__VIEWSTATE"})
validation = soup.find("input", {"name": "__EVENTVALIDATION"})
if viewstate == None or not viewstate.get("value") or validation == None or not validation.get("value"):
raise Exception("Invalid response")
args["__VIEWSTATE"], args["__EVENTVALIDATION"] = viewstate.get(
'value'), validation.get('value')
r = s.post(API_URL, data=args)
r.raise_for_status()
args: dict[str, str] = POST_ARGS[1]
args["__VIEWSTATE"], args["__EVENTVALIDATION"] = self.__get_viewstate_and_validation(
r.text)
for update in r.text.split("|"):
if not update.startswith("<label"):
continue
soup = BeautifulSoup(update, "html.parser")
if len(soup.find_all("option")) < 2:
raise Exception("postcode not found")
for option in soup.find_all("option"):
if option["value"] in ("0", ""):
continue
if self.__compare(option.text, self._address):
args[POST_POST_UPRN_KEY] = option["value"]
break
if args[POST_POST_UPRN_KEY] == "":
raise Exception("Address not found")
r = s.post(API_URL, data=args)
r.raise_for_status()
args = POST_ARGS[2]
args["__VIEWSTATE"], args["__EVENTVALIDATION"] = self.__get_viewstate_and_validation(
r.text)
r = s.post(API_URL, data=args)
r.raise_for_status()
soup = BeautifulSoup(r.text, "html.parser")
panel = soup.find("div", {"class": "ss_confPanel well well-sm"})
if not panel:
raise Exception("No data found")
lines: str = str(panel).replace("<b>", "").replace("</b>", "")
entries = []
for line in lines.split("<br/>"):
line = line.strip()
if not line.startswith("Your"):
continue
bin_type = line[4:line.find("bin")].strip()
date = re.search(r"\d{2}/\d{2}/\d{4}", line)
if not date:
continue
date = date.group(0)
date = datetime.datetime.strptime(date, "%d/%m/%Y").date()
icon = ICON_MAP.get(bin_type) # Collection icon
entries.append(Collection(date=date, t=bin_type, icon=icon))
return entries

View File

@@ -0,0 +1,39 @@
# Rushcliffe Brough Council
Support for schedules provided by [Rushcliffe Brough Council](https://www.rushcliffe.gov.uk/), serving Rushcliffe Brough Council, UK.
## Configuration via configuration.yaml
```yaml
waste_collection_schedule:
sources:
- name: rushcliffe_gov_uk
args:
postcode: POSTCODE
address: ADDRESS
```
### Configuration Variables
**postcode**
*(String) (required)*
**address**
*(String) (required)*
## Example
```yaml
waste_collection_schedule:
sources:
- name: rushcliffe_gov_uk
args:
postcode: NG12 5FE
address: 2 Church Drive, Keyworth, NOTTINGHAM, NG12 5FE
```
## How to get the source argument
Find the parameter of your address using [https://selfservice.rushcliffe.gov.uk/renderform.aspx?t=1242&k=86BDCD8DE8D868B9E23D10842A7A4FE0F1023CCA](https://selfservice.rushcliffe.gov.uk/renderform.aspx?t=1242&k=86BDCD8DE8D868B9E23D10842A7A4FE0F1023CCA) and write them exactly like on the web page.

View File

@@ -30,7 +30,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, Basildon Council, Basingstoke and Deane Borough Council, Bath & North East Somerset Council, Bedford Borough Council, Binzone, Blackburn with Darwen Borough Council, Bracknell Forest Council, Bradford Metropolitan District Council, Braintree District Council, Breckland Council, Bristol City Council, Broadland District Council, Buckinghamshire Waste Collection - Former Chiltern, South Bucks or Wycombe areas, 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, Horsham District Council, Huntingdonshire District Council, 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, 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, Rushmoor Borough Council, Salford City Council, Sheffield City Council, South Cambridgeshire District Council, South Derbyshire District Council, South Gloucestershire Council, South Hams District Council, South Norfolk Council, South Oxfordshire District Council, South Tyneside Council, Southampton City Council, Stevenage Borough Council, Stockport Council, Stockton-on-Tees Borough Council, Swindon Borough Council, Telford and Wrekin 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, Waverley Borough Council, Wealden District Council, Welwyn Hatfield Borough Council, West Berkshire Council, West Devon Borough Council, West Dunbartonshire Council, Wigan Council, Wiltshire Council, Wirral Council, Wyre Forest District Council |
| United Kingdom | Aberdeenshire Council, Amber Valley Borough Council, Ashfield District Council, Basildon Council, Basingstoke and Deane Borough Council, Bath & North East Somerset Council, Bedford Borough Council, Binzone, Blackburn with Darwen Borough Council, Bracknell Forest Council, Bradford Metropolitan District Council, Braintree District Council, Breckland Council, Bristol City Council, Broadland District Council, Buckinghamshire Waste Collection - Former Chiltern, South Bucks or Wycombe areas, 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, Horsham District Council, Huntingdonshire District Council, 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, 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, Sheffield City Council, South Cambridgeshire District Council, South Derbyshire District Council, South Gloucestershire Council, South Hams District Council, South Norfolk Council, South Oxfordshire District Council, South Tyneside Council, Southampton City Council, Stevenage Borough Council, Stockport Council, Stockton-on-Tees Borough Council, Swindon Borough Council, Telford and Wrekin 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, Waverley Borough Council, Wealden District Council, Welwyn Hatfield Borough Council, West Berkshire Council, West Devon Borough Council, West Dunbartonshire Council, Wigan Council, Wiltshire Council, Wirral Council, Wyre Forest District Council |
| United States of America | City of Oklahoma City, City of Pittsburgh, ReCollect, Republic Services, Seattle Public Utilities |
<!--End of country section-->