forked from mampfes/hacs_waste_collection_schedule
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add Conwy County Borough Council source (mampfes#1456)
* Add Conwy County Borough Council source * reformatting + using get params --------- Co-authored-by: 5ila5 <[email protected]>
- Loading branch information
Showing
4 changed files
with
92 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
54 changes: 54 additions & 0 deletions
54
custom_components/waste_collection_schedule/waste_collection_schedule/source/conwy_gov_uk.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
from datetime import datetime | ||
|
||
import requests | ||
from bs4 import BeautifulSoup | ||
from waste_collection_schedule import Collection # type: ignore[attr-defined] | ||
|
||
TITLE = "Conwy County Borough Council" | ||
DESCRIPTION = "Source for Conwy County Borough Council." | ||
URL = "https://www.conwy.gov.uk/" | ||
TEST_CASES = { | ||
"50000009637": {"uprn": 50000009637}, | ||
"100101037037": {"uprn": "100101037037"}, | ||
"50000007574": {"uprn": 50000007574}, | ||
} | ||
|
||
|
||
ICON_MAP = { | ||
"garden": "mdi:leaf", | ||
"electrical": "mdi:battery", | ||
"refuse": "mdi:trash-can", | ||
"recycle": "mdi:recycle", | ||
} | ||
|
||
|
||
API_URL = "https://www.conwy.gov.uk/Contensis-Forms/erf/collection-result-soap-xmas.asp" | ||
|
||
|
||
class Source: | ||
def __init__(self, uprn: str | int): | ||
self._uprn: str | int = uprn | ||
|
||
def fetch(self): | ||
r = requests.get(API_URL, params={"uprn": self._uprn, "ilangid": 1}) | ||
r.raise_for_status() | ||
|
||
entries = [] | ||
|
||
soup = BeautifulSoup(r.text, "html.parser") | ||
collection_dates = soup.select(".containererf") | ||
|
||
if not collection_dates: | ||
raise Exception("Could not find collections") | ||
|
||
for collection in collection_dates: | ||
date_str = collection.select_one("#main #content").text.strip() | ||
bin_types = [el.text for el in collection.select("#main1 li")] | ||
|
||
date = datetime.strptime(date_str, "%A, %d/%m/%Y").date() | ||
|
||
for bin_type in bin_types: | ||
icon = ICON_MAP.get(bin_type.split(" ")[0].lower()) # Collection icon | ||
entries.append(Collection(date=date, t=bin_type, icon=icon)) | ||
|
||
return entries |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
# Conwy County Borough Council | ||
|
||
Support for schedules provided by [Conwy County Borough Council](https://www.conwy.gov.uk/), serving Conwy, UK. | ||
|
||
## Configuration via configuration.yaml | ||
|
||
```yaml | ||
waste_collection_schedule: | ||
sources: | ||
- name: conwy_gov_uk | ||
args: | ||
uprn: "UPRN" | ||
|
||
``` | ||
### Configuration Variables | ||
**uprn** | ||
*(String | Integer) (required)* | ||
## Example | ||
```yaml | ||
waste_collection_schedule: | ||
sources: | ||
- name: conwy_gov_uk | ||
args: | ||
uprn: "200003177805" | ||
|
||
``` | ||
## How to get the source argument | ||
Go to <https://www.conwy.gov.uk/en/Resident/Recycling-and-Waste/Check-my-collection-day.aspx>, enter your postcode and click 'Submit'. In the list of addresses that appears, right click your address and select 'Copy Link' or 'Copy Link Address'. Paste this URL somewhere - the last element of this URL is your UPRN: https://www.conwy.gov.uk/Contensis-Forms/erf/collection-result-soap-xmas.asp?ilangid=1&uprn={UPRN} | ||
Another way to discover your Unique Property Reference Number (UPRN) is by going to <https://www.findmyaddress.co.uk/> and entering in your address details. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters