add Renfrewshire council, UK (#2070)

* add Renfrewshire council, UK

* reforamtting + change URL to API_URL

---------

Co-authored-by: 5ila5 <5ila5@users.noreply.github.com>
Co-authored-by: 5ila5 <38183212+5ila5@users.noreply.github.com>
This commit is contained in:
Scott McGready
2024-05-08 21:54:01 +01:00
committed by GitHub
parent 22ee11d62a
commit e4c2532de2
4 changed files with 137 additions and 1 deletions

View File

@@ -1315,6 +1315,7 @@ Waste collection schedules in the following formats and countries are supported.
- [Reading Council](/doc/source/reading_gov_uk.md) / reading.gov.uk
- [Redbridge Council](/doc/source/redbridge_gov_uk.md) / redbridge.gov.uk
- [Reigate & Banstead Borough Council](/doc/source/reigatebanstead_gov_uk.md) / reigate-banstead.gov.uk
- [Renfrewshire Council](/doc/source/renfrewshire_gov_uk.md) / renfrewshire.gov.uk
- [Rhondda Cynon Taf County Borough Council](/doc/source/rctcbc_gov_uk.md) / rctcbc.gov.uk
- [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

View File

@@ -0,0 +1,98 @@
from urllib.parse import parse_qs, urlparse
import requests
from bs4 import BeautifulSoup
from dateutil import parser
from waste_collection_schedule import Collection # type: ignore[attr-defined]
TITLE = "Renfrewshire Council"
DESCRIPTION = "Source for renfrewshire.gov.uk services for Renfrewshire"
URL = "https://renfrewshire.gov.uk/"
API_URL = "https://www.renfrewshire.gov.uk/article/2320/Check-your-bin-collection-day"
TEST_CASES = {
"Test_001": {"postcode": "PA12 4JU", "uprn": 123033059},
"Test_002": {"postcode": "PA12 4AJ", "uprn": "123034174"},
"Test_003": {"postcode": "PA12 4EW", "uprn": "123033042"},
}
ICON_MAP = {
"Grey": "mdi:trash-can",
"Brown": "mdi:leaf",
"Green": "mdi:glass-fragile",
"Blue": "mdi:note",
}
class Source:
def __init__(self, postcode, uprn):
self._postcode = postcode
self._uprn = str(uprn)
def fetch(self):
session = requests.Session()
bin_collection_info_page = self.__get_bin_collection_info_page(
session, self._uprn, self._postcode
)
return self.__get_bin_collection_info(bin_collection_info_page)
def __get_goss_form_ids(self, url):
parsed_form_url = urlparse(url)
form_url_values = parse_qs(parsed_form_url.query)
return {
"page_session_id": form_url_values["pageSessionId"][0],
"session_id": form_url_values["fsid"][0],
"nonce": form_url_values["fsn"][0],
}
def __get_bin_collection_info_page(self, session, uprn, postcode):
r = session.get(API_URL)
r.raise_for_status()
soup = BeautifulSoup(r.text, "html.parser")
form = soup.find(id="RENFREWSHIREBINCOLLECTIONS_FORM")
goss_ids = self.__get_goss_form_ids(form["action"])
r = session.post(
form["action"],
data={
"RENFREWSHIREBINCOLLECTIONS_PAGESESSIONID": goss_ids["page_session_id"],
"RENFREWSHIREBINCOLLECTIONS_SESSIONID": goss_ids["session_id"],
"RENFREWSHIREBINCOLLECTIONS_NONCE": goss_ids["nonce"],
"RENFREWSHIREBINCOLLECTIONS_VARIABLES": "",
"RENFREWSHIREBINCOLLECTIONS_PAGENAME": "PAGE1",
"RENFREWSHIREBINCOLLECTIONS_PAGEINSTANCE": "0",
"RENFREWSHIREBINCOLLECTIONS_PAGE1_ADDRESSSTRING": "",
"RENFREWSHIREBINCOLLECTIONS_PAGE1_UPRN": uprn,
"RENFREWSHIREBINCOLLECTIONS_PAGE1_ADDRESSLOOKUPPOSTCODE": postcode,
"RENFREWSHIREBINCOLLECTIONS_PAGE1_NAVBUTTONS_NEXT": "Load Address",
},
)
r.raise_for_status()
return r.text
def __get_bin_collection_info(self, binformation):
soup = BeautifulSoup(binformation, "html.parser")
all_collections = soup.select(
"#RENFREWSHIREBINCOLLECTIONS_PAGE1_COLLECTIONDETAILS"
)
for collection in all_collections:
dates = collection.select("p.collection__date")
date_list = []
bin_list = []
for individualdate in dates:
date_list.append(parser.parse(individualdate.get_text()).date())
bins = collection.select("p.bins__name")
for individualbin in bins:
bin_list.append(individualbin.get_text().strip())
schedule = list(zip(date_list, bin_list))
entries = []
for sched_entry in schedule:
entries.append(
Collection(
date=sched_entry[0],
t=sched_entry[1],
icon=ICON_MAP.get(sched_entry[1]),
)
)
return entries

View File

@@ -0,0 +1,37 @@
# Renfrewshire Council
Support for schedules provided by [Renfrewshire Council](https://www.renfrewshire.gov.uk/article/2320/Check-your-bin-collection-day).
## Configuration via configuration.yaml
```yaml
waste_collection_schedule:
sources:
- name: renfrewshire_gov_uk
args:
postcode: POSTCODE
uprn: UNIQUE_PROPERTY_REFERENCE_NUMBER
```
### Configuration Variables
**postcode**
*(string) (required)*
**uprn**
*(string) (required)*
## Example
```yaml
waste_collection_schedule:
sources:
- name: renfrewshire_gov_uk
args:
postcode: "PA12 4AJ"
uprn: "123034174"
```
## 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 your address details.

File diff suppressed because one or more lines are too long