Skip to content

Commit

Permalink
Merge pull request #1062 from 5ila5/broxtowe_gov_uk
Browse files Browse the repository at this point in the history
adding source broxtowe_gov_uk
  • Loading branch information
5ila5 authored Jul 6, 2023
2 parents bf900b9 + 9affdbc commit f2a35d3
Show file tree
Hide file tree
Showing 4 changed files with 196 additions and 1 deletion.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -719,6 +719,7 @@ Waste collection schedules in the following formats and countries are supported.
- [Breckland Council](/doc/source/breckland_gov_uk.md) / breckland.gov.uk/mybreckland
- [Bristol City Council](/doc/source/bristol_gov_uk.md) / bristol.gov.uk
- [Broadland District Council](/doc/source/south_norfolk_and_broadland_gov_uk.md) / southnorfolkandbroadland.gov.uk
- [Broxtowe Borough Council](/doc/source/broxtowe_gov_uk.md) / broxtowe.gov.uk
- [Buckinghamshire Waste Collection - Former Chiltern, South Bucks or Wycombe areas](/doc/source/chiltern_gov_uk.md) / chiltern.gov.uk
- [Cambridge City Council](/doc/source/cambridge_gov_uk.md) / cambridge.gov.uk
- [Canterbury City Council](/doc/source/canterbury_gov_uk.md) / canterbury.gov.uk
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,143 @@
import requests
from waste_collection_schedule import Collection # type: ignore[attr-defined]
from bs4 import BeautifulSoup, NavigableString, Tag
import datetime


TITLE = "Broxtowe Borough Council"
DESCRIPTION = "Source for Broxtowe Borough Council."
URL = "https://www.broxtowe.gov.uk/"
TEST_CASES = {
"100031343805 NG9 2NL": {"uprn": 100031343805, "postcode": "NG9 2NL"},
"100031514955 NG9 4DU": {"uprn": " 100031308988", "postcode": "NG9 4DU"},
"U100031514955 NG9 4DU": {"uprn": "U100031308988 ", "postcode": "NG9 4DU"}

}


ICON_MAP = {
"BLACK": "mdi:trash-can",
"GLASS": "mdi:bottle-soda",
"GREEN": "mdi:leaf",
}


API_URL = "https://selfservice.broxtowe.gov.uk/renderform.aspx?t=217&k=9D2EF214E144EE796430597FB475C3892C43C528"


POSTCODE_ARGS = {
"ctl00$ScriptManager1": "ctl00$ContentPlaceHolder1$APUP_5683|ctl00$ContentPlaceHolder1$FF5683BTN",
"__EVENTTARGET": "ctl00$ContentPlaceHolder1$FF5683BTN",
"__ASYNCPOST": "true",
}

UPRN_ARGS = {
"ctl00$ScriptManager1": "ctl00$ContentPlaceHolder1$APUP_5683|ctl00$ContentPlaceHolder1$FF5683DDL",
"__EVENTTARGET": "ctl00$ContentPlaceHolder1$FF5683DDL",
"__ASYNCPOST": "true",
}

SUBMIT_ARGS = {
"__EVENTTARGET": "ctl00$ContentPlaceHolder1$btnSubmit",
}


class Source:
def __init__(self, uprn: str | int, postcode: str):
self._uprn: str = str(uprn).strip()
if self._uprn[0].upper() == "U":
self._uprn = self._uprn[1:].strip()

self._postcode: str = str(postcode)
self._uprn_args = UPRN_ARGS.copy()
self._postcode_args = POSTCODE_ARGS.copy()
self._submit_args = SUBMIT_ARGS.copy()
self._uprn_args["ctl00$ContentPlaceHolder1$FF5683DDL"] = f"U{self._uprn}"
self._postcode_args["ctl00$ContentPlaceHolder1$FF5683TB"] = f"{self._postcode}"

def __get_hidden_fiels(self, response: requests.Response, to_update: dict[str, str]):
response.raise_for_status()
r_list = response.text.split("|")

if r_list[1] == "error":
raise Exception("could not get valid data from ashford.gov.uk")

indexes = [index for index, value in enumerate(
r_list) if value == "hiddenField"]

for index in indexes:
key = r_list[index+1]
value = r_list[index+2]
to_update[key] = value

def fetch(self):
s = requests.Session()
headers = {"User-Agent": "Mozilla/5.0"}
s.headers.update(headers)

r = s.get(API_URL)
r.raise_for_status()

soup = BeautifulSoup(r.text, "html.parser")
for key in ["__VIEWSTATE", "__VIEWSTATEGENERATOR", "__EVENTVALIDATION"]:
search = soup.find(id=key)
if not search or not isinstance(search, Tag):
continue

self._postcode_args[key] = search.attrs["value"]

r = s.post(API_URL, data=self._postcode_args)
r.raise_for_status()

if "No addresses were found for the post code you entered." in r.text:
raise Exception(
"No addresses were found for the post code you entered.")

self.__get_hidden_fiels(r, self._uprn_args)

r = s.post(API_URL, data=self._uprn_args)
r.raise_for_status()

self.__get_hidden_fiels(r, self._submit_args)

self._submit_args["ctl00$ContentPlaceHolder1$btnSubmit"] = "ctl00$ContentPlaceHolder1$btnSubmit"
r = s.post(API_URL, data=self._submit_args)
r.raise_for_status()

if "No collection calendars are available for the selected property." in r.text:
raise Exception(
f"No collection calendars are available for the selected property. Make sure your address returns entries on the council website ({API_URL}).")

soup = BeautifulSoup(r.text, "html.parser")

table = soup.find("table", class_="bartec")

if table == None or isinstance(table, NavigableString):
raise Exception("could not get valid data from ashford.gov.uk")

entries = []
trs = table.find_all("tr")

if not trs or len(trs) < 2:
raise Exception("could not get valid data from ashford.gov.uk")

for row in trs[1:]:
bint_type = row.find("td")
if not bint_type:
continue

bint_type = bint_type.text

collections = row.find_all("td")
if not collections or len(collections) < 2:
continue
collections = collections[2:]

for collection in collections:
date = datetime.datetime.strptime(
collection.text, "%A, %d %B %Y").date()
icon = ICON_MAP.get(bint_type.split(" ")[0]) # Collection icon

entries.append(Collection(date=date, t=bint_type, icon=icon))

return entries
51 changes: 51 additions & 0 deletions doc/source/broxtowe_gov_uk.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
# Broxtowe Borough Council

Support for schedules provided by [Broxtowe Borough Council](https://www.broxtowe.gov.uk/), serving Broxtowe Borough Council, UK.

## Configuration via configuration.yaml

```yaml
waste_collection_schedule:
sources:
- name: broxtowe_gov_uk
args:
uprn: UPRN
postcode: POSTCODE

```
### Configuration Variables
**uprn**
*(String | Integer) (required)*
**postcode**
*(String) (required)*
## Example
```yaml
waste_collection_schedule:
sources:
- name: broxtowe_gov_uk
args:
uprn: 100031343805
postcode: NG9 2NL

```
## How to get the source argument
Use your postcode as postcode argument
### Get your UPRN using findmyaddress.co.uk
An easy way to find your Unique Property Reference Number (UPRN) is by going to <https://www.findmyaddress.co.uk/> and entering your address details.
### Get Your UPRN analysing browser traffic
- Fill out your postcode at https://selfservice.broxtowe.gov.uk/renderform.aspx?t=217&k=9D2EF214E144EE796430597FB475C3892C43C528 and press `Search`.
- Open your browser developer tools (in most browser `F12` or `right click -> inspect`) and open the `network` tab.
- Select your address.
- Your network tab should recieve a new request, open it.
- Select `request`. You should now see the argument `ctl00$ContentPlaceHolder1$FF5683DDL` containting your UPRN with a U as prefix (example: `ctl00$ContentPlaceHolder1$FF5683DDL: U100031343805` means your UPRN is `100031343805`).
2 changes: 1 addition & 1 deletion info.md
Original file line number Diff line number Diff line change
Expand Up @@ -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, Ashford Borough 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 Kingdom | Aberdeenshire Council, Amber Valley Borough Council, Ashfield District Council, Ashford Borough 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, Broxtowe Borough 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 | Albuquerque, New Mexico, USA, City of Oklahoma City, City of Pittsburgh, Louisville, Kentucky, USA, Newark, Delaware, USA, Olympia, Washington, USA, ReCollect, Recycle Coach, Republic Services, Seattle Public Utilities, Tucson, Arizona, USA |
<!--End of country section-->

Expand Down

0 comments on commit f2a35d3

Please sign in to comment.