-
-
Notifications
You must be signed in to change notification settings - Fork 674
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Adding vejle_dk source * Fix: ffv_dk references removed * reformatting --------- Co-authored-by: 5ila5 <[email protected]>
- Loading branch information
1 parent
c5d5066
commit 9a1904f
Showing
4 changed files
with
149 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
97 changes: 97 additions & 0 deletions
97
custom_components/waste_collection_schedule/waste_collection_schedule/source/vejle_dk.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,97 @@ | ||
import logging | ||
import re | ||
from datetime import date | ||
from typing import List | ||
|
||
import requests | ||
from bs4 import BeautifulSoup | ||
from waste_collection_schedule import Collection # type: ignore[attr-defined] | ||
|
||
TITLE = "Vejle Kommune" | ||
DESCRIPTION = "Vejle Kommune" | ||
URL = "https://www.vejle.dk/" | ||
FORM_URL = "https://www.affaldonline.dk/kalender/vejle/" | ||
API_URL = "https://www.affaldonline.dk/kalender/vejle/showInfo.php" | ||
|
||
TEST_CASES = { | ||
"test": { | ||
"values": "Poppelvej|1||||7100|Vejle|1254086|1254086|0", | ||
}, | ||
} | ||
|
||
_LOGGER = logging.getLogger("waste_collection_schedule.vejle_dk") | ||
|
||
|
||
DANISH_MONTHS = [ | ||
"januar", | ||
"februar", | ||
"marts", | ||
"april", | ||
"maj", | ||
"juni", | ||
"juli", | ||
"august", | ||
"september", | ||
"oktober", | ||
"november", | ||
"december", | ||
] | ||
|
||
DATE_REGEX = r"(\d{1,2})\. (\w+) (\d{4})" | ||
|
||
|
||
class Source: | ||
def __init__(self, values: str): | ||
_LOGGER.debug("Initializing Source with values=%s", values) | ||
self._api_url = API_URL | ||
self._values = values | ||
|
||
def fetch(self) -> List[Collection]: | ||
_LOGGER.debug("Fetching data from %s", self._api_url) | ||
|
||
entries: List[Collection] = [] | ||
|
||
post_data = {"values": self._values} | ||
|
||
response = requests.post(self._api_url, data=post_data) | ||
response.raise_for_status() | ||
|
||
html_content = response.text | ||
soup = BeautifulSoup(html_content, "html.parser") | ||
|
||
# Find all instances of "Næste tømningsdag" | ||
next_pickup_info = soup.find_all(string=re.compile("Næste tømningsdag:")) | ||
if not next_pickup_info: | ||
raise ValueError( | ||
"No waste schemes found. Please check the provided values." | ||
) | ||
|
||
for info in next_pickup_info: | ||
text = info.strip() | ||
match = re.search(DATE_REGEX, text) | ||
if match: | ||
try: | ||
day = int(match.group(1)) | ||
month_name = match.group(2) | ||
year = int(match.group(3)) | ||
month_index = DANISH_MONTHS.index(month_name) + 1 | ||
formatted_date = date(year, month_index, day) | ||
|
||
# Extract waste type from the text | ||
waste_type_match = re.search(r"\((.*?)\)", text) | ||
if not waste_type_match: | ||
raise ValueError("No waste type found in string: %s", text) | ||
waste_type = waste_type_match.group(1) | ||
|
||
entries.append(Collection(date=formatted_date, t=waste_type)) | ||
_LOGGER.debug( | ||
"Added collection: date=%s, type=%s", | ||
formatted_date, | ||
waste_type, | ||
) | ||
except ValueError as e: | ||
_LOGGER.error("Error parsing date: %s from string: %s", e, text) | ||
else: | ||
_LOGGER.warning("No valid date found in string: %s", text) | ||
|
||
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,50 @@ | ||
# Vejle Kommune | ||
|
||
Support for schedules in Vejle Kommune | ||
Uses affaldonline, which serves several Danish municipalities, but differs when it comes to the layout of the response, which seems to be designed for the individual municipality. | ||
I chose to write a specific solution for Vejle Kommune, since each municipality may choose a different supplier in the future. | ||
This code should be relatively easy to adjust for "Silkeborg forsyning", "Assens Forsyning" and "Middelfart Kommune". | ||
|
||
## Configuration via configuration.yaml | ||
|
||
```yaml | ||
waste_collection_schedule: | ||
sources: | ||
- name: vejle_dk | ||
args: | ||
values: "Poppelvej|1||||7100|Vejle|1254086|1254086|0" | ||
``` | ||
### Configuration Variables | ||
**values** | ||
_(String) (required)_ | ||
A string that includes the street name, house number, postal code, city name, and some numbers that seems to be internal affaldonline references. | ||
## How to find the "values" string to use | ||
Go to the Affaldonline site for vejle: https://www.affaldonline.dk/kalender/vejle/ | ||
Open the developer console in your browser. | ||
Enter your address and select the house number in the dropdown menu. | ||
In the Network tab in the browser console, select the latest URL ending in **showInfo.php** and look under Payload, where you should find the **values** variable read for copy/paste. | ||
Depending on your browser, it should also be possible to right-click the house number field, and select "Inspect" - this should give you the html for the field, and here you can select the value from your house number in the list. | ||
### Customizing Waste Types | ||
Customizing waste types is a feature of the Waste Collection Schedule component and is very useful here, since the waste types in affaldonline are often long and not very consistent. | ||
```yaml | ||
waste_collection_schedule: | ||
sources: | ||
- name: vejle_dk | ||
args: | ||
values: "Poppelvej|1||||7100|Vejle|1254086|1254086|0" | ||
customize: | ||
- type: "Dagrenovation" | ||
alias: "Rest- og madaffald" | ||
``` |
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