From e79ce9b2861775f1e6d15e3db7d7bf4035c69a2a Mon Sep 17 00:00:00 2001 From: m26dvd <31007572+m26dvd@users.noreply.github.com> Date: Sun, 27 Oct 2024 23:08:37 +0000 Subject: [PATCH] feat: Adding Fife Council fix: #933 --- uk_bin_collection/tests/input.json | 7 ++ .../uk_bin_collection/councils/FifeCouncil.py | 68 +++++++++++++++++++ wiki/Councils.md | 37 ++++++++-- 3 files changed, 108 insertions(+), 4 deletions(-) create mode 100644 uk_bin_collection/uk_bin_collection/councils/FifeCouncil.py diff --git a/uk_bin_collection/tests/input.json b/uk_bin_collection/tests/input.json index 4fa68896dc..2507f31f55 100644 --- a/uk_bin_collection/tests/input.json +++ b/uk_bin_collection/tests/input.json @@ -484,6 +484,13 @@ "url": "https://www.fenland.gov.uk/article/13114/", "wiki_name": "Fenland District Council" }, + "FifeCouncil": { + "url": "https://www.fife.gov.uk", + "wiki_command_url_override": "https://www.fife.gov.uk", + "uprn": "320203521", + "wiki_name": "Fife Council", + "wiki_note": "You will need to use [FindMyAddress](https://www.findmyaddress.co.uk/search) to find the UPRN." + }, "FlintshireCountyCouncil": { "url": "https://digital.flintshire.gov.uk", "wiki_command_url_override": "https://digital.flintshire.gov.uk", diff --git a/uk_bin_collection/uk_bin_collection/councils/FifeCouncil.py b/uk_bin_collection/uk_bin_collection/councils/FifeCouncil.py new file mode 100644 index 0000000000..cbf04f347a --- /dev/null +++ b/uk_bin_collection/uk_bin_collection/councils/FifeCouncil.py @@ -0,0 +1,68 @@ +from datetime import datetime + +import requests +from bs4 import BeautifulSoup + +from uk_bin_collection.uk_bin_collection.common import * +from uk_bin_collection.uk_bin_collection.get_bin_data import AbstractGetBinDataClass + + +class CouncilClass(AbstractGetBinDataClass): + """ + Concrete classes have to implement all abstract operations of the + base class. They can also override some operations with a default + implementation. + """ + + def parse_data(self, page: str, **kwargs) -> dict: + # Get and check UPRN + user_uprn = kwargs.get("uprn") + check_uprn(user_uprn) + bindata = {"bins": []} + + API_URL = "https://www.fife.gov.uk/api/custom?action=powersuite_bin_calendar_collections&actionedby=bin_calendar&loadform=true&access=citizen&locale=en" + AUTH_URL = "https://www.fife.gov.uk/api/citizen?preview=false&locale=en" + AUTH_KEY = "Authorization" + + r = requests.get(AUTH_URL) + r.raise_for_status() + auth_token = r.headers[AUTH_KEY] + + post_data = { + "name": "bin_calendar", + "data": { + "uprn": user_uprn, + }, + "email": "", + "caseid": "", + "xref": "", + "xref1": "", + "xref2": "", + } + + headers = { + "referer": "https://www.fife.gov.uk/services/forms/bin-calendar", + "accept": "application/json", + "content-type": "application/json", + AUTH_KEY: auth_token, + } + + r = requests.post(API_URL, data=json.dumps(post_data), headers=headers) + r.raise_for_status() + + result = r.json() + + for collection in result["data"]["tab_collections"]: + dict_data = { + "type": collection["colour"], + "collectionDate": datetime.strptime( + collection["date"], + "%A, %B %d, %Y", + ).strftime("%d/%m/%Y"), + } + bindata["bins"].append(dict_data) + + bindata["bins"].sort( + key=lambda x: datetime.strptime(x.get("collectionDate"), "%d/%m/%Y") + ) + return bindata diff --git a/wiki/Councils.md b/wiki/Councils.md index 8244d8e058..e78eb2732d 100644 --- a/wiki/Councils.md +++ b/wiki/Councils.md @@ -77,6 +77,8 @@ This document is still a work in progress, don't worry if your council isn't lis - [Falkirk Council](#falkirk-council) - [Fareham Borough Council](#fareham-borough-council) - [Fenland District Council](#fenland-district-council) +- [Fife Council](#fife-council) +- [Flintshire County Council](#flintshire-county-council) - [Forest of Dean District Council](#forest-of-dean-district-council) - [Gateshead Council](#gateshead-council) - [Gedling Borough Council](#gedling-borough-council) @@ -996,6 +998,28 @@ Additional parameters: --- +### Fife Council +```commandline +python collect_data.py FifeCouncil https://www.fife.gov.uk -u XXXXXXXX +``` +Additional parameters: +- `-u` - UPRN + +Note: You will need to use [FindMyAddress](https://www.findmyaddress.co.uk/search) to find the UPRN. + +--- + +### Flintshire County Council +```commandline +python collect_data.py FlintshireCountyCouncil https://digital.flintshire.gov.uk -u XXXXXXXX +``` +Additional parameters: +- `-u` - UPRN + +Note: You will need to use [FindMyAddress](https://www.findmyaddress.co.uk/search) to find the UPRN. + +--- + ### Forest of Dean District Council ```commandline python collect_data.py ForestOfDeanDistrictCouncil https://community.fdean.gov.uk/s/waste-collection-enquiry -s -p "XXXX XXX" -n XX -w http://HOST:PORT/ @@ -1421,10 +1445,14 @@ Note: Pass the house name/number plus the name of the street with the postcode p ### Midlothian Council ```commandline -python collect_data.py MidlothianCouncil https://www.midlothian.gov.uk/directory_record/XXXXXX/XXXXXX +python collect_data.py MidlothianCouncil https://www.midlothian.gov.uk/info/1054/bins_and_recycling/343/bin_collection_days -s -p "XXXX XXX" -n XX ``` +Additional parameters: +- `-s` - skip get URL +- `-p` - postcode +- `-n` - house number -Note: Follow the instructions [here](https://www.midlothian.gov.uk/info/1054/bins_and_recycling/343/bin_collection_days) until you get the page that shows the weekly collections for your address then copy the URL and replace the URL in the command. +Note: Pass the house name/number wrapped in double quotes along with the postcode parameter --- @@ -2027,7 +2055,7 @@ Additional parameters: ### Southwark Council ```commandline -python collect_data.py SouthwarkCouncil https://www.southwark.gov.uk/bins/lookup/XXXXXXXX -u XXXXXXXX +python collect_data.py SouthwarkCouncil https://services.southwark.gov.uk/bins/lookup/XXXXXXXX -u XXXXXXXX ``` Additional parameters: - `-u` - UPRN @@ -2191,10 +2219,11 @@ Additional parameters: ### Teignbridge Council ```commandline -python collect_data.py TeignbridgeCouncil https://www.google.co.uk -u XXXXXXXX +python collect_data.py TeignbridgeCouncil https://www.google.co.uk -u XXXXXXXX -w http://HOST:PORT/ ``` Additional parameters: - `-u` - UPRN +- `-w` - remote Selenium web driver URL (required for Home Assistant) Note: Provide Google as the URL as the real URL breaks the integration. You will need to use [FindMyAddress](https://www.findmyaddress.co.uk/search) to find the UPRN.