Skip to content

Commit 23d8783

Browse files
littleyodacodesalatdev
authored andcommitted
Ean Search added
1 parent 33a850f commit 23d8783

File tree

1 file changed

+25
-0
lines changed

1 file changed

+25
-0
lines changed

src/python_picnic_api2/client.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -177,5 +177,30 @@ def print_categories(self, depth: int = 0):
177177
tree = "\n".join(_tree_generator(self.get_categories(depth=depth)))
178178
print(tree)
179179

180+
def get_product_from_gtin(self, etan: str, maxRedirects: int = 5):
181+
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+
url = "https://picnic.app/" + self._country_code.lower() + "/qr/gtin/" + etan
190+
while maxRedirects > 0:
191+
if url == "http://picnic.app/nl/link/store/storefront":
192+
# gtin unknown
193+
return None
194+
r = self.session.get(url, headers=headers, allow_redirects=False)
195+
maxRedirects -= 1
196+
if ";id=" in r.url:
197+
# found the product id
198+
return r.url.split(";id=",1)[1]
199+
if "Location" not in r.headers:
200+
# product id not found but also no futher redirect
201+
return None
202+
url = r.headers["Location"]
203+
return None
204+
180205

181206
__all__ = ["PicnicAPI"]

0 commit comments

Comments
 (0)