lhpapi 1.0.2__tar.gz → 1.0.4__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,4 +1,4 @@
1
- /.pytest_cache
2
- /dist
3
- /src/lhpapi/__pycache__
4
- /tests/__pycache__
1
+ /.pytest_cache
2
+ /dist
3
+ /src/lhpapi/__pycache__
4
+ /tests/__pycache__
@@ -1,3 +1,13 @@
1
+ ## 1.0.4 (2024-12-24)
2
+ ### Fixed
3
+ - Fix NI invalid values
4
+ - Fix BB encoding
5
+ - Add get_all_stations()
6
+
7
+ ## 1.0.3 (2024-10-02)
8
+ ### Fixed
9
+ - Fix BE encoding
10
+
1
11
  ## 1.0.2 (2024-03-03)
2
12
  ### Fixed
3
13
  - Update minimum required Python version to 3.11 due to usage of datetime.UTC
@@ -1,6 +1,6 @@
1
- Metadata-Version: 2.1
1
+ Metadata-Version: 2.4
2
2
  Name: lhpapi
3
- Version: 1.0.2
3
+ Version: 1.0.4
4
4
  Summary: API to retrieve data from the Länderübergreifendes Hochwasser Portal (LHP)
5
5
  Project-URL: Homepage, https://github.com/stephan192/lhpapi
6
6
  Project-URL: Issues, https://github.com/stephan192/lhpapi/issues
@@ -4,7 +4,7 @@ build-backend = "hatchling.build"
4
4
 
5
5
  [project]
6
6
  name = "lhpapi"
7
- version = "1.0.2"
7
+ version = "1.0.4"
8
8
  authors = [
9
9
  { name="stephan192", email="stephan192@outlook.com" },
10
10
  ]
@@ -6,7 +6,7 @@ from datetime import datetime
6
6
  from types import FunctionType
7
7
  from typing import Final
8
8
 
9
- from .api_utils import DynamicData, LHPError, StaticData
9
+ from .api_utils import DynamicData, LHPError, StaticData, fetch_json
10
10
  from .bb_api import init_BB, update_BB
11
11
  from .be_api import init_BE, update_BE
12
12
  from .bw_api import init_BW, update_BW
@@ -70,6 +70,13 @@ def update_lhp_data(static_data: StaticData) -> DynamicData:
70
70
  )
71
71
 
72
72
 
73
+ def get_all_stations() -> dict[str, str]:
74
+ """Return all available stations."""
75
+ return fetch_json(
76
+ "https://raw.githubusercontent.com/stephan192/lhpapi/refs/heads/master/docs/pegel.json"
77
+ )
78
+
79
+
73
80
  class HochwasserPortalAPI:
74
81
  """API to retrieve the data."""
75
82
 
@@ -52,24 +52,6 @@ def fetch_json(url: str, timeout: float = 10.0) -> Any:
52
52
  return json_data
53
53
 
54
54
 
55
- def fetch_soup(
56
- url: str, timeout: float = 10.0, remove_xml: bool = False
57
- ) -> BeautifulSoup:
58
- """Fetch data via soup."""
59
- response = get(url=url, timeout=timeout)
60
- # Override encoding by real educated guess (required for e.g. SH)
61
- response.encoding = response.apparent_encoding
62
- response.raise_for_status()
63
- if remove_xml and (
64
- (response.text.find('<?xml version="1.0" encoding="ISO-8859-15" ?>')) == 0
65
- ):
66
- text = response.text[response.text.find("<!DOCTYPE html>") :]
67
- soup = BeautifulSoup(text, "lxml")
68
- else:
69
- soup = BeautifulSoup(response.text, "lxml")
70
- return soup
71
-
72
-
73
55
  def fetch_text(url: str, timeout: float = 10.0, forced_encoding: str = None) -> str:
74
56
  """Fetch data via text."""
75
57
  response = get(url=url, timeout=timeout)
@@ -82,6 +64,15 @@ def fetch_text(url: str, timeout: float = 10.0, forced_encoding: str = None) ->
82
64
  return response.text
83
65
 
84
66
 
67
+ def fetch_soup(
68
+ url: str, timeout: float = 10.0, forced_encoding: str = None
69
+ ) -> BeautifulSoup:
70
+ """Fetch data via soup."""
71
+ text = fetch_text(url=url, timeout=timeout, forced_encoding=forced_encoding)
72
+ soup = BeautifulSoup(text, "lxml")
73
+ return soup
74
+
75
+
85
76
  def calc_stage(level: float, stage_levels: list[float]) -> int:
86
77
  """Calc stage from level and stage levels."""
87
78
  if (level is None) or all(sl is None for sl in stage_levels):
@@ -2,6 +2,8 @@
2
2
 
3
3
  from __future__ import annotations
4
4
 
5
+ from html import unescape
6
+
5
7
  from .api_utils import (
6
8
  DynamicData,
7
9
  LHPError,
@@ -61,7 +63,7 @@ def fix_bb_encoding(string_in: str) -> str:
61
63
  replace = False
62
64
  else:
63
65
  string_out = string_out + chr(num)
64
- return string_out
66
+ return unescape(string_out)
65
67
 
66
68
 
67
69
  def init_BB(ident: str) -> StaticData: # pylint: disable=invalid-name
@@ -107,6 +109,7 @@ def update_BB(static_data: StaticData) -> DynamicData: # pylint: disable=invali
107
109
  lines = page.split("\n")
108
110
  # Parse data
109
111
  start_found = False
112
+ stage_valid = False
110
113
  prev_line = None
111
114
  for line in lines:
112
115
  line = line.strip()
@@ -21,7 +21,7 @@ def init_BE(ident: str) -> StaticData: # pylint: disable=invalid-name
21
21
  # Get data
22
22
  page = fetch_soup(
23
23
  "https://wasserportal.berlin.de/start.php?anzeige=tabelle_ow&messanzeige=ms_ow_berlin",
24
- remove_xml=True,
24
+ forced_encoding="ISO-8859-15",
25
25
  )
26
26
  # Parse data
27
27
  table = page.find("table", id="pegeltab")
@@ -72,6 +72,8 @@ def update_NI(static_data: StaticData) -> DynamicData: # pylint: disable=invali
72
72
  )
73
73
  except (IndexError, KeyError):
74
74
  value = None
75
+ if value == -888.0:
76
+ value = None
75
77
  try:
76
78
  if data["getStammdatenResult"][0]["Parameter"][0]["Einheit"] == "cm":
77
79
  level = value
@@ -85,15 +87,18 @@ def update_NI(static_data: StaticData) -> DynamicData: # pylint: disable=invali
85
87
  except (IndexError, KeyError):
86
88
  level = None
87
89
  flow = None
88
- try:
89
- last_update = convert_to_datetime(
90
- data["getStammdatenResult"][0]["Parameter"][0]["Datenspuren"][0][
91
- "AktuellerMesswert_Zeitpunkt"
92
- ],
93
- "%d.%m.%Y %H:%M",
94
- )
95
- except (IndexError, KeyError):
90
+ if (level is None) and (flow is None):
96
91
  last_update = None
92
+ else:
93
+ try:
94
+ last_update = convert_to_datetime(
95
+ data["getStammdatenResult"][0]["Parameter"][0]["Datenspuren"][0][
96
+ "AktuellerMesswert_Zeitpunkt"
97
+ ],
98
+ "%d.%m.%Y %H:%M",
99
+ )
100
+ except (IndexError, KeyError):
101
+ last_update = None
97
102
  return DynamicData(level=level, stage=stage, flow=flow, last_update=last_update)
98
103
  except Exception as err:
99
104
  raise LHPError(err, "ni_api.py: update_NI()") from err
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes