Energy prices as command prompt ASCII table
Introduction
The website jeroen.nl returns current and daily energy prices, but it would be nice to view this information from the command line.
Description
The Elixir program retrieves the content of the energy prices website and parses the data and displays an ASCII table and summary and is on CodeBerg: photonsphere/dynprice.
The core functionality is implemented as follows:
1defp extract_prices(verbose, body) do
2 {:ok, document} = Floki.parse_document(body)
3
4 current_price_element =
5 document
6 |> Floki.find("div > p:fl-contains('De actuele dynamische')")
7 |> Floki.text()
8 |> String.replace("euro per kWh ", "")
9 |> String.replace(
10 "De actuele dynamische inkoop prijs voor stroom is op dit moment",
11 "Huidige:"
12 )
13 |> String.replace("euro per kWh ", "")
14 |> String.trim()
15 |> String.replace("(", "- momenteel ")
16 |> (fn s -> Regex.replace(~r"\)\.", s, "") end).()
17
18 max_min_price_element =
19 document
20 |> Floki.find("div > p:fl-contains('Hoogste prijs vandaag:')")
21 |> Floki.text()
22 |> String.replace("euro per kWh ", "")
23 |> String.replace("Hoogste prijs vandaag:", "Hoogste:")
24 |> String.replace("Laagste prijs vandaag:", "Laagste:")
25 |> String.trim()
26 |> String.replace("(", "- ")
27 |> (fn s -> Regex.replace(~r"\)\.", s, "") end).()
28
29 {_, _, [script]} =
30 document
31 |> Floki.find("div#graph_energie_vandaag-966 > div > div > script")
32 |> List.first()
33
34 labels =
35 Regex.run(~r"labels:\s*\[(.*?)\]", script, capture: :all_but_first)
36
37 regex =
38 ~r"data:\s*\[(.*?)\].*?[^{}]data:\s*\[(.*?)\].*?[^{}]data:\s*\[(.*?)\].*?[^{}]data:\s*\[(.*?)\].*?"s
39
40 data =
41 Regex.run(regex, script, capture: :all_but_first)
42
43 daily_prices_element = AsciiTableConverter.to_ascii_table(verbose, labels, data)
44
45 {:ok,
46 %{
47 daily_prices: daily_prices_element,
48 current_price: current_price_element,
49 max_min_price: max_min_price_element
50 }}
51 end
Usage
1$ ./dynprice
2https://jeroen.nl/dynamische-energie/prijzen
3
4Hour Total Inkoop
500:00 0.281 0.112
601:00 0.27 0.103
702:00 0.262 0.096
803:00 0.254 0.09
904:00 0.251 0.087
1005:00 0.255 0.091
1106:00 0.263 0.097
1207:00 0.266 0.099
1308:00 0.265 0.099
1409:00 0.25 0.086
1510:00 0.233 0.072
1611:00 0.226 0.067
1712:00 0.215 0.057
1813:00 0.204 0.048
1914:00 0.212 0.055
2015:00 0.224 0.065
2116:00 0.242 0.08
2217:00 0.285 0.116
2318:00 0.3 0.127
2419:00 0.306 0.133
2520:00 0.293 0.121
2621:00 0.277 0.109
2722:00 0.273 0.105
2823:00 0.261 0.095
29
30(euro per kWh)
31Huidige: 0.072480 - momenteel 10:14
32Hoogste: 0.132960 - tussen 19:00 en 20:00
33Laagste: 0.048000 - tussen 13:00 en 14:00
With the verbose option:
1$ ./dynprice --verbose
2https://jeroen.nl/dynamische-energie/prijzen
3
4Hour Total Inkoop Opslag Belast. BTW
500:00 0.281 0.112 0.019 0.102 0.049
601:00 0.27 0.103 0.019 0.102 0.047
702:00 0.262 0.096 0.019 0.102 0.045
803:00 0.254 0.09 0.019 0.102 0.044
904:00 0.251 0.087 0.019 0.102 0.044
1005:00 0.255 0.091 0.019 0.102 0.044
1106:00 0.263 0.097 0.019 0.102 0.046
1207:00 0.266 0.099 0.019 0.102 0.046
1308:00 0.265 0.099 0.019 0.102 0.046
1409:00 0.25 0.086 0.019 0.102 0.043
1510:00 0.233 0.072 0.019 0.102 0.04
1611:00 0.226 0.067 0.019 0.102 0.039
1712:00 0.215 0.057 0.019 0.102 0.037
1813:00 0.204 0.048 0.019 0.102 0.035
1914:00 0.212 0.055 0.019 0.102 0.037
2015:00 0.224 0.065 0.019 0.102 0.039
2116:00 0.242 0.08 0.019 0.102 0.042
2217:00 0.285 0.116 0.019 0.102 0.05
2318:00 0.3 0.127 0.019 0.102 0.052
2419:00 0.306 0.133 0.019 0.102 0.053
2520:00 0.293 0.121 0.019 0.102 0.051
2621:00 0.277 0.109 0.019 0.102 0.048
2722:00 0.273 0.105 0.019 0.102 0.047
2823:00 0.261 0.095 0.019 0.102 0.045
29
30(euro per kWh)
31Huidige: 0.072480 - momenteel 10:26
32Hoogste: 0.132960 - tussen 19:00 en 20:00
33Laagste: 0.048000 - tussen 13:00 en 14:00