Repository: mampfes/hacs_waste_collection_schedule

Issue:
Need help in adding new source for AWB Bad Kreuznach
I like to add a new source.

I collected the neccessary API stuff and can download the data with curl. But I don't know python. Is anybody willing to help me?

My research:

## Cities

Cities are stored in the json array "jsonOrte" in https://app.awb-bad-kreuznach.de/js/adresse.js.

My example is Hargesheim

## Streets
url: https://app.awb-bad-kreuznach.de/api/loadStreets.php
payload: "ort=Hargesheim"
answer: [
 ...
  {
    "strasse": "Händelstraße"
  },
  {
    "strasse": "Hermann-Löns-Weg"
  },
  {
    "strasse": "Hinter Fels"
  },
  {
    "strasse": "Hunsrückstraße"
  },
  ...
]

My example is Winzenheimer Straße 16

## Coordinates

url: https://app.awb-bad-kreuznach.de/api/checkAddress.php
paylod: "ort=Hargesheim&strasse=Winzenheimer+Stra%C3%9Fe&nummer=16"
answer: {lat: "49.8636529485", lon: "7.83477274091"}

## Dates

"termin" is the date.
"restmuell" is garbage.
"bio" is organic waste.
"wert" is recycling.
"papier" is paper.

The numbers  is the ID of the collection tour and "0" means that there is no collection on this date. 

"TRIM(wochentag)" seems to be the actual date for collection. I checked it with the calendar from the app for different dates througout the year.
"wochentag" is another weekday but I don't understand why it's there.

url: https://app.awb-bad-kreuznach.de/api/loadDates.php
payload: "ort=Hargesheim&strasse=Winzenheimer+Stra%C3%9Fe&mode=web&lat=49.8636529485&lon=7.83477274091"
answer: [
  {
    "id": "17279",
    "state": "1",
    "termin": "2022-02-21",
    "restmuell": "0",
    "bio": "0",
    "wert": "0",
    "papier": "1",
    "TRIM(wochentag)": "Montag",
    "wochentag": "Montag"
  },
  {
    "id": "17286",
    "state": "1",
    "termin": "2022-02-25",
    "restmuell": "0",
    "bio": "0",
    "wert": "3",
    "papier": "0",
    "TRIM(wochentag)": "Freitag",
    "wochentag": "Freitag"
  },
  {
    "id": "17319",
    "state": "1",
    "termin": "2022-03-21",
    "restmuell": "0",
    "bio": "0",
    "wert": "0",
    "papier": "1",
    "TRIM(wochentag)": "Montag",
    "wochentag": "Montag"
  },
  ...
]

Best regards
Chris



Discussion:

Resolving patch:
diff --git a/custom_components/waste_collection_schedule/waste_collection_schedule/source/awb_bad_kreuznach_de.py b/custom_components/waste_collection_schedule/waste_collection_schedule/source/awb_bad_kreuznach_de.py
new file mode 100644
--- /dev/null
+++ b/custom_components/waste_collection_schedule/waste_collection_schedule/source/awb_bad_kreuznach_de.py
@@ -0,0 +1,56 @@
+import datetime
+import json
+
+import requests
+from waste_collection_schedule import Collection  # type: ignore[attr-defined]
+
+TITLE = "AWB Bad Kreuznach"
+DESCRIPTION = "Source for AWB Bad Kreuznach."
+URL = "https://app.awb-bad-kreuznach.de/"
+TEST_CASES = {
+    "Hargesheim": {"ort": "Hargesheim", "strasse": "Winzenheimer Straße", "nummer": 16}
+}
+
+TYPES = ("restmuell", "bio", "wert", "papier")
+
+
+class Source:
+    def __init__(self, ort, strasse, nummer):
+        self._ort = ort
+        self._strasse = strasse
+        self._nummer = nummer
+
+    def fetch(self):
+        pass
+        args = {
+            "ort": self._ort,
+            "strasse": self._strasse,
+            "nummer": self._nummer,
+        }
+
+        # get latitude/longitude file
+        r = requests.post(
+            "https://app.awb-bad-kreuznach.de/api/checkAddress.php", data=args
+        )
+        data = json.loads(r.text)
+
+        # get dates
+        del args["nummer"]
+        args["mode"] = "web"
+        args["lat"] = data["lat"]
+        args["lon"] = data["lon"]
+        print(args)
+        r = requests.post(
+            "https://app.awb-bad-kreuznach.de/api/loadDates.php", data=args
+        )
+        data = json.loads(r.text)
+
+        entries = []
+
+        for d in data["termine"]:
+            date = datetime.date.fromisoformat(d["termin"])
+            for type in TYPES:
+                if d[type] != "0":
+                    entries.append(Collection(date, type, date))
+
+        return entries


Test patch: