File: update_biocell_list.py

package info (click to toggle)
python-dwdwfsapi 1.1.0-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 776 kB
  • sloc: python: 739; makefile: 3
file content (44 lines) | stat: -rw-r--r-- 1,201 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
"""DWD WFS API - Utility module to update biocells.md."""

from __future__ import annotations

import requests


def fetch_json() -> dict:
    """Fetch data via json."""
    url = (
        "https://maps.dwd.de/geoserver/dwd/ows?service=WFS"
        + "&version=2.0.0&request=GetFeature&typeName="
        + "dwd:Biowettergebiete"
        + "&OutputFormat=application/json"
    )
    response = requests.get(url=url, timeout=10.0)
    return response.json()


def parse_json(data: dict) -> tuple[str, str]:
    """Parse json data."""
    stations = []
    for x in data["features"]:
        cellid = x["properties"]["GF"]
        name = x["properties"]["GEN"]
        stations.append((cellid, name))
    return stations


# Fetch and sort all available stations
all_stations = []
print("Fetching Biowettergebiete")
all_stations.extend(parse_json(fetch_json()))
all_stations.sort(key=lambda x: x[0])

print("Updating biocells.md")
with open("biocells.md", "w", encoding="utf-8") as f:
    print("| Cell ID | Name |", file=f)
    print("|---------|------|", file=f)
    for cell in all_stations:
        output = "| " + str(cell[0]) + " | " + cell[1] + " |"
        print(output, file=f)

    f.close()