Support for Contarina in Treviso, Italy (#1542)

* feat(source): support for Contarina in Treviso, Italy

* fix(Contarina): cleanup of newline in district name

* reformatting + add Italy to update_docu_links.py

---------

Co-authored-by: Luca Crema <lcrema@euronext.com>
Co-authored-by: 5ila5 <5ila5@users.noreply.github.com>
This commit is contained in:
Luca Crema
2023-12-21 15:33:15 +01:00
committed by GitHub
parent a79b0aed17
commit 889281b251
5 changed files with 129 additions and 0 deletions

View File

@@ -839,6 +839,12 @@ Waste collection schedules in the following formats and countries are supported.
- [ÉTH (Érd, Diósd, Nagytarcsa, Sóskút, Tárnok)](/doc/source/eth_erd_hu.md) / eth-erd.hu
</details>
<details>
<summary>Italy</summary>
- [Contarina](/doc/source/contarina_it.md) / contarina.it
</details>
<details>
<summary>Lithuania</summary>

View File

@@ -0,0 +1,79 @@
from datetime import datetime
import requests
from bs4 import BeautifulSoup
from waste_collection_schedule import Collection # type: ignore[attr-defined]
TITLE = "Contarina"
DESCRIPTION = "Waste collection provider in Treviso, Italy"
URL = "https://contarina.it/"
TEST_CASES = {
"Trevignano": {"district": "Trevignano"},
"Montebelluna 1": {"district": "Montebelluna centro storico"},
"Montebelluna 2": {"district": "Montebelluna fuori centro storico"},
}
API_URL = f"{URL}cittadino/raccolta-differenziata/eco-calendario"
ICON_MAP = {
"secco": "mdi:trash-can",
"vpl": "mdi:recycle",
"carta": "mdi:package-variant",
"umido": "mdi:leaf",
"vegetale": "mdi:leaf",
}
class Source:
def __init__(self, district: str):
self._district = district.lower()
def _find_district_table(self, soup: BeautifulSoup) -> BeautifulSoup:
tables = soup.find_all("table", {"class": ["table", "comune"]})
for table in tables:
row = table.find_all("tr")[0]
cells = row.find_all("td")
if len(cells) == 0:
continue
table_district = cells[0].text.strip().lower().replace("\n", " ")
if self._district in table_district:
return table
return None
def fetch(self) -> list[Collection]:
# Fetch the page
r = requests.get(API_URL)
# Parse the page
soup = BeautifulSoup(r.content, "html.parser")
# Find the table that contains the given district
# in the first column of the first row
table = self._find_district_table(soup)
if table is None:
raise Exception(f"Could not find district {self._district}")
# The first cell of the second rows contains another table
# with header "Date" and "Waste type"
second_row = table.find_all("tr")[1]
waste_table = second_row.find_all("table")[0]
waste_rows = waste_table.find_all("tr")
# Skip first row and parse the rest as collections
# NOTE: there may be more than one collection per day
collections = []
for row in waste_rows[1:]:
cells = row.find_all("td")
date = datetime.strptime(cells[0].text.strip(), "%d-%m-%Y").date()
paragraphs = cells[1].find_all("p")
for paragraph in paragraphs:
waste_text = paragraph.text.strip()
icon = ICON_MAP[waste_text.lower()]
collections.append(Collection(date, waste_text, icon))
return collections

View File

@@ -0,0 +1,39 @@
# Contarina
Support for schedules provided by [Contarina.it](https://contarina.it/).
## Types
Supported waste types for this sources are:
- Secco
- Carta
- VPL (vetro, plastica e lattine)
- Umido
- Vegetale
## Configuration via configuration.yaml
```yaml
waste_collection_schedule:
sources:
- name: contarina_it
args:
district: DISTRICT_NAME
```
### Configuration Variables
**district_name**
*(string) (required)*
The list of allowed district names (comune) can be found in the [calendar page](https://contarina.it/cittadino/raccolta-differenziata/eco-calendario).
## Example
```yaml
waste_collection_schedule:
sources:
- name: contarina_it
args:
district: Treviso
```

File diff suppressed because one or more lines are too long

View File

@@ -418,6 +418,10 @@ COUNTRYCODES = [
"code": "hu",
"name": "Hungary",
},
{
"code": "it",
"name": "Italy",
},
{
"code": "lt",
"name": "Lithuania",