feat: Add Newcastle-Upon-Tyne source

This commit is contained in:
Lewis England
2022-10-07 09:17:11 +01:00
parent 2d0d80adff
commit 983f8d6e83
2 changed files with 98 additions and 0 deletions

View File

@@ -0,0 +1,63 @@
import logging
from datetime import datetime
import re
import requests
# These lines are needed to suppress the InsecureRequestWarning resulting from the POST verify=False option
# With verify=True the POST fails due to a SSLCertVerificationError.
import urllib3
from waste_collection_schedule import Collection
urllib3.disable_warnings()
# The following links may provide a better way of dealing with this, as using verify=False is not ideal:
# https://urllib3.readthedocs.io/en/1.26.x/advanced-usage.html#ssl-warnings
# https://urllib3.readthedocs.io/en/1.26.x/user-guide.html#ssl
_LOGGER = logging.getLogger(__name__)
TITLE = "newcastle.gov.uk"
DESCRIPTION = (
"""Source for waste collection services for Newcastle City Council"""
)
URL = "https://community.newcastle.gov.uk/my-neighbourhood/ajax/getBinsNew.php"
REGEX = "<strong>(Green|Blue|Brown) [bB]in \\((Domestic|Recycling|Garden)( Waste)?\\) details: <\\/strong><br\\/>" \
"collection day : [a-zA-Z]*day<br\\/>" \
"Next collection : ([0-9]{2}-[A-Za-z]+-[0-9]{4})"
ICONS = {
"DOMESTIC": "mdi:trash-can",
"RECYCLING": "mdi:recycle",
"GARDEN": "mdi:leaf",
}
TEST_CASES = {
"Test_001": {"uprn": "004510053797"},
"Test_002": {"uprn": 4510053797}
}
class Source:
def __init__(self, uprn=None):
self._uprn = str(uprn).zfill(12)
if not self._uprn:
_LOGGER.error(
"uprn must be provided in config"
)
self._uprn = self._uprn.zfill(12)
self._session = requests.Session()
def fetch(self):
entries = []
res = requests.get(f"{URL}?uprn={self._uprn}")
collections = re.findall(REGEX, res.text)
for collection in collections:
collection_type = collection[1]
collection_date = collection[3]
entries.append(
Collection(
date=datetime.strptime(collection_date, '%d-%b-%Y').date(),
t=collection_type,
icon=ICONS.get(collection_type.upper()),
)
)
return entries

View File

@@ -0,0 +1,35 @@
# Newcastle City Council
Support for schedules provided by [Newcastle City Council](https://community.newcastle.gov.uk/my-neighbourhood/)
**Note**: Collection schedule will only show the next date, not all future dates
## Configuration via configuration.yaml
```yaml
waste_collection_schedule:
sources:
- name: newcastle_gov_uk
args:
uprn: UNIQUE_PROPERTY_REFERENCE_NUMBER
```
### Configuration Variables
**uprn**<br>
*(string) (required)*
This is required to unambiguously identify the property.
## Example using UPRN
```yaml
waste_collection_schedule:
sources:
- name: newcastle_gov_uk
args:
uprn: 74023685
```
## 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 in your address details.