Skip to content

Commit

Permalink
New Source: North Norfolk, UK (#3281)
Browse files Browse the repository at this point in the history
* initial commit

* initial set-up

* test cases added

* test cases updated, tidy-up

* fix query rate issue

* tidy-up

* .md added

* update_docu_links

---------

Co-authored-by: 5ila5 <5ila5@users.noreply.github.com>
  • Loading branch information
dt215git and 5ila5 authored Dec 26, 2024
1 parent b19f0af commit 6d85718
Show file tree
Hide file tree
Showing 8 changed files with 245 additions and 1 deletion.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2146,6 +2146,7 @@ If your service provider is not listed, feel free to open a [source request issu
- [North Kesteven District Council](/doc/source/north_kesteven_org_uk.md) / n-kesteven.org.uk
- [North Lanarkshire Council](/doc/source/northlanarkshire_gov_uk.md) / northlanarkshire.gov.uk
- [North Lincolnshire Council](/doc/source/northlincs_gov_uk.md) / northlincs.gov.uk
- [North Norfolk District Council](/doc/source/north_norfolk_gov_uk.md) / north-norfolk.gov.uk
- [North Northamptonshire council](/doc/source/northnorthants_gov_uk.md) / northnorthants.gov.uk
- [North Somerset Council](/doc/source/nsomerset_gov_uk.md) / n-somerset.gov.uk
- [North West Leicestershire District Council](/doc/source/nwleics_gov_uk.md) / nwleics.gov.uk
Expand Down
6 changes: 6 additions & 0 deletions custom_components/waste_collection_schedule/sources.json
Original file line number Diff line number Diff line change
Expand Up @@ -13628,6 +13628,12 @@
"default_params": {},
"id": "northlincs_gov_uk"
},
{
"title": "North Norfolk District Council",
"module": "north_norfolk_gov_uk",
"default_params": {},
"id": "north_norfolk_gov_uk"
},
{
"title": "North Northamptonshire council",
"module": "northnorthants_gov_uk",
Expand Down
23 changes: 23 additions & 0 deletions custom_components/waste_collection_schedule/translations/de.json
Original file line number Diff line number Diff line change
Expand Up @@ -15059,6 +15059,29 @@
"uprn": "Eine einfache Möglichkeit, Ihre Unique Property Reference Number (UPRN) zu finden, besteht darin, auf https://www.findmyaddress.co.uk/ zu gehen und Ihre Adressdaten einzugeben."
}
},
"args_north_norfolk_gov_uk": {
"title": "Quelle konfigurieren",
"description": "Konfiguriere deinen Service Provider. \n\nan easy way to discover your Unique Property Reference Number (UPRN) is by going to https://www.findmyaddress.co.uk/ and entering in your address details.\n\nMehr details: https://github.com/mampfes/hacs_waste_collection_schedule/blob/master/doc/source/north_norfolk_gov_uk.md",
"data": {
"calendar_title": "Kalender Titel",
"uprn": "UPRN"
},
"data_description": {
"calendar_title": "Ein lesbarerer oder benutzerfreundlicherer Name für den Müllkalender. Wenn nichts angegeben wird, wird der Name der Quelle verwendet.",
"uprn": "Eine einfache Möglichkeit, Ihre Unique Property Reference Number (UPRN) zu finden, besteht darin, auf https://www.findmyaddress.co.uk/ zu gehen und Ihre Adressdaten einzugeben."
}
},
"reconfigure_north_norfolk_gov_uk": {
"title": "Quelle Neu Konfigurieren",
"description": "Konfiguriere deinen Service Provider. \n\nan easy way to discover your Unique Property Reference Number (UPRN) is by going to https://www.findmyaddress.co.uk/ and entering in your address details.\n\nMehr details: https://github.com/mampfes/hacs_waste_collection_schedule/blob/master/doc/source/north_norfolk_gov_uk.md",
"data": {
"calendar_title": "Kalender Titel",
"uprn": "UPRN"
},
"data_description": {
"uprn": "Eine einfache Möglichkeit, Ihre Unique Property Reference Number (UPRN) zu finden, besteht darin, auf https://www.findmyaddress.co.uk/ zu gehen und Ihre Adressdaten einzugeben."
}
},
"args_northnorthants_gov_uk": {
"title": "Quelle konfigurieren",
"description": "Konfiguriere deinen Service Provider. Mehr details: https://github.com/mampfes/hacs_waste_collection_schedule/blob/master/doc/source/northnorthants_gov_uk.md",
Expand Down
23 changes: 23 additions & 0 deletions custom_components/waste_collection_schedule/translations/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -15082,6 +15082,29 @@
"uprn": "An easy way to discover your Unique Property Reference Number (UPRN) is by going to https://www.findmyaddress.co.uk/ and entering in your address details."
}
},
"args_north_norfolk_gov_uk": {
"title": "Configure Source",
"description": "Configure your service provider. \n\nan easy way to discover your Unique Property Reference Number (UPRN) is by going to https://www.findmyaddress.co.uk/ and entering in your address details.\n\nMore details: https://github.com/mampfes/hacs_waste_collection_schedule/blob/master/doc/source/north_norfolk_gov_uk.md.",
"data": {
"calendar_title": "Calendar Title",
"uprn": "Unique Property Reference Number (UPRN)"
},
"data_description": {
"calendar_title": "A more readable, or user-friendly, name for the waste calendar. If nothing is provided, the name returned by the source will be used.",
"uprn": "Unique Property Reference Number (UPRN)"
}
},
"reconfigure_north_norfolk_gov_uk": {
"title": "Reconfigure Source",
"description": "Configure your service provider. \n\nan easy way to discover your Unique Property Reference Number (UPRN) is by going to https://www.findmyaddress.co.uk/ and entering in your address details.\n\nMore details: https://github.com/mampfes/hacs_waste_collection_schedule/blob/master/doc/source/north_norfolk_gov_uk.md.",
"data": {
"calendar_title": "Calendar Title",
"uprn": "Unique Property Reference Number (UPRN)"
},
"data_description": {
"uprn": "Unique Property Reference Number (UPRN)"
}
},
"args_northnorthants_gov_uk": {
"title": "Configure Source",
"description": "Configure your service provider. More details: https://github.com/mampfes/hacs_waste_collection_schedule/blob/master/doc/source/northnorthants_gov_uk.md.",
Expand Down
23 changes: 23 additions & 0 deletions custom_components/waste_collection_schedule/translations/it.json
Original file line number Diff line number Diff line change
Expand Up @@ -15041,6 +15041,29 @@
"uprn": "Un modo facile per scoprire il tuo Numero di Riferimento Proprietà Unica (UPRN) è andare su https://www.findmyaddress.co.uk/ e inserire i dettagli del tuo indirizzo."
}
},
"args_north_norfolk_gov_uk": {
"title": "Configurazione Sorgente",
"description": "Compila i campi per ottenere le informazioni sul tuo servizio di raccolta. \n\nan easy way to discover your Unique Property Reference Number (UPRN) is by going to https://www.findmyaddress.co.uk/ and entering in your address details.\n\nMaggiori informazioni: https://github.com/mampfes/hacs_waste_collection_schedule/blob/master/doc/source/north_norfolk_gov_uk.md.",
"data": {
"calendar_title": "Nome Calendario",
"uprn": "UPRN"
},
"data_description": {
"calendar_title": "Puoi cambiare il nome del calendario della raccolta dei rifiuti, altrimenti di default verra' utilizzato il nome del tuo fornitore di servizi.",
"uprn": "Un modo facile per scoprire il tuo Numero di Riferimento Proprietà Unica (UPRN) è andare su https://www.findmyaddress.co.uk/ e inserire i dettagli del tuo indirizzo."
}
},
"reconfigure_north_norfolk_gov_uk": {
"title": "Riconfigurazione Sorgente",
"description": "Compila i campi per ottenere le informazioni sul tuo servizio di raccolta. \n\nan easy way to discover your Unique Property Reference Number (UPRN) is by going to https://www.findmyaddress.co.uk/ and entering in your address details.\n\nPer maggiori informazioni: https://github.com/mampfes/hacs_waste_collection_schedule/blob/master/doc/source/north_norfolk_gov_uk.md.",
"data": {
"calendar_title": "Nome Calendario",
"uprn": "UPRN"
},
"data_description": {
"uprn": "Un modo facile per scoprire il tuo Numero di Riferimento Proprietà Unica (UPRN) è andare su https://www.findmyaddress.co.uk/ e inserire i dettagli del tuo indirizzo."
}
},
"args_northnorthants_gov_uk": {
"title": "Configurazione Sorgente",
"description": "Compila i campi per ottenere le informazioni sul tuo servizio di raccolta. Maggiori informazioni: https://github.com/mampfes/hacs_waste_collection_schedule/blob/master/doc/source/northnorthants_gov_uk.md.",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
import json
from datetime import date, datetime, timedelta
from time import sleep

import requests
from bs4 import BeautifulSoup
from waste_collection_schedule import Collection # type: ignore[attr-defined]

TITLE = "North Norfolk District Council"
DESCRIPTION = "Source for waste collection services for North Norfolk District Council"
URL = "https://www.north-norfolk.gov.uk/"

HEADERS = {"user-agent": "Mozilla/5.0"}

HOW_TO_GET_ARGUMENTS_DESCRIPTION = {
"en": "an easy way to discover your Unique Property Reference Number (UPRN) is by going to https://www.findmyaddress.co.uk/ and entering in your address details.",
}

PARAM_TRANSLATIONS = {
"en": {
"uprn": "Unique Property Reference Number (UPRN)",
}
}

PARAM_DESCRIPTIONS = {
"en": {
"uprn": "Unique Property Reference Number (UPRN)",
}
}

TEST_CASES = {
"Test_001": {
"uprn": "100090878875",
},
"Test_002": {
"uprn": 100090883974,
},
"Test_003": {
"uprn": "100090880632",
},
}

ICON_MAP = {
"Grey bin": "mdi:trash-can",
"Green bin": "mdi:recycle",
"Brown bin": "mdi:leaf",
}


class Source:
def __init__(self, uprn: str | int):
self._uprn = str(uprn)

def append_year(self, d: str) -> date:
# Website doesn't return the year.
# Append the current year, and then check to see if the date is in the past.
# If it is, increment the year by 1.
today: date = datetime.now().date()
year: int = today.year
dt: date = datetime.strptime(f"{d} {str(year)}", "%A %d %B %Y").date()
if (dt - today) < timedelta(days=-31):
dt = dt.replace(year=dt.year + 1)
return dt

def fetch(self):
sleep(1) # prevents test case failures due to query rate

s = requests.Session()

# visit homepage to get token for later queries
r = s.get(
"https://forms.north-norfolk.gov.uk/xforms/Address/Show/CollectionAddress",
headers=HEADERS,
)
soup: BeautifulSoup = BeautifulSoup(r.content, "html.parser")
token: str = soup.find("input", {"name": "__RequestVerificationToken"}).get(
"value"
)

payload: dict = {
"__RequestVerificationToken": token,
}

# use uprn to get address details
params: dict = {"uprn": self._uprn, "localAddress": "True"}
r = s.get(
"https://forms.north-norfolk.gov.uk/xforms/AddressSearch/GetAddressForUprn",
params=params,
headers=HEADERS,
)
r_json: dict = json.loads(r.content)

payload.update(
{
"SearchPostcode": r_json["postcode"],
"Address": r_json["uprn"],
"GisUprn": r_json["uprn"],
"GisUsrn": r_json["bS7666USRN"],
"GisTownName": r_json["townName"],
"GisPostTown": r_json["postTown"],
"GisPostCode": r_json["postcode"],
"GisAddress": r_json["locAddress1BS7666"],
"Address1": "",
"Address2": "",
"Address3": "",
"Address4": "",
"Postcode": "",
"LocalSearch": "True",
"DisableManualEntry": "True",
"ComponentMode": "False",
"IsDirty": "True",
}
)

# get collection schedule
r = s.post(
"https://forms.north-norfolk.gov.uk/xforms/Address/Show/CollectionAddress",
headers=HEADERS,
data=payload,
)

entries: list = []

soup = BeautifulSoup(r.content, "html.parser")
li: list = soup.find_all("li")
for item in li:
details: list = item.find_all("strong")
entries.append(
Collection(
date=self.append_year(details[2].text),
t=str(details[0].text),
icon=ICON_MAP.get(details[0].text),
)
)

return entries
32 changes: 32 additions & 0 deletions doc/source/north_norfolk_gov_uk.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# North Norfolk District Council

Support for schedules provided by [North Norfolk District Council](https://forms.north-norfolk.gov.uk/xforms/Address/Show/CollectionAddress), serving North Norfolk, UK.

## Configuration via configuration.yaml

```yaml
waste_collection_schedule:
sources:
- name: north_norfolk_gov_uk
args:
uprn: UNIQUE_PROPERTY_REFERENCE_NUMBER
```
### Configuration Variables
**uprn**
*(string) (required)*
## Example
```yaml
waste_collection_schedule:
sources:
- name: north_norfolk_gov_uk
args:
uprn: "100090880632"
```
## How to find your `UPRN`

An easy way to discover your Unique Property Reference Number (UPRN) is by going to <https://www.findmyaddress.co.uk/> and entering in your address details.
`
Loading

0 comments on commit 6d85718

Please sign in to comment.