Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix for parsing new Poznan's waste collection schedule #3182

Merged
merged 3 commits into from
Dec 18, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import datetime
import logging
import re

import requests
from bs4 import BeautifulSoup, Tag
Expand All @@ -22,13 +21,13 @@
API_URL = "https://www.poznan.pl/mim/odpady/harmonogramy.html"

ICON_MAP = {
1: "mdi:trash-can",
2: "mdi:recycle",
3: "mdi:recycle",
4: "mdi:recycle",
5: "mdi:recycle",
6: "mdi:recycle",
7: "mdi:trash-can",
"Odpady zmieszane": "mdi:trash-can",
"Papier": "mdi:newspaper-variant-outline",
"Metale i tworzywa sztuczne": "mdi:bottle-soda-classic-outline",
"Szkło": "mdi:glass-fragile",
"Bioodpady": "mdi:recycle",
"Odpady wystawkowe": "mdi:cupboard",
"Drzewka świąteczne": "mdi:pine-tree",
}


Expand All @@ -48,34 +47,33 @@ def fetch(self) -> list[Collection]:
r = requests.post(f"{API_URL}", data)
r.raise_for_status()

# Fix their broken html table
fixed_text = re.sub(r"</td>\s*<tr>\s*<tr>\s*<td", "</td></tr><tr><td", r.text)
fixed_text = re.sub(
r"</th>\s*<tr>\s*<tr>\s*<td", "</td></tr><tr><td", fixed_text
)
soup = BeautifulSoup(r.text, "html.parser")

soup = BeautifulSoup(fixed_text, "html.parser")
year = datetime.date.today().year
month = datetime.date.today().month

table = soup.find("table")
table = soup.find("table", id="schedule_0")
if not isinstance(table, Tag):
raise Exception("Invalid address")

year = datetime.date.today().year
month = datetime.date.today().month
formatted_date = f"{month}.{year}"

# find all non empty tr's
trs = [
tr for tr in table.find_all("tr") if isinstance(tr, Tag) and tr.find_all()
]
entries = []
name_map = [th.text.strip() for th in table.find_all("th")]

for row_index, row in enumerate(trs):
if row_index == 0 or row_index > 12:
continue
for cell_index, cell in enumerate(row.find_all("td")):
for row in trs[1:]: # Skipping first row since it is a header
all_cells = row.find_all("td")
collection_name = all_cells[0].text.strip()
# iterate over all rows with dates without collection name
for cell in all_cells[1:]:
if (
cell_index == 0
or not isinstance(cell, Tag)
not isinstance(cell, Tag)
or not cell["data-value"] == formatted_date
or not cell.text.strip()
):
continue
Expand All @@ -84,9 +82,9 @@ def fetch(self) -> list[Collection]:
day = day.strip()
entries.append(
Collection(
datetime.date(year, row_index, int(day)),
name_map[cell_index],
ICON_MAP[cell_index],
datetime.date(year, month, int(day)),
collection_name,
ICON_MAP.get(collection_name, "mdi:recycle"),
)
)

Expand Down
Loading