pybraendstofpriser 2.2.0__tar.gz → 2.3.0__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.
@@ -0,0 +1,53 @@
1
+ Metadata-Version: 2.4
2
+ Name: pybraendstofpriser
3
+ Version: 2.3.0
4
+ Summary: Library for fetching fuel prices from Fuelprices.dk API
5
+ License: GPL-3.0-only
6
+ License-File: LICENSE
7
+ Author: Malene Trab
8
+ Author-email: malene@trab.dk
9
+ Requires-Python: >=3.11,<4.0
10
+ Classifier: License :: OSI Approved :: GNU General Public License v3 (GPLv3)
11
+ Classifier: Programming Language :: Python :: 3
12
+ Classifier: Programming Language :: Python :: 3.11
13
+ Classifier: Programming Language :: Python :: 3.12
14
+ Classifier: Programming Language :: Python :: 3.13
15
+ Classifier: Programming Language :: Python :: 3.14
16
+ Classifier: Programming Language :: Python :: 3.15
17
+ Requires-Dist: aiohttp (>=3.13.2,<4.0.0)
18
+ Description-Content-Type: text/markdown
19
+
20
+ ![Buy Me A Coffee](https://www.buymeacoffee.com/assets/img/custom_images/orange_img.png)
21
+
22
+ ## pybraendstofpriser
23
+
24
+ This is a PyPI module for fetching fuel prices from Fuelprices.dk API, primarily developed for use with [Home Assistant](https://home-assistant.io), but I try to keep it as widely usable as possible.
25
+
26
+ The API requires an API key to be used - see [https://fuelprices.dk](https://fuelprices.dk) for more information.
27
+
28
+ ### Fetch prices for multiple stations
29
+
30
+ ```python
31
+ from pybraendstofpriser import Braendstofpriser
32
+
33
+ client = Braendstofpriser("your-api-key")
34
+ stations = await client.get_prices_bulk([1234, 4321])
35
+
36
+ for item in stations:
37
+ print(item["station"]["name"], item["prices"])
38
+ ```
39
+
40
+ `get_prices_bulk` returns an `Flist` of station records with `company`, `station`,
41
+ `prices`, `price_units`, and `price_kinds`, preserving the API's values (including
42
+ prices as strings). It requests all current products for each station.
43
+
44
+ Up to 1,000 distinct station IDs are fetched in one HTTP request with a page size
45
+ of 1,000. Larger selections are split into groups of at most 1,000 IDs; any further
46
+ pages are fetched automatically. Duplicate IDs are requested once, and an empty
47
+ selection makes no requests. Result order follows the API within each group.
48
+
49
+ The API omits unknown, inactive, stale, and unpriced stations. HTTP errors are
50
+ raised to the caller, including errors on later groups or pages; partial results
51
+ are not returned. The existing `get_prices` method remains available for fetching
52
+ a single station.
53
+
@@ -0,0 +1,33 @@
1
+ ![Buy Me A Coffee](https://www.buymeacoffee.com/assets/img/custom_images/orange_img.png)
2
+
3
+ ## pybraendstofpriser
4
+
5
+ This is a PyPI module for fetching fuel prices from Fuelprices.dk API, primarily developed for use with [Home Assistant](https://home-assistant.io), but I try to keep it as widely usable as possible.
6
+
7
+ The API requires an API key to be used - see [https://fuelprices.dk](https://fuelprices.dk) for more information.
8
+
9
+ ### Fetch prices for multiple stations
10
+
11
+ ```python
12
+ from pybraendstofpriser import Braendstofpriser
13
+
14
+ client = Braendstofpriser("your-api-key")
15
+ stations = await client.get_prices_bulk([1234, 4321])
16
+
17
+ for item in stations:
18
+ print(item["station"]["name"], item["prices"])
19
+ ```
20
+
21
+ `get_prices_bulk` returns an `Flist` of station records with `company`, `station`,
22
+ `prices`, `price_units`, and `price_kinds`, preserving the API's values (including
23
+ prices as strings). It requests all current products for each station.
24
+
25
+ Up to 1,000 distinct station IDs are fetched in one HTTP request with a page size
26
+ of 1,000. Larger selections are split into groups of at most 1,000 IDs; any further
27
+ pages are fetched automatically. Duplicate IDs are requested once, and an empty
28
+ selection makes no requests. Result order follows the API within each group.
29
+
30
+ The API omits unknown, inactive, stale, and unpriced stations. HTTP errors are
31
+ raised to the caller, including errors on later groups or pages; partial results
32
+ are not returned. The existing `get_prices` method remains available for fetching
33
+ a single station.
@@ -5,16 +5,17 @@ from __future__ import annotations
5
5
  import logging
6
6
  import sys
7
7
  from collections import namedtuple
8
-
9
- from .exceptions import StationNotFoundError
8
+ from collections.abc import Sequence
10
9
 
11
10
  from .conn import Connector, Endpoint
11
+ from .exceptions import StationNotFoundError
12
12
 
13
13
  if sys.version_info < (3, 11, 0):
14
14
  sys.exit("The pybraendstofpriser module requires Python 3.11.0 or later")
15
15
 
16
16
  _LOGGER = logging.getLogger(__name__)
17
17
  Company = namedtuple("Company", "module namespace products name")
18
+ _BULK_STATION_LIMIT = 1000
18
19
 
19
20
 
20
21
  class Flist(list):
@@ -74,3 +75,32 @@ class Braendstofpriser:
74
75
  args = {"station_id": station_id}
75
76
 
76
77
  return await self.conn.fetch_data(Endpoint.PRICES, args)
78
+
79
+ async def get_prices_bulk(self, station_ids: Sequence[int]) -> Flist[dict]:
80
+ """Get all current products for the requested stations.
81
+
82
+ Split IDs into groups of at most 1000 and fetch all pages per group.
83
+ Return the API records unchanged; raise on any failed request.
84
+ """
85
+ unique_ids = list(dict.fromkeys(station_ids))
86
+ stations = Flist()
87
+ for start in range(0, len(unique_ids), _BULK_STATION_LIMIT):
88
+ batch = unique_ids[start : start + _BULK_STATION_LIMIT]
89
+ page = 1
90
+ while True:
91
+ data = await self.conn.fetch_data(
92
+ Endpoint.STATIONS_BULK,
93
+ {
94
+ "station_ids": ",".join(
95
+ str(station_id) for station_id in batch
96
+ ),
97
+ "page": page,
98
+ "page_size": _BULK_STATION_LIMIT,
99
+ },
100
+ )
101
+ stations.extend(data["items"])
102
+ if page >= data["total_pages"]:
103
+ break
104
+ page += 1
105
+
106
+ return stations
@@ -1,8 +1,10 @@
1
1
  """Connector module for communicating with fuel price services."""
2
2
 
3
3
  from __future__ import annotations
4
+
4
5
  from typing import Optional
5
6
  from wsgiref import headers
7
+
6
8
  import aiohttp
7
9
 
8
10
  from .const import API_ENDPOINT, API_TIMEOUT
@@ -13,6 +15,7 @@ class Endpoint:
13
15
 
14
16
  PRICES = "/prices"
15
17
  STATIONS = "/stations"
18
+ STATIONS_BULK = "/stations/bulk"
16
19
  COMPANIES = "/companies"
17
20
 
18
21
 
@@ -1,9 +1,9 @@
1
1
  [tool.poetry]
2
2
  name = "pybraendstofpriser"
3
- version = "2.2.0"
3
+ version = "2.3.0"
4
4
  description = "Library for fetching fuel prices from Fuelprices.dk API"
5
5
  authors = ["Malene Trab <malene@trab.dk>"]
6
- license = "MIT"
6
+ license = "GPL-3.0-only"
7
7
  readme = "README.md"
8
8
 
9
9
  [tool.poetry.dependencies]
@@ -1,25 +0,0 @@
1
- Metadata-Version: 2.4
2
- Name: pybraendstofpriser
3
- Version: 2.2.0
4
- Summary: Library for fetching fuel prices from Fuelprices.dk API
5
- License: MIT
6
- License-File: LICENSE
7
- Author: Malene Trab
8
- Author-email: malene@trab.dk
9
- Requires-Python: >=3.11,<4.0
10
- Classifier: License :: OSI Approved :: MIT License
11
- Classifier: Programming Language :: Python :: 3
12
- Classifier: Programming Language :: Python :: 3.11
13
- Classifier: Programming Language :: Python :: 3.12
14
- Classifier: Programming Language :: Python :: 3.13
15
- Classifier: Programming Language :: Python :: 3.14
16
- Requires-Dist: aiohttp (>=3.13.2,<4.0.0)
17
- Description-Content-Type: text/markdown
18
-
19
- ![Buy Me A Coffee](https://www.buymeacoffee.com/assets/img/custom_images/orange_img.png)
20
-
21
- ## pybraendstofpriser
22
-
23
- This is a PyPI module for fetching fuel prices from Fuelprices.dk API, primarily developed for use with [Home Assistant](https://home-assistant.io), but I try to keep it as widely usable as possible.
24
-
25
- The API requires an API key to be used - see [https://fuelprices.dk](https://fuelprices.dk) for more information.
@@ -1,7 +0,0 @@
1
- ![Buy Me A Coffee](https://www.buymeacoffee.com/assets/img/custom_images/orange_img.png)
2
-
3
- ## pybraendstofpriser
4
-
5
- This is a PyPI module for fetching fuel prices from Fuelprices.dk API, primarily developed for use with [Home Assistant](https://home-assistant.io), but I try to keep it as widely usable as possible.
6
-
7
- The API requires an API key to be used - see [https://fuelprices.dk](https://fuelprices.dk) for more information.