python-picnic-api2 2.0.2__py3-none-any.whl → 2.0.4__py3-none-any.whl

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.
@@ -47,5 +47,5 @@ __all__ = [
47
47
  "OrderArticle",
48
48
  ]
49
49
  __title__ = "python-picnic-api"
50
- __version__ = "2.0.2"
50
+ __version__ = "2.0.4"
51
51
  __author__ = "Mike Brink"
@@ -1,3 +1,4 @@
1
+ import secrets
1
2
  from hashlib import md5
2
3
  from urllib.parse import quote
3
4
 
@@ -26,10 +27,10 @@ from .session import (
26
27
  DEFAULT_URL = "https://storefront-prod.{}.picnicinternational.com/api/{}"
27
28
  DEFAULT_COUNTRY_CODE = "NL"
28
29
  DEFAULT_API_VERSION = "15"
29
- _HEADERS = {
30
- "x-picnic-agent": "30100;1.206.1-#15408",
31
- "x-picnic-did": "598F770380CA54B6",
32
- }
30
+ # Pinned below app 1.224: newer agents get the /pages/ endpoints as a
31
+ # {"layout", "script"} wrapper (1.224-1.246) or React Server Components (1.247+),
32
+ # which the PML parsers do not handle.
33
+ _PICNIC_AGENT = "30100;1.206.1-#15408"
33
34
 
34
35
 
35
36
  class PicnicAPI:
@@ -39,18 +40,31 @@ class PicnicAPI:
39
40
  password: str = None,
40
41
  country_code: str = DEFAULT_COUNTRY_CODE,
41
42
  auth_token: str = None,
43
+ device_id: str | None = None,
42
44
  ):
43
45
  self._country_code = country_code
44
46
  self._base_url = _url_generator(
45
47
  DEFAULT_URL, self._country_code, DEFAULT_API_VERSION
46
48
  )
47
49
 
50
+ # Picnic seems to start blocking static device ids
51
+ self._device_id = device_id or secrets.token_hex(8).upper()
52
+ self._picnic_headers = {
53
+ "x-picnic-agent": _PICNIC_AGENT,
54
+ "x-picnic-did": self._device_id,
55
+ }
56
+
48
57
  self.session = PicnicAPISession(auth_token=auth_token)
49
58
 
50
59
  # Login if not authenticated
51
60
  if not self.session.authenticated and username and password:
52
61
  self.login(username, password)
53
62
 
63
+ @property
64
+ def device_id(self) -> str:
65
+ """The ``x-picnic-did`` value sent to Picnic; persist it to reuse it."""
66
+ return self._device_id
67
+
54
68
  @staticmethod
55
69
  def _parse_json(response, url: str):
56
70
  """Decode a response body, raising a descriptive error if it is not JSON.
@@ -74,7 +88,7 @@ class PicnicAPI:
74
88
  url = self._base_url + path
75
89
 
76
90
  # Make the request, add special picnic headers if needed
77
- headers = _HEADERS if add_picnic_headers else None
91
+ headers = self._picnic_headers if add_picnic_headers else None
78
92
  response = self.session.get(url, headers=headers)
79
93
  body = self._parse_json(response, url)
80
94
 
@@ -89,7 +103,7 @@ class PicnicAPI:
89
103
  url = (base_url_override if base_url_override else self._base_url) + path
90
104
  kwargs = {"json": data}
91
105
  if add_picnic_headers:
92
- kwargs["headers"] = _HEADERS
106
+ kwargs["headers"] = self._picnic_headers
93
107
  response = self.session.post(url, **kwargs)
94
108
  body = self._parse_json(response, url)
95
109
 
@@ -136,7 +150,7 @@ class PicnicAPI:
136
150
  def _post_2fa(self, path: str, data=None):
137
151
  """POST for 2FA endpoints that may return empty (204) or JSON error bodies."""
138
152
  url = self._base_url + path
139
- response = self.session.post(url, json=data, headers=_HEADERS)
153
+ response = self.session.post(url, json=data, headers=self._picnic_headers)
140
154
 
141
155
  if response.status_code == 204 or not response.content:
142
156
  return None
@@ -315,7 +329,9 @@ class PicnicAPI:
315
329
  if url == "http://picnic.app/nl/link/store/storefront":
316
330
  # gtin unknown
317
331
  return None
318
- r = self.session.get(url, headers=_HEADERS, allow_redirects=False)
332
+ r = self.session.get(
333
+ url, headers=self._picnic_headers, allow_redirects=False
334
+ )
319
335
  maxRedirects -= 1
320
336
  if ";id=" in r.url:
321
337
  # found the article id
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.5
2
2
  Name: python-picnic-api2
3
- Version: 2.0.2
3
+ Version: 2.0.4
4
4
  Project-URL: homepage, https://github.com/codesalatdev/python-picnic-api
5
5
  Project-URL: repository, https://github.com/codesalatdev/python-picnic-api
6
6
  Author-email: Mike Brink <mjh.brink@icloud.com>, CodeSalat <pypi@codesalat.dev>
@@ -47,6 +47,12 @@ picnic = PicnicAPI(username='username', password='password', country_code="NL")
47
47
 
48
48
  The country_code parameter defaults to `NL`, but you have to change it if you live in a different country than the Netherlands (ISO 3166-1 Alpha-2). This obviously only works for countries that picnic services.
49
49
 
50
+ Picnic identifies each app install by a device ID (the `x-picnic-did` header). `PicnicAPI` generates a random one per instance. To keep the same ID across restarts, store `picnic.device_id` and pass it back in:
51
+
52
+ ```python
53
+ picnic = PicnicAPI(auth_token=token, device_id=stored_device_id)
54
+ ```
55
+
50
56
  ### Two-factor authentication (2FA)
51
57
 
52
58
  For new logins, Picnic may require two-factor authentication. When 2FA is required, logging in raises a `Picnic2FARequired` exception. You then need to request a code and verify it:
@@ -1,5 +1,5 @@
1
- python_picnic_api2/__init__.py,sha256=fuTT-aUyPOS75WCmpPgk4lTgkcVSRPtfp7Qly1-Saf0,975
2
- python_picnic_api2/client.py,sha256=9hLWpDMYl8mhOllixDI416BOlCgDpsBVTtFP6-jI7lk,11816
1
+ python_picnic_api2/__init__.py,sha256=odnlTJPCArsEx05Xyp8RzupW2OdKjv3Mz1pMhn2IqgU,975
2
+ python_picnic_api2/client.py,sha256=36dVigbSTV9ERWxO_xsTtJAbQOdkVvD18iYUsVIJLLg,12496
3
3
  python_picnic_api2/exceptions.py,sha256=q3ERqBze8rMoNjX0QcRfinCWhExSdob6AFs4l23iSyQ,1973
4
4
  python_picnic_api2/helper.py,sha256=CUoxRu2SZnHE-8azyfm7MUJ_vBA7ifVkHK-IUgUKuRM,971
5
5
  python_picnic_api2/session.py,sha256=2nhlyJ2MXR1yHQRxw85B-KXF4zt8L5__KrI-T_VvURg,2388
@@ -13,7 +13,7 @@ python_picnic_api2/models/delivery.py,sha256=i29cw3E_To3azBGtYKvYkKolRpWJypWxPql
13
13
  python_picnic_api2/models/pml.py,sha256=1NAygk1u2SKBo-QwWMoeuZ4ixTJdDLHdnM7blYdgplM,5290
14
14
  python_picnic_api2/models/search.py,sha256=kGJVXKUml2wFfDyap000inhe5udGO_EssxVji0FJ1f4,2150
15
15
  python_picnic_api2/models/user.py,sha256=Q0qmnhYacCCx93UO_OrpHLdiE4VtCWsunRhJUmxgruY,1662
16
- python_picnic_api2-2.0.2.dist-info/METADATA,sha256=VgQhZ26R-O8ACVjnPXNsiVqOxgbTmxtZO-90qKa-3Pc,8646
17
- python_picnic_api2-2.0.2.dist-info/WHEEL,sha256=2iZrJkDIkeDh0k9SikKMqFuRP-ua6mb4Ir1AsER4BEw,87
18
- python_picnic_api2-2.0.2.dist-info/licenses/LICENSE.md,sha256=CUvt2ilcBwKAfRAnoA4RNPTLcdUj3En21LPK9YkFMGg,11358
19
- python_picnic_api2-2.0.2.dist-info/RECORD,,
16
+ python_picnic_api2-2.0.4.dist-info/METADATA,sha256=XB3LcyfFfmhsvMYoxotF-Ld69e2hlBCrjmTVZRr5s10,8938
17
+ python_picnic_api2-2.0.4.dist-info/WHEEL,sha256=2iZrJkDIkeDh0k9SikKMqFuRP-ua6mb4Ir1AsER4BEw,87
18
+ python_picnic_api2-2.0.4.dist-info/licenses/LICENSE.md,sha256=CUvt2ilcBwKAfRAnoA4RNPTLcdUj3En21LPK9YkFMGg,11358
19
+ python_picnic_api2-2.0.4.dist-info/RECORD,,