mirror of
https://github.com/sascha-hemi/hacs_waste_collection_schedule.git
synced 2026-03-21 03:04:09 +01:00
refactor breckland_gov_uk
This commit is contained in:
@@ -1,94 +1,82 @@
|
||||
import logging
|
||||
import requests
|
||||
from datetime import datetime
|
||||
|
||||
import requests
|
||||
from waste_collection_schedule import Collection
|
||||
|
||||
TITLE = "Breckland Council, UK"
|
||||
DESCRIPTION = "Source for breckland.gov.uk"
|
||||
URL = "https://www.breckland.gov.uk/mybreckland"
|
||||
TEST_CASES = {
|
||||
"test1" : {"postcode":"IP22 2LJ","address":"glen travis" },
|
||||
"test2" : {"uprn":"10011977093"},
|
||||
"test1": {"postcode": "IP22 2LJ", "address": "glen travis"},
|
||||
"test2": {"uprn": "10011977093"},
|
||||
}
|
||||
|
||||
API_URL = "https://www.breckland.gov.uk/apiserver/ajaxlibrary"
|
||||
ICON_MAP = {
|
||||
"Refuse Collection Service": "mdi:trash-can",
|
||||
"Recycling Collection Service": "mdi:recycle",
|
||||
"Garden Waste Service": "mdi:leaf",
|
||||
"Refuse Collection Service": "mdi:trash-can",
|
||||
"Recycling Collection Service": "mdi:recycle",
|
||||
"Garden Waste Service": "mdi:leaf",
|
||||
}
|
||||
|
||||
_LOGGER = logging.getLogger(__name__)
|
||||
|
||||
headers = {'referer': URL}
|
||||
headers = {"referer": URL}
|
||||
|
||||
|
||||
class Source:
|
||||
def __init__(self,postcode=None,address=None,uprn=None):
|
||||
def __init__(self, postcode=None, address=None, uprn=None):
|
||||
self._postcode = postcode
|
||||
self._address = address
|
||||
self._uprn = uprn
|
||||
|
||||
if postcode is None and address is None and uprn is None:
|
||||
raise Exception("no attributes - specify postcode and address or just uprn")
|
||||
|
||||
def fetch(self):
|
||||
|
||||
if self._postcode == None and self._address == None and self._uprn == None:
|
||||
_LOGGER.error("no attributes - specify postcode and address or just uprn")
|
||||
return []
|
||||
|
||||
if self._uprn == None:
|
||||
if self._uprn is None:
|
||||
json_data = {
|
||||
"jsonrpc":"2.0",
|
||||
"id":"",
|
||||
"method":"Breckland.Whitespace.JointWasteAPI.GetSiteIDsByPostcode",
|
||||
"params":{
|
||||
"postcode":self._postcode,
|
||||
"environment":"live"
|
||||
}
|
||||
}
|
||||
"jsonrpc": "2.0",
|
||||
"id": "",
|
||||
"method": "Breckland.Whitespace.JointWasteAPI.GetSiteIDsByPostcode",
|
||||
"params": {"postcode": self._postcode, "environment": "live"},
|
||||
}
|
||||
|
||||
r = requests.post(API_URL, json=json_data,headers=headers)
|
||||
|
||||
if r.status_code != 200:
|
||||
_LOGGER.error("Error querying calender data")
|
||||
return []
|
||||
r = requests.post(API_URL, json=json_data, headers=headers)
|
||||
r.raise_for_status()
|
||||
|
||||
json_response = r.json()
|
||||
|
||||
for key in json_response['result']:
|
||||
if self._address.lower() in key['name'].lower():
|
||||
self._uprn = (key['uprn'])
|
||||
for key in json_response["result"]:
|
||||
if self._address.lower() in key["name"].lower():
|
||||
self._uprn = key["uprn"]
|
||||
|
||||
if self._uprn == None:
|
||||
_LOGGER.error("Error querying calender data")
|
||||
return []
|
||||
if self._uprn is None:
|
||||
raise Exception("Error querying calendar data")
|
||||
|
||||
json_data = {
|
||||
"jsonrpc":"2.0",
|
||||
"id":"",
|
||||
"method":"Breckland.Whitespace.JointWasteAPI.GetBinCollectionsByUprn",
|
||||
"params":{
|
||||
"uprn":self._uprn,
|
||||
"environment":"live"
|
||||
}
|
||||
}
|
||||
"jsonrpc": "2.0",
|
||||
"id": "",
|
||||
"method": "Breckland.Whitespace.JointWasteAPI.GetBinCollectionsByUprn",
|
||||
"params": {"uprn": self._uprn, "environment": "live"},
|
||||
}
|
||||
|
||||
r = requests.post(API_URL, json=json_data,headers=headers)
|
||||
r = requests.post(API_URL, json=json_data, headers=headers)
|
||||
r.raise_for_status()
|
||||
|
||||
if r.status_code != 200:
|
||||
_LOGGER.error("Error querying calender data")
|
||||
return []
|
||||
|
||||
waste = r.json()
|
||||
waste = waste['result']
|
||||
waste = r.json()["result"]
|
||||
|
||||
entries = []
|
||||
|
||||
for data in waste:
|
||||
print(data['collectiontype'])
|
||||
print(data['nextcollection'])
|
||||
entries.append(Collection(
|
||||
datetime.strptime(data['nextcollection'],'%d/%m/%Y %H:%M:%S').date(),
|
||||
data['collectiontype'],
|
||||
ICON_MAP.get(data['collectiontype'])
|
||||
))
|
||||
entries.append(
|
||||
Collection(
|
||||
datetime.strptime(
|
||||
data["nextcollection"], "%d/%m/%Y %H:%M:%S"
|
||||
).date(),
|
||||
data["collectiontype"],
|
||||
ICON_MAP.get(data["collectiontype"]),
|
||||
)
|
||||
)
|
||||
|
||||
return entries
|
||||
return entries
|
||||
|
||||
@@ -11,8 +11,7 @@ waste_collection_schedule:
|
||||
args:
|
||||
postcode: POSTCODE
|
||||
address: ADDRESS
|
||||
uprn: UPRN
|
||||
|
||||
prn: UPRN
|
||||
```
|
||||
|
||||
### Configuration Variables
|
||||
@@ -47,9 +46,8 @@ waste_collection_schedule:
|
||||
|
||||
If uprn is provided, only uprn is used. Otherwise postcode and address are required.
|
||||
|
||||
You can find all relevant informations at [Breckland Council](https://www.breckland.gov.uk/mybreckland) homepage
|
||||
use the POSTCODE -> click find address.
|
||||
Choose your address. Please only use the first part of your address. If you got an error, use less charecters from address.
|
||||
You can find all relevant information at [Breckland Council](https://www.breckland.gov.uk/mybreckland) homepage. Use the POSTCODE -> click find address.
|
||||
Choose your address. Please only use the first part of your address. If you got an error, use less characters from address.
|
||||
|
||||
## How to find your UPRN
|
||||
|
||||
|
||||
Reference in New Issue
Block a user