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

Wolfsburger Abfallwirtschaft und Straßenreinigung doesn't work anymore #3242

Open
standbrot opened this issue Dec 19, 2024 · 1 comment
Open
Labels
source request Request to add a new source

Comments

@standbrot
Copy link

Municipality / Region

Wolfsburg, Germany

Collection Calendar Webpage

https://was-wolfsburg.de/ or https://abfuhrtermine.waswob.de/

Example Address

City: Barnstorf
Street: Bahnhofspassage

Collection Data Format

As html on a webpage

Additional Information

Wolfsburg updated the way to search for your waste dates. It used the city or district (for paper, organic waste and residual waste) and the street (for plastic).
But since the update they are forwarding you to another domain were only the street ist used.

@standbrot standbrot added the source request Request to add a new source label Dec 19, 2024
@standbrot
Copy link
Author

standbrot commented Dec 20, 2024

I'm not a Programmer.
I just tinkerd around. To be honest i asked ChatGPT5 to help. After some try and error "we" came up with this code. It seems like it works fine.
Here the Code:

from datetime import date, datetime
import requests
from bs4 import BeautifulSoup, Tag
from waste_collection_schedule import Collection  # type: ignore[attr-defined]

TITLE = "Wolfsburger Abfallwirtschaft und Straßenreinigung"
DESCRIPTION = "Source for waste collections for WAS-Wolfsburg, Germany."
URL = "https://waswob.de"  # Neue URL
TEST_CASES = {
    "Kattowitzer Str.": {"street": "Kattowitzer Str."},
    "Heinrich-Nordhoff-Str.": {"street": "Heinrich-Nordhoff-Str."},
}

ICON_MAP = {
    "Gelber Sack": "mdi:recycle",
    "Bioabfall": "mdi:leaf",
    "Restabfall": "mdi:trash-can",
    "Altpapier": "mdi:file-document-outline",
}


class Source:
    def __init__(self, street: str | None):
        self._street = street
        if street is None:
            raise ValueError("Street must be set")

    def fetch(self) -> list[Collection]:
        """
        Fetch waste collection data for the specified street.
        """
        if not self._street:
            raise ValueError("Street must be provided to fetch data.")

        # URL der JSON-API
        url = f"https://abfuhrtermine.waswob.de/php/abfuhrtermineeingabe.php?k={self._street.replace(' ', '%20')}"

        try:
            # Daten abrufen
            response = requests.get(url)
            response.raise_for_status()  # Fehler prüfen
            data = response.json()  # JSON-Inhalt parsen
        except requests.RequestException as e:
            raise RuntimeError(f"Error fetching data: {e}")
        except ValueError:
            raise RuntimeError("Invalid JSON response from the server.")

        # Ergebnisse verarbeiten
        entries = []
        for entry in data:
            try:
                waste_type = entry.get("waste", "Unbekannt").title()  # Großbuchstaben anwenden
                date_str = entry.get("date", "01.01.1970")  # Fallback-Datum
                collection_date = datetime.strptime(date_str, "%d.%m.%Y").date()

                # Icon zuordnen
                icon = ICON_MAP.get(waste_type, "mdi:trash-can")

                # Sammlung hinzufügen
                entries.append(Collection(date=collection_date, t=waste_type, icon=icon))
            except Exception as e:
                print(f"Error processing entry: {entry}, {e}")

        return entries

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
source request Request to add a new source
Projects
None yet
Development

No branches or pull requests

1 participant