python-picnic-api2 1.1.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.
- python_picnic_api2-1.1.0/LICENSE.md +202 -0
- python_picnic_api2-1.1.0/PKG-INFO +104 -0
- python_picnic_api2-1.1.0/README.rst +81 -0
- python_picnic_api2-1.1.0/pyproject.toml +22 -0
- python_picnic_api2-1.1.0/python_picnic_api2/__init__.py +6 -0
- python_picnic_api2-1.1.0/python_picnic_api2/client.py +164 -0
- python_picnic_api2-1.1.0/python_picnic_api2/helper.py +111 -0
- python_picnic_api2-1.1.0/python_picnic_api2/session.py +54 -0
|
@@ -0,0 +1,202 @@
|
|
|
1
|
+
|
|
2
|
+
Apache License
|
|
3
|
+
Version 2.0, January 2004
|
|
4
|
+
http://www.apache.org/licenses/
|
|
5
|
+
|
|
6
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
7
|
+
|
|
8
|
+
1. Definitions.
|
|
9
|
+
|
|
10
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
11
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
12
|
+
|
|
13
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
14
|
+
the copyright owner that is granting the License.
|
|
15
|
+
|
|
16
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
17
|
+
other entities that control, are controlled by, or are under common
|
|
18
|
+
control with that entity. For the purposes of this definition,
|
|
19
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
20
|
+
direction or management of such entity, whether by contract or
|
|
21
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
22
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
23
|
+
|
|
24
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
25
|
+
exercising permissions granted by this License.
|
|
26
|
+
|
|
27
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
28
|
+
including but not limited to software source code, documentation
|
|
29
|
+
source, and configuration files.
|
|
30
|
+
|
|
31
|
+
"Object" form shall mean any form resulting from mechanical
|
|
32
|
+
transformation or translation of a Source form, including but
|
|
33
|
+
not limited to compiled object code, generated documentation,
|
|
34
|
+
and conversions to other media types.
|
|
35
|
+
|
|
36
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
37
|
+
Object form, made available under the License, as indicated by a
|
|
38
|
+
copyright notice that is included in or attached to the work
|
|
39
|
+
(an example is provided in the Appendix below).
|
|
40
|
+
|
|
41
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
42
|
+
form, that is based on (or derived from) the Work and for which the
|
|
43
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
44
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
45
|
+
of this License, Derivative Works shall not include works that remain
|
|
46
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
47
|
+
the Work and Derivative Works thereof.
|
|
48
|
+
|
|
49
|
+
"Contribution" shall mean any work of authorship, including
|
|
50
|
+
the original version of the Work and any modifications or additions
|
|
51
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
52
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
53
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
54
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
55
|
+
means any form of electronic, verbal, or written communication sent
|
|
56
|
+
to the Licensor or its representatives, including but not limited to
|
|
57
|
+
communication on electronic mailing lists, source code control systems,
|
|
58
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
59
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
60
|
+
excluding communication that is conspicuously marked or otherwise
|
|
61
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
62
|
+
|
|
63
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
64
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
65
|
+
subsequently incorporated within the Work.
|
|
66
|
+
|
|
67
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
68
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
69
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
70
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
71
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
72
|
+
Work and such Derivative Works in Source or Object form.
|
|
73
|
+
|
|
74
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
75
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
76
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
77
|
+
(except as stated in this section) patent license to make, have made,
|
|
78
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
79
|
+
where such license applies only to those patent claims licensable
|
|
80
|
+
by such Contributor that are necessarily infringed by their
|
|
81
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
82
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
83
|
+
institute patent litigation against any entity (including a
|
|
84
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
85
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
86
|
+
or contributory patent infringement, then any patent licenses
|
|
87
|
+
granted to You under this License for that Work shall terminate
|
|
88
|
+
as of the date such litigation is filed.
|
|
89
|
+
|
|
90
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
91
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
92
|
+
modifications, and in Source or Object form, provided that You
|
|
93
|
+
meet the following conditions:
|
|
94
|
+
|
|
95
|
+
(a) You must give any other recipients of the Work or
|
|
96
|
+
Derivative Works a copy of this License; and
|
|
97
|
+
|
|
98
|
+
(b) You must cause any modified files to carry prominent notices
|
|
99
|
+
stating that You changed the files; and
|
|
100
|
+
|
|
101
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
102
|
+
that You distribute, all copyright, patent, trademark, and
|
|
103
|
+
attribution notices from the Source form of the Work,
|
|
104
|
+
excluding those notices that do not pertain to any part of
|
|
105
|
+
the Derivative Works; and
|
|
106
|
+
|
|
107
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
108
|
+
distribution, then any Derivative Works that You distribute must
|
|
109
|
+
include a readable copy of the attribution notices contained
|
|
110
|
+
within such NOTICE file, excluding those notices that do not
|
|
111
|
+
pertain to any part of the Derivative Works, in at least one
|
|
112
|
+
of the following places: within a NOTICE text file distributed
|
|
113
|
+
as part of the Derivative Works; within the Source form or
|
|
114
|
+
documentation, if provided along with the Derivative Works; or,
|
|
115
|
+
within a display generated by the Derivative Works, if and
|
|
116
|
+
wherever such third-party notices normally appear. The contents
|
|
117
|
+
of the NOTICE file are for informational purposes only and
|
|
118
|
+
do not modify the License. You may add Your own attribution
|
|
119
|
+
notices within Derivative Works that You distribute, alongside
|
|
120
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
121
|
+
that such additional attribution notices cannot be construed
|
|
122
|
+
as modifying the License.
|
|
123
|
+
|
|
124
|
+
You may add Your own copyright statement to Your modifications and
|
|
125
|
+
may provide additional or different license terms and conditions
|
|
126
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
127
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
128
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
129
|
+
the conditions stated in this License.
|
|
130
|
+
|
|
131
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
132
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
133
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
134
|
+
this License, without any additional terms or conditions.
|
|
135
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
136
|
+
the terms of any separate license agreement you may have executed
|
|
137
|
+
with Licensor regarding such Contributions.
|
|
138
|
+
|
|
139
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
140
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
141
|
+
except as required for reasonable and customary use in describing the
|
|
142
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
143
|
+
|
|
144
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
145
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
146
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
147
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
148
|
+
implied, including, without limitation, any warranties or conditions
|
|
149
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
150
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
151
|
+
appropriateness of using or redistributing the Work and assume any
|
|
152
|
+
risks associated with Your exercise of permissions under this License.
|
|
153
|
+
|
|
154
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
155
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
156
|
+
unless required by applicable law (such as deliberate and grossly
|
|
157
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
158
|
+
liable to You for damages, including any direct, indirect, special,
|
|
159
|
+
incidental, or consequential damages of any character arising as a
|
|
160
|
+
result of this License or out of the use or inability to use the
|
|
161
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
162
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
163
|
+
other commercial damages or losses), even if such Contributor
|
|
164
|
+
has been advised of the possibility of such damages.
|
|
165
|
+
|
|
166
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
167
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
168
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
169
|
+
or other liability obligations and/or rights consistent with this
|
|
170
|
+
License. However, in accepting such obligations, You may act only
|
|
171
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
172
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
173
|
+
defend, and hold each Contributor harmless for any liability
|
|
174
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
175
|
+
of your accepting any such warranty or additional liability.
|
|
176
|
+
|
|
177
|
+
END OF TERMS AND CONDITIONS
|
|
178
|
+
|
|
179
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
180
|
+
|
|
181
|
+
To apply the Apache License to your work, attach the following
|
|
182
|
+
boilerplate notice, with the fields enclosed by brackets "{}"
|
|
183
|
+
replaced with your own identifying information. (Don't include
|
|
184
|
+
the brackets!) The text should be enclosed in the appropriate
|
|
185
|
+
comment syntax for the file format. We also recommend that a
|
|
186
|
+
file or class name and description of purpose be included on the
|
|
187
|
+
same "printed page" as the copyright notice for easier
|
|
188
|
+
identification within third-party archives.
|
|
189
|
+
|
|
190
|
+
Copyright {yyyy} {name of copyright owner}
|
|
191
|
+
|
|
192
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
193
|
+
you may not use this file except in compliance with the License.
|
|
194
|
+
You may obtain a copy of the License at
|
|
195
|
+
|
|
196
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
197
|
+
|
|
198
|
+
Unless required by applicable law or agreed to in writing, software
|
|
199
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
200
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
201
|
+
See the License for the specific language governing permissions and
|
|
202
|
+
limitations under the License.
|
|
@@ -0,0 +1,104 @@
|
|
|
1
|
+
Metadata-Version: 2.3
|
|
2
|
+
Name: python-picnic-api2
|
|
3
|
+
Version: 1.1.0
|
|
4
|
+
Summary:
|
|
5
|
+
License: Apache-2.0
|
|
6
|
+
Author: Mike Brink
|
|
7
|
+
Author-email: mjh.brink@icloud.com
|
|
8
|
+
Requires-Python: >=3.6,<4.0
|
|
9
|
+
Classifier: License :: OSI Approved :: Apache Software License
|
|
10
|
+
Classifier: Programming Language :: Python :: 3
|
|
11
|
+
Classifier: Programming Language :: Python :: 3.6
|
|
12
|
+
Classifier: Programming Language :: Python :: 3.7
|
|
13
|
+
Classifier: Programming Language :: Python :: 3.8
|
|
14
|
+
Classifier: Programming Language :: Python :: 3.9
|
|
15
|
+
Classifier: Programming Language :: Python :: 3.10
|
|
16
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
17
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
18
|
+
Classifier: Programming Language :: Python :: 3.13
|
|
19
|
+
Requires-Dist: requests (>=2.24.0,<3.0.0)
|
|
20
|
+
Project-URL: Repository, https://github.com/codesalatdev/python-picnic-api
|
|
21
|
+
Description-Content-Type: text/x-rst
|
|
22
|
+
|
|
23
|
+
"""""""""""""""""
|
|
24
|
+
Python-Picnic-API
|
|
25
|
+
"""""""""""""""""
|
|
26
|
+
|
|
27
|
+
.. image:: https://camo.githubusercontent.com/cd005dca0ef55d7725912ec03a936d3a7c8de5b5/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f6275792532306d6525323061253230636f666665652d646f6e6174652d79656c6c6f772e737667
|
|
28
|
+
:target: https://www.buymeacoffee.com/MikeBrink
|
|
29
|
+
:alt: Buy me a coffee
|
|
30
|
+
|
|
31
|
+
Unofficial Python wrapper for the Picnic_ API. While not all API methods have been implemented yet, you'll find most of what you need to build a working application are available.
|
|
32
|
+
|
|
33
|
+
This library is not affiliated with Picnic and retrieves data from the endpoints of the mobile application. Use at your own risk.
|
|
34
|
+
|
|
35
|
+
.. _Picnic: https://picnic.app/nl/
|
|
36
|
+
|
|
37
|
+
===============
|
|
38
|
+
Getting started
|
|
39
|
+
===============
|
|
40
|
+
The easiest way to install is directly from pip::
|
|
41
|
+
|
|
42
|
+
$ pip install python-picnic-api
|
|
43
|
+
|
|
44
|
+
|
|
45
|
+
-----
|
|
46
|
+
Usage
|
|
47
|
+
-----
|
|
48
|
+
I'll go over a few common operations here you'll most likely use in applications.
|
|
49
|
+
To login:
|
|
50
|
+
|
|
51
|
+
.. code-block:: python
|
|
52
|
+
|
|
53
|
+
from python_picnic_api import PicnicAPI
|
|
54
|
+
|
|
55
|
+
picnic = PicnicAPI(username='username', password='password', country_code="NL")
|
|
56
|
+
|
|
57
|
+
The country_code parameter defaults to NL, but you have to change it if you live in a different country than the Netherlands (Germany: DE, Belgium: BE).
|
|
58
|
+
You can also store your credentials by setting the store value to true, this will store your credentials and country_code in /config/app.yaml.
|
|
59
|
+
|
|
60
|
+
Searching for a product
|
|
61
|
+
-----------------------
|
|
62
|
+
.. code-block:: python
|
|
63
|
+
|
|
64
|
+
>>> picnic.search('coffee')
|
|
65
|
+
[{'type': 'CATEGORY', 'id': 'coffee', 'links': [{'type': 'SEARCH', 'href': 'https://storefront-prod.nl.picnicinternational.com/api/15/search?search_term=coffee'}], 'name': 'coffee', 'items': [{'type': 'SINGLE_ARTICLE', 'id': '10511523', 'decorators': [{'type': 'UNIT_QUANTITY', 'unit_quantity_text': '500 gram'}], 'name': 'Lavazza espresso koffiebonen', 'display_price': 599, 'price': 599, 'image_id': 'd3fb2888fc41514bc06dfd6b52f8622cc222d017d2651501f227a537915fcc4f', 'max_count': 50, 'unit_quantity': '500 gram', 'unit_quantity_sub': '€11.98/kg', 'tags': []}, ...
|
|
66
|
+
|
|
67
|
+
Check cart
|
|
68
|
+
----------
|
|
69
|
+
.. code-block:: python
|
|
70
|
+
|
|
71
|
+
>>> picnic.get_cart()
|
|
72
|
+
{'type': 'ORDER', 'id': 'shopping_cart', 'items': [], 'delivery_slots': [...
|
|
73
|
+
|
|
74
|
+
|
|
75
|
+
Manipulating your cart
|
|
76
|
+
----------------------
|
|
77
|
+
All of these methods will return the shopping cart.
|
|
78
|
+
|
|
79
|
+
.. code-block:: python
|
|
80
|
+
|
|
81
|
+
# adding 2 'Lavazza espresso koffiebonen' to cart
|
|
82
|
+
picnic.add_product('10511523', count=2)
|
|
83
|
+
|
|
84
|
+
# removing 1 'Lavazza espresso koffiebonen' from cart
|
|
85
|
+
picnic.remove_product('10511523')
|
|
86
|
+
|
|
87
|
+
# clearing the cart
|
|
88
|
+
picnic.clear_cart()
|
|
89
|
+
|
|
90
|
+
See upcoming deliveries
|
|
91
|
+
------------------------
|
|
92
|
+
.. code-block:: python
|
|
93
|
+
|
|
94
|
+
>>> picnic.get_current_deliveries()
|
|
95
|
+
[]
|
|
96
|
+
|
|
97
|
+
|
|
98
|
+
See available delivery slots
|
|
99
|
+
----------------------------
|
|
100
|
+
.. code-block:: python
|
|
101
|
+
|
|
102
|
+
>>> picnic.get_delivery_slots()
|
|
103
|
+
|
|
104
|
+
|
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
"""""""""""""""""
|
|
2
|
+
Python-Picnic-API
|
|
3
|
+
"""""""""""""""""
|
|
4
|
+
|
|
5
|
+
.. image:: https://camo.githubusercontent.com/cd005dca0ef55d7725912ec03a936d3a7c8de5b5/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f6275792532306d6525323061253230636f666665652d646f6e6174652d79656c6c6f772e737667
|
|
6
|
+
:target: https://www.buymeacoffee.com/MikeBrink
|
|
7
|
+
:alt: Buy me a coffee
|
|
8
|
+
|
|
9
|
+
Unofficial Python wrapper for the Picnic_ API. While not all API methods have been implemented yet, you'll find most of what you need to build a working application are available.
|
|
10
|
+
|
|
11
|
+
This library is not affiliated with Picnic and retrieves data from the endpoints of the mobile application. Use at your own risk.
|
|
12
|
+
|
|
13
|
+
.. _Picnic: https://picnic.app/nl/
|
|
14
|
+
|
|
15
|
+
===============
|
|
16
|
+
Getting started
|
|
17
|
+
===============
|
|
18
|
+
The easiest way to install is directly from pip::
|
|
19
|
+
|
|
20
|
+
$ pip install python-picnic-api
|
|
21
|
+
|
|
22
|
+
|
|
23
|
+
-----
|
|
24
|
+
Usage
|
|
25
|
+
-----
|
|
26
|
+
I'll go over a few common operations here you'll most likely use in applications.
|
|
27
|
+
To login:
|
|
28
|
+
|
|
29
|
+
.. code-block:: python
|
|
30
|
+
|
|
31
|
+
from python_picnic_api import PicnicAPI
|
|
32
|
+
|
|
33
|
+
picnic = PicnicAPI(username='username', password='password', country_code="NL")
|
|
34
|
+
|
|
35
|
+
The country_code parameter defaults to NL, but you have to change it if you live in a different country than the Netherlands (Germany: DE, Belgium: BE).
|
|
36
|
+
You can also store your credentials by setting the store value to true, this will store your credentials and country_code in /config/app.yaml.
|
|
37
|
+
|
|
38
|
+
Searching for a product
|
|
39
|
+
-----------------------
|
|
40
|
+
.. code-block:: python
|
|
41
|
+
|
|
42
|
+
>>> picnic.search('coffee')
|
|
43
|
+
[{'type': 'CATEGORY', 'id': 'coffee', 'links': [{'type': 'SEARCH', 'href': 'https://storefront-prod.nl.picnicinternational.com/api/15/search?search_term=coffee'}], 'name': 'coffee', 'items': [{'type': 'SINGLE_ARTICLE', 'id': '10511523', 'decorators': [{'type': 'UNIT_QUANTITY', 'unit_quantity_text': '500 gram'}], 'name': 'Lavazza espresso koffiebonen', 'display_price': 599, 'price': 599, 'image_id': 'd3fb2888fc41514bc06dfd6b52f8622cc222d017d2651501f227a537915fcc4f', 'max_count': 50, 'unit_quantity': '500 gram', 'unit_quantity_sub': '€11.98/kg', 'tags': []}, ...
|
|
44
|
+
|
|
45
|
+
Check cart
|
|
46
|
+
----------
|
|
47
|
+
.. code-block:: python
|
|
48
|
+
|
|
49
|
+
>>> picnic.get_cart()
|
|
50
|
+
{'type': 'ORDER', 'id': 'shopping_cart', 'items': [], 'delivery_slots': [...
|
|
51
|
+
|
|
52
|
+
|
|
53
|
+
Manipulating your cart
|
|
54
|
+
----------------------
|
|
55
|
+
All of these methods will return the shopping cart.
|
|
56
|
+
|
|
57
|
+
.. code-block:: python
|
|
58
|
+
|
|
59
|
+
# adding 2 'Lavazza espresso koffiebonen' to cart
|
|
60
|
+
picnic.add_product('10511523', count=2)
|
|
61
|
+
|
|
62
|
+
# removing 1 'Lavazza espresso koffiebonen' from cart
|
|
63
|
+
picnic.remove_product('10511523')
|
|
64
|
+
|
|
65
|
+
# clearing the cart
|
|
66
|
+
picnic.clear_cart()
|
|
67
|
+
|
|
68
|
+
See upcoming deliveries
|
|
69
|
+
------------------------
|
|
70
|
+
.. code-block:: python
|
|
71
|
+
|
|
72
|
+
>>> picnic.get_current_deliveries()
|
|
73
|
+
[]
|
|
74
|
+
|
|
75
|
+
|
|
76
|
+
See available delivery slots
|
|
77
|
+
----------------------------
|
|
78
|
+
.. code-block:: python
|
|
79
|
+
|
|
80
|
+
>>> picnic.get_delivery_slots()
|
|
81
|
+
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
[tool.poetry]
|
|
2
|
+
name = "python-picnic-api2"
|
|
3
|
+
version = "1.1.0"
|
|
4
|
+
description = ""
|
|
5
|
+
license = "Apache-2.0"
|
|
6
|
+
authors = ["Mike Brink <mjh.brink@icloud.com>", "Noah Groß <pypi@codesalat.dev>"]
|
|
7
|
+
readme = "README.rst"
|
|
8
|
+
repository = "https://github.com/codesalatdev/python-picnic-api"
|
|
9
|
+
|
|
10
|
+
[tool.poetry.dependencies]
|
|
11
|
+
python = "^3.6"
|
|
12
|
+
requests = "^2.24.0"
|
|
13
|
+
|
|
14
|
+
[tool.poetry.dev-dependencies]
|
|
15
|
+
pytest = "^5.2"
|
|
16
|
+
flake8 = "^3.8.3"
|
|
17
|
+
black = {version = "^19.10b0", allow-prereleases = true}
|
|
18
|
+
python-dotenv = "^0.15.0"
|
|
19
|
+
|
|
20
|
+
[build-system]
|
|
21
|
+
requires = ["poetry-core"]
|
|
22
|
+
build-backend = "poetry.core.masonry.api"
|
|
@@ -0,0 +1,164 @@
|
|
|
1
|
+
from hashlib import md5
|
|
2
|
+
|
|
3
|
+
from .helper import _tree_generator, _url_generator, _get_category_name, _extract_search_results
|
|
4
|
+
from .session import PicnicAPISession, PicnicAuthError
|
|
5
|
+
|
|
6
|
+
DEFAULT_URL = "https://storefront-prod.{}.picnicinternational.com/api/{}"
|
|
7
|
+
GLOBAL_GATEWAY_URL = "https://gateway-prod.global.picnicinternational.com"
|
|
8
|
+
DEFAULT_COUNTRY_CODE = "NL"
|
|
9
|
+
DEFAULT_API_VERSION = "15"
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
class PicnicAPI:
|
|
13
|
+
def __init__(
|
|
14
|
+
self, username: str = None, password: str = None,
|
|
15
|
+
country_code: str = DEFAULT_COUNTRY_CODE, auth_token: str = None
|
|
16
|
+
):
|
|
17
|
+
self._country_code = country_code
|
|
18
|
+
self._base_url = _url_generator(
|
|
19
|
+
DEFAULT_URL, self._country_code, DEFAULT_API_VERSION
|
|
20
|
+
)
|
|
21
|
+
|
|
22
|
+
self.session = PicnicAPISession(auth_token=auth_token)
|
|
23
|
+
|
|
24
|
+
# Login if not authenticated
|
|
25
|
+
if not self.session.authenticated and username and password:
|
|
26
|
+
self.login(username, password)
|
|
27
|
+
|
|
28
|
+
self.high_level_categories = None
|
|
29
|
+
|
|
30
|
+
def initialize_high_level_categories(self):
|
|
31
|
+
"""Initialize high-level categories once to avoid multiple requests."""
|
|
32
|
+
if not self.high_level_categories:
|
|
33
|
+
self.high_level_categories = self.get_categories(depth=1)
|
|
34
|
+
|
|
35
|
+
def _get(self, path: str, add_picnic_headers=False):
|
|
36
|
+
url = self._base_url + path
|
|
37
|
+
|
|
38
|
+
# Make the request, add special picnic headers if needed
|
|
39
|
+
headers = {
|
|
40
|
+
"x-picnic-agent": "30100;1.15.272-15295;",
|
|
41
|
+
"x-picnic-did": "3C417201548B2E3B"
|
|
42
|
+
} if add_picnic_headers else None
|
|
43
|
+
response = self.session.get(url, headers=headers).json()
|
|
44
|
+
|
|
45
|
+
if self._contains_auth_error(response):
|
|
46
|
+
raise PicnicAuthError("Picnic authentication error")
|
|
47
|
+
|
|
48
|
+
return response
|
|
49
|
+
|
|
50
|
+
def _post(self, path: str, data=None, base_url_override=None):
|
|
51
|
+
url = (self._base_url if not base_url_override else base_url_override) + path
|
|
52
|
+
response = self.session.post(url, json=data).json()
|
|
53
|
+
|
|
54
|
+
if self._contains_auth_error(response):
|
|
55
|
+
raise PicnicAuthError(f"Picnic authentication error: {response['error'].get('message')}")
|
|
56
|
+
|
|
57
|
+
return response
|
|
58
|
+
|
|
59
|
+
@staticmethod
|
|
60
|
+
def _contains_auth_error(response):
|
|
61
|
+
if not isinstance(response, dict):
|
|
62
|
+
return False
|
|
63
|
+
|
|
64
|
+
error_code = response.setdefault("error", {}).get("code")
|
|
65
|
+
return error_code == "AUTH_ERROR" or error_code == "AUTH_INVALID_CRED"
|
|
66
|
+
|
|
67
|
+
def login(self, username: str, password: str):
|
|
68
|
+
path = "/user/login"
|
|
69
|
+
secret = md5(password.encode("utf-8")).hexdigest()
|
|
70
|
+
data = {"key": username, "secret": secret, "client_id": 30100}
|
|
71
|
+
|
|
72
|
+
return self._post(path, data)
|
|
73
|
+
|
|
74
|
+
def logged_in(self):
|
|
75
|
+
return self.session.authenticated
|
|
76
|
+
|
|
77
|
+
def get_user(self):
|
|
78
|
+
return self._get("/user")
|
|
79
|
+
|
|
80
|
+
def search(self, term: str):
|
|
81
|
+
path = f"/pages/search-page-results?search_term={term}"
|
|
82
|
+
raw_results = self._get(path, add_picnic_headers=True)
|
|
83
|
+
return _extract_search_results(raw_results)
|
|
84
|
+
|
|
85
|
+
def get_lists(self, list_id: str = None):
|
|
86
|
+
if list_id:
|
|
87
|
+
path = "/lists/" + list_id
|
|
88
|
+
else:
|
|
89
|
+
path = "/lists"
|
|
90
|
+
return self._get(path)
|
|
91
|
+
|
|
92
|
+
def get_sublist(self, list_id: str, sublist_id: str) -> list:
|
|
93
|
+
"""Get sublist.
|
|
94
|
+
|
|
95
|
+
Args:
|
|
96
|
+
list_id (str): ID of list, corresponding to requested sublist.
|
|
97
|
+
sublist_id (str): ID of sublist.
|
|
98
|
+
|
|
99
|
+
Returns:
|
|
100
|
+
list: Sublist result.
|
|
101
|
+
"""
|
|
102
|
+
return self._get(f"/lists/{list_id}?sublist={sublist_id}")
|
|
103
|
+
|
|
104
|
+
def get_cart(self):
|
|
105
|
+
return self._get("/cart")
|
|
106
|
+
|
|
107
|
+
def get_article(self, article_id: str, add_category_name=False):
|
|
108
|
+
path = "/articles/" + article_id
|
|
109
|
+
article = self._get(path)
|
|
110
|
+
if add_category_name and "category_link" in article:
|
|
111
|
+
self.initialize_high_level_categories()
|
|
112
|
+
article.update(
|
|
113
|
+
category_name=_get_category_name(article['category_link'], self.high_level_categories)
|
|
114
|
+
)
|
|
115
|
+
return article
|
|
116
|
+
|
|
117
|
+
def get_article_category(self, article_id: str):
|
|
118
|
+
path = "/articles/" + article_id + "/category"
|
|
119
|
+
return self._get(path)
|
|
120
|
+
|
|
121
|
+
def add_product(self, product_id: str, count: int = 1):
|
|
122
|
+
data = {"product_id": product_id, "count": count}
|
|
123
|
+
return self._post("/cart/add_product", data)
|
|
124
|
+
|
|
125
|
+
def remove_product(self, product_id: str, count: int = 1):
|
|
126
|
+
data = {"product_id": product_id, "count": count}
|
|
127
|
+
return self._post("/cart/remove_product", data)
|
|
128
|
+
|
|
129
|
+
def clear_cart(self):
|
|
130
|
+
return self._post("/cart/clear")
|
|
131
|
+
|
|
132
|
+
def get_delivery_slots(self):
|
|
133
|
+
return self._get("/cart/delivery_slots")
|
|
134
|
+
|
|
135
|
+
def get_delivery(self, delivery_id: str):
|
|
136
|
+
path = "/deliveries/" + delivery_id
|
|
137
|
+
return self._get(path)
|
|
138
|
+
|
|
139
|
+
def get_delivery_scenario(self, delivery_id: str):
|
|
140
|
+
path = "/deliveries/" + delivery_id + "/scenario"
|
|
141
|
+
return self._get(path, add_picnic_headers=True)
|
|
142
|
+
|
|
143
|
+
def get_delivery_position(self, delivery_id: str):
|
|
144
|
+
path = "/deliveries/" + delivery_id + "/position"
|
|
145
|
+
return self._get(path, add_picnic_headers=True)
|
|
146
|
+
|
|
147
|
+
def get_deliveries(self, summary: bool = False, data=None):
|
|
148
|
+
data = [] if data is None else data
|
|
149
|
+
if summary:
|
|
150
|
+
return self._post("/deliveries/summary", data=data)
|
|
151
|
+
return self._post("/deliveries", data=data)
|
|
152
|
+
|
|
153
|
+
def get_current_deliveries(self):
|
|
154
|
+
return self.get_deliveries(data=["CURRENT"])
|
|
155
|
+
|
|
156
|
+
def get_categories(self, depth: int = 0):
|
|
157
|
+
return self._get(f"/my_store?depth={depth}")["catalog"]
|
|
158
|
+
|
|
159
|
+
def print_categories(self, depth: int = 0):
|
|
160
|
+
tree = "\n".join(_tree_generator(self.get_categories(depth=depth)))
|
|
161
|
+
print(tree)
|
|
162
|
+
|
|
163
|
+
|
|
164
|
+
__all__ = ["PicnicAPI"]
|
|
@@ -0,0 +1,111 @@
|
|
|
1
|
+
import json
|
|
2
|
+
import re
|
|
3
|
+
from typing import List, Dict, Any, Optional
|
|
4
|
+
|
|
5
|
+
# prefix components:
|
|
6
|
+
space = " "
|
|
7
|
+
branch = "│ "
|
|
8
|
+
# pointers:
|
|
9
|
+
tee = "├── "
|
|
10
|
+
last = "└── "
|
|
11
|
+
|
|
12
|
+
IMAGE_SIZES = ["small", "medium", "regular", "large", "extra-large"]
|
|
13
|
+
IMAGE_BASE_URL = "https://storefront-prod.nl.picnicinternational.com/static/images"
|
|
14
|
+
|
|
15
|
+
SOLE_ARTICLE_ID_PATTERN = re.compile(r'"sole_article_id":"(\w+)"')
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
def _tree_generator(response: list, prefix: str = ""):
|
|
19
|
+
"""A recursive tree generator,
|
|
20
|
+
will yield a visual tree structure line by line
|
|
21
|
+
with each line prefixed by the same characters
|
|
22
|
+
"""
|
|
23
|
+
# response each get pointers that are ├── with a final └── :
|
|
24
|
+
pointers = [tee] * (len(response) - 1) + [last]
|
|
25
|
+
for pointer, item in zip(pointers, response):
|
|
26
|
+
if "name" in item: # print the item
|
|
27
|
+
pre = ""
|
|
28
|
+
if "unit_quantity" in item.keys():
|
|
29
|
+
pre = f"{item['unit_quantity']} "
|
|
30
|
+
after = ""
|
|
31
|
+
if "display_price" in item.keys():
|
|
32
|
+
after = f" €{int(item['display_price'])/100.0:.2f}"
|
|
33
|
+
|
|
34
|
+
yield prefix + pointer + pre + item["name"] + after
|
|
35
|
+
if "items" in item: # extend the prefix and recurse:
|
|
36
|
+
extension = branch if pointer == tee else space
|
|
37
|
+
# i.e. space because last, └── , above so no more |
|
|
38
|
+
yield from _tree_generator(item["items"], prefix=prefix + extension)
|
|
39
|
+
|
|
40
|
+
|
|
41
|
+
def _url_generator(url: str, country_code: str, api_version: str):
|
|
42
|
+
return url.format(country_code.lower(), api_version)
|
|
43
|
+
|
|
44
|
+
|
|
45
|
+
def _get_category_id_from_link(category_link: str) -> Optional[str]:
|
|
46
|
+
pattern = r"categories/(\d+)"
|
|
47
|
+
first_number = re.search(pattern, category_link)
|
|
48
|
+
if first_number:
|
|
49
|
+
result = str(first_number.group(1))
|
|
50
|
+
return result
|
|
51
|
+
else:
|
|
52
|
+
return None
|
|
53
|
+
|
|
54
|
+
|
|
55
|
+
def _get_category_name(category_link: str, categories: list) -> Optional[str]:
|
|
56
|
+
category_id = _get_category_id_from_link(category_link)
|
|
57
|
+
if category_id:
|
|
58
|
+
category = next(
|
|
59
|
+
(item for item in categories if item["id"] == category_id), None
|
|
60
|
+
)
|
|
61
|
+
if category:
|
|
62
|
+
return category["name"]
|
|
63
|
+
else:
|
|
64
|
+
return None
|
|
65
|
+
else:
|
|
66
|
+
return None
|
|
67
|
+
|
|
68
|
+
|
|
69
|
+
def get_recipe_image(id: str, size="regular"):
|
|
70
|
+
sizes = IMAGE_SIZES + ["1250x1250"]
|
|
71
|
+
assert size in sizes, "size must be one of: " + ", ".join(sizes)
|
|
72
|
+
return f"{IMAGE_BASE_URL}/recipes/{id}/{size}.png"
|
|
73
|
+
|
|
74
|
+
|
|
75
|
+
def get_image(id: str, size="regular", suffix="webp"):
|
|
76
|
+
assert (
|
|
77
|
+
"tile" in size if suffix == "webp" else True
|
|
78
|
+
), "webp format only supports tile sizes"
|
|
79
|
+
assert suffix in ["webp", "png"], "suffix must be webp or png"
|
|
80
|
+
sizes = IMAGE_SIZES + [f"tile-{size}" for size in IMAGE_SIZES]
|
|
81
|
+
|
|
82
|
+
assert size in sizes, "size must be one of: " + ", ".join(sizes)
|
|
83
|
+
return f"{IMAGE_BASE_URL}/{id}/{size}.{suffix}"
|
|
84
|
+
|
|
85
|
+
def _extract_search_results(raw_results, max_items: int = 10):
|
|
86
|
+
"""Extract search results from the nested dictionary structure returned by Picnic search.
|
|
87
|
+
Number of max items can be defined to reduce excessive nested search"""
|
|
88
|
+
search_results = []
|
|
89
|
+
|
|
90
|
+
def find_articles(node):
|
|
91
|
+
if len(search_results) >= max_items:
|
|
92
|
+
return
|
|
93
|
+
|
|
94
|
+
content = node.get("content", {})
|
|
95
|
+
if content.get("type") == "SELLING_UNIT_TILE" and "sellingUnit" in content:
|
|
96
|
+
selling_unit = content["sellingUnit"]
|
|
97
|
+
sole_article_ids = SOLE_ARTICLE_ID_PATTERN.findall(json.dumps(node))
|
|
98
|
+
sole_article_id = sole_article_ids[0] if sole_article_ids else None
|
|
99
|
+
result_entry = {
|
|
100
|
+
**selling_unit,
|
|
101
|
+
"sole_article_id": sole_article_id,
|
|
102
|
+
}
|
|
103
|
+
search_results.append(result_entry)
|
|
104
|
+
|
|
105
|
+
for child in node.get("children", []):
|
|
106
|
+
find_articles(child)
|
|
107
|
+
|
|
108
|
+
body = raw_results.get("body", {})
|
|
109
|
+
find_articles(body.get("child", {}))
|
|
110
|
+
|
|
111
|
+
return [{"items": search_results}]
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
from requests import Response, Session
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
class PicnicAuthError(Exception):
|
|
5
|
+
"""Indicates an error when authenticating to the Picnic API."""
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
class PicnicAPISession(Session):
|
|
9
|
+
AUTH_HEADER = "x-picnic-auth"
|
|
10
|
+
|
|
11
|
+
def __init__(self, auth_token: str = None):
|
|
12
|
+
super().__init__()
|
|
13
|
+
self._auth_token = auth_token
|
|
14
|
+
|
|
15
|
+
self.headers.update(
|
|
16
|
+
{
|
|
17
|
+
"User-Agent": "okhttp/3.9.0",
|
|
18
|
+
"Content-Type": "application/json; charset=UTF-8",
|
|
19
|
+
self.AUTH_HEADER: self._auth_token
|
|
20
|
+
}
|
|
21
|
+
)
|
|
22
|
+
|
|
23
|
+
@property
|
|
24
|
+
def authenticated(self):
|
|
25
|
+
"""Returns whether the user is authenticated by checking if the authentication token is set."""
|
|
26
|
+
return bool(self._auth_token)
|
|
27
|
+
|
|
28
|
+
@property
|
|
29
|
+
def auth_token(self):
|
|
30
|
+
"""Returns the auth token."""
|
|
31
|
+
return self._auth_token
|
|
32
|
+
|
|
33
|
+
def _update_auth_token(self, auth_token):
|
|
34
|
+
"""Update the auth token if not None and changed."""
|
|
35
|
+
if auth_token and auth_token != self._auth_token:
|
|
36
|
+
self._auth_token = auth_token
|
|
37
|
+
self.headers.update({self.AUTH_HEADER: self._auth_token})
|
|
38
|
+
|
|
39
|
+
def get(self, url, **kwargs) -> Response:
|
|
40
|
+
"""Do a GET request and update the auth token if set."""
|
|
41
|
+
response = super(PicnicAPISession, self).get(url, **kwargs)
|
|
42
|
+
self._update_auth_token(response.headers.get(self.AUTH_HEADER))
|
|
43
|
+
|
|
44
|
+
return response
|
|
45
|
+
|
|
46
|
+
def post(self, url, data=None, json=None, **kwargs) -> Response:
|
|
47
|
+
"""Do a POST request and update the auth token if set."""
|
|
48
|
+
response = super(PicnicAPISession, self).post(url, data, json, **kwargs)
|
|
49
|
+
self._update_auth_token(response.headers.get(self.AUTH_HEADER))
|
|
50
|
+
|
|
51
|
+
return response
|
|
52
|
+
|
|
53
|
+
|
|
54
|
+
__all__ = ["PicnicAuthError", "PicnicAPISession"]
|