weather_provider_api.routers.weather.sources.weather_alert package

Submodules

weather_provider_api.routers.weather.sources.weather_alert.weather_alert module

A class to retrieve the current Weather Alert status according to the KNMI site.

class AlertDivParser

Bases: HTMLParser

A simple HTML parser to extract the alert color from the KNMI weather alert page.

__init__()

Initialize the AlertDivParser class.

handle_starttag(tag, attrs)

Handle the start tag of HTML elements and look for a div with the class ‘alert’ and ‘alert–<color>’.

If it is, extract the color and store it in the found_color attribute.

Parameters:
  • tag (str)

  • attrs (list[tuple[str, str | None]])

Return type:

None

class WeatherAlert

Bases: object

A class (not a Weather Model!) that parses the Weather Alert status from the KNMI site (Weeralarm).

__init__()

Initialize the WeatherAlert class with the necessary information and settings.

get_alarm()

A function that retrieves the current weather alarm stage for each of the Dutch provinces and puts those together into a formatted list of results (string-based).

Returns:

A list of tuples holding all the provinces and their retrieved current alarm stages according to KNMI

Return type:

list[tuple[str, str]]

static process_page(page_text, status_code, province)

Parse the weather alert page for a province and retrieve its current alarm stage.

It does so by looking for a div with the class “alert” and “alert–<color>” (where color is the code of the alarm stage). If it finds such a div, it returns the color as the alarm stage. If it doesn’t find such a div, it returns an error message based on the status code.

Args:

page_text: The response content retrieved while trying to download the page status_code: The status code retrieved while trying to download the page. province: The province associated with the url, status code and alarm stage.

Returns:

A tuple holding the province and a result-string for that province.

Parameters:
  • page_text (str)

  • status_code (int)

  • province (str)

Return type:

tuple[str, str]

class WeatherAlertCode

Bases: Enum

Enum class with valid Weather Alert Codes.

green = 'green'
orange = 'orange'
red = 'red'
yellow = 'yellow'
extract_alert_color(page_text)

Extract the alert color from the KNMI weather alert page using the AlertDivParser.

Parameters:

page_text (str)

Return type:

str | None

Module contents