|
15 | 15 | GLOBAL_GATEWAY_URL = "https://gateway-prod.global.picnicinternational.com" |
16 | 16 | DEFAULT_COUNTRY_CODE = "NL" |
17 | 17 | DEFAULT_API_VERSION = "15" |
| 18 | +_HEADERS = { |
| 19 | + "x-picnic-agent": "30100;1.15.272-15295;", |
| 20 | + "x-picnic-did": "3C417201548B2E3B", |
| 21 | +} |
18 | 22 |
|
19 | 23 |
|
20 | 24 | class PicnicAPI: |
@@ -47,14 +51,7 @@ def _get(self, path: str, add_picnic_headers=False): |
47 | 51 | url = self._base_url + path |
48 | 52 |
|
49 | 53 | # Make the request, add special picnic headers if needed |
50 | | - headers = ( |
51 | | - { |
52 | | - "x-picnic-agent": "30100;1.15.272-15295;", |
53 | | - "x-picnic-did": "3C417201548B2E3B", |
54 | | - } |
55 | | - if add_picnic_headers |
56 | | - else None |
57 | | - ) |
| 54 | + headers = _HEADERS if add_picnic_headers else None |
58 | 55 | response = self.session.get(url, headers=headers).json() |
59 | 56 |
|
60 | 57 | if self._contains_auth_error(response): |
@@ -177,27 +174,21 @@ def print_categories(self, depth: int = 0): |
177 | 174 | tree = "\n".join(_tree_generator(self.get_categories(depth=depth))) |
178 | 175 | print(tree) |
179 | 176 |
|
180 | | - def get_product_from_gtin(self, etan: str, maxRedirects: int = 5): |
| 177 | + def get_article_by_gtin(self, etan: str, maxRedirects: int = 5): |
| 178 | + # Finds the article ID for a gtin/ean (barcode). |
181 | 179 |
|
182 | | - # Finds the product ID for a gtin/ean (barcode). |
183 | | - headers = ( |
184 | | - { |
185 | | - "x-picnic-agent": "30100;1.15.272-15295;", |
186 | | - "x-picnic-did": "3C417201548B2E3B", |
187 | | - } |
188 | | - ) |
189 | 180 | url = "https://picnic.app/" + self._country_code.lower() + "/qr/gtin/" + etan |
190 | 181 | while maxRedirects > 0: |
191 | 182 | if url == "http://picnic.app/nl/link/store/storefront": |
192 | 183 | # gtin unknown |
193 | 184 | return None |
194 | | - r = self.session.get(url, headers=headers, allow_redirects=False) |
| 185 | + r = self.session.get(url, headers=_HEADERS, allow_redirects=False) |
195 | 186 | maxRedirects -= 1 |
196 | 187 | if ";id=" in r.url: |
197 | | - # found the product id |
198 | | - return r.url.split(";id=",1)[1] |
| 188 | + # found the article id |
| 189 | + return self.get_article(r.url.split(";id=", 1)[1]) |
199 | 190 | if "Location" not in r.headers: |
200 | | - # product id not found but also no futher redirect |
| 191 | + # article id not found but also no futher redirect |
201 | 192 | return None |
202 | 193 | url = r.headers["Location"] |
203 | 194 | return None |
|
0 commit comments