forked from sundial-org/awesome-openclaw-skills
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpicnic-cli.mjs
More file actions
350 lines (323 loc) · 10.6 KB
/
Copy pathpicnic-cli.mjs
File metadata and controls
350 lines (323 loc) · 10.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
#!/usr/bin/env node
/**
* Picnic CLI - Wrapper for the picnic-api npm package
* Usage: node picnic-cli.mjs <command> [args]
*/
import PicnicClient from 'picnic-api';
import fs from 'fs';
import path from 'path';
import os from 'os';
const CONFIG_PATH = path.join(os.homedir(), '.config', 'picnic', 'config.json');
function loadConfig() {
try {
if (fs.existsSync(CONFIG_PATH)) {
return JSON.parse(fs.readFileSync(CONFIG_PATH, 'utf8'));
}
} catch (e) {
// ignore
}
return {};
}
function saveConfig(config) {
const dir = path.dirname(CONFIG_PATH);
if (!fs.existsSync(dir)) {
fs.mkdirSync(dir, { recursive: true });
}
fs.writeFileSync(CONFIG_PATH, JSON.stringify(config, null, 2), { mode: 0o600 });
}
function getClient() {
const config = loadConfig();
if (!config.authKey) {
console.error(JSON.stringify({ error: 'Not logged in. Run: picnic login <email> <password>' }));
process.exit(1);
}
return new PicnicClient({
countryCode: config.countryCode || 'DE',
authKey: config.authKey
});
}
function output(data) {
console.log(JSON.stringify(data, null, 2));
}
async function main() {
const [,, command, ...args] = process.argv;
try {
switch (command) {
case 'login': {
const [email, password, countryCode = 'DE'] = args;
if (!email || !password) {
output({ error: 'Usage: picnic login <email> <password> [countryCode=DE]' });
process.exit(1);
}
const client = new PicnicClient({ countryCode });
const result = await client.login(email, password);
if (result.second_factor_authentication_required) {
// Save partial config, user needs to verify 2FA
saveConfig({
authKey: result.authKey,
countryCode,
needs2FA: true
});
output({
status: '2fa_required',
message: 'Two-factor authentication required. Run: picnic verify-2fa <code>',
authKey: result.authKey
});
} else {
saveConfig({ authKey: result.authKey, countryCode });
output({ status: 'ok', message: 'Logged in successfully' });
}
break;
}
case 'verify-2fa': {
const [code] = args;
if (!code) {
output({ error: 'Usage: picnic verify-2fa <code>' });
process.exit(1);
}
const client = getClient();
const result = await client.verify2FACode(code);
const config = loadConfig();
delete config.needs2FA;
saveConfig(config);
output({ status: 'ok', message: '2FA verified successfully' });
break;
}
case 'send-2fa': {
const [channel = 'SMS'] = args;
const client = getClient();
await client.generate2FACode(channel);
output({ status: 'ok', message: `2FA code sent via ${channel}` });
break;
}
case 'status': {
const config = loadConfig();
if (config.authKey) {
const client = getClient();
try {
const user = await client.getUserDetails();
output({
status: 'logged_in',
user: {
firstName: user.firstname,
lastName: user.lastname,
email: user.contact_email,
address: user.address
}
});
} catch (e) {
output({ status: 'error', message: 'Auth key expired, please login again' });
}
} else {
output({ status: 'not_logged_in' });
}
break;
}
case 'search': {
const query = args.join(' ');
if (!query) {
output({ error: 'Usage: picnic search <query>' });
process.exit(1);
}
const client = getClient();
const results = await client.search(query);
output({
query,
count: results.length,
products: results.slice(0, 20).map(p => ({
id: p.id,
name: p.name,
price: p.display_price,
unit: p.unit_quantity,
available: p.max_count > 0
}))
});
break;
}
case 'cart': {
const client = getClient();
const cart = await client.getShoppingCart();
output({
itemCount: cart.total_count,
totalPrice: cart.total_price,
items: (cart.items || []).map(item => ({
id: item.items?.[0]?.id,
name: item.items?.[0]?.name,
quantity: item.items?.[0]?.decorators?.find(d => d.type === 'QUANTITY')?.quantity,
price: item.items?.[0]?.display_price
})).filter(i => i.id)
});
break;
}
case 'add': {
const [productId, count = '1'] = args;
if (!productId) {
output({ error: 'Usage: picnic add <productId> [count=1]' });
process.exit(1);
}
const client = getClient();
const cart = await client.addProductToShoppingCart(productId, parseInt(count, 10));
output({
status: 'ok',
message: `Added ${count}x product to cart`,
totalItems: cart.total_count,
totalPrice: cart.total_price
});
break;
}
case 'remove': {
const [productId, count = '1'] = args;
if (!productId) {
output({ error: 'Usage: picnic remove <productId> [count=1]' });
process.exit(1);
}
const client = getClient();
const cart = await client.removeProductFromShoppingCart(productId, parseInt(count, 10));
output({
status: 'ok',
message: `Removed ${count}x product from cart`,
totalItems: cart.total_count,
totalPrice: cart.total_price
});
break;
}
case 'clear': {
const client = getClient();
await client.clearShoppingCart();
output({ status: 'ok', message: 'Cart cleared' });
break;
}
case 'slots': {
const client = getClient();
const slotsData = await client.getDeliverySlots();
const slots = [];
for (const slotGroup of slotsData.delivery_slots || []) {
for (const slot of slotGroup.slot_selector_data || []) {
if (slot.state === 'SELECTABLE') {
slots.push({
id: slot.slot_id,
date: slotGroup.start,
window: `${slot.window_start} - ${slot.window_end}`,
price: slot.price,
selected: slot.selected
});
}
}
}
output({ slots: slots.slice(0, 15) });
break;
}
case 'set-slot': {
const [slotId] = args;
if (!slotId) {
output({ error: 'Usage: picnic set-slot <slotId>' });
process.exit(1);
}
const client = getClient();
const cart = await client.setDeliverySlot(slotId);
output({ status: 'ok', message: 'Delivery slot set' });
break;
}
case 'deliveries': {
const client = getClient();
const deliveries = await client.getDeliveries();
output({
deliveries: deliveries.slice(0, 10).map(d => ({
id: d.delivery_id,
status: d.status,
date: d.slot?.window_start?.split('T')[0],
window: `${d.slot?.window_start?.split('T')[1]?.slice(0,5)} - ${d.slot?.window_end?.split('T')[1]?.slice(0,5)}`,
totalPrice: d.orders?.reduce((sum, o) => sum + (o.total_price || 0), 0)
}))
});
break;
}
case 'delivery': {
const [deliveryId] = args;
if (!deliveryId) {
output({ error: 'Usage: picnic delivery <deliveryId>' });
process.exit(1);
}
const client = getClient();
const delivery = await client.getDelivery(deliveryId);
// Extract items from orders
const items = [];
for (const order of delivery.orders || []) {
for (const line of order.items || []) {
const article = line.items?.[0];
if (article) {
const qty = article.decorators?.find(d => d.type === 'QUANTITY')?.quantity || 1;
items.push({
name: article.name,
quantity: qty,
unitSize: article.unit_quantity,
price: line.display_price
});
}
}
}
output({
id: delivery.delivery_id,
status: delivery.status,
date: delivery.slot?.window_start?.split('T')[0],
window: `${delivery.slot?.window_start?.split('T')[1]?.slice(0,5)} - ${delivery.slot?.window_end?.split('T')[1]?.slice(0,5)}`,
totalPrice: delivery.orders?.reduce((sum, o) => sum + (o.total_price || 0), 0),
items
});
break;
}
case 'categories': {
const client = getClient();
const categories = await client.getCategories(1);
output({
categories: (categories.catalog || []).map(c => ({
id: c.id,
name: c.name,
itemCount: c.items?.length
}))
});
break;
}
case 'user': {
const client = getClient();
const user = await client.getUserDetails();
output({
firstName: user.firstname,
lastName: user.lastname,
email: user.contact_email,
phone: user.phone,
address: user.address,
householdSize: user.household_details?.adults
});
break;
}
case 'help':
default:
output({
usage: 'picnic <command> [args]',
commands: {
'login <email> <password> [country=DE]': 'Login to Picnic (country: DE or NL)',
'verify-2fa <code>': 'Verify 2FA code after login',
'send-2fa [channel=SMS]': 'Request new 2FA code',
'status': 'Check login status',
'user': 'Show user details',
'search <query>': 'Search for products',
'cart': 'Show current cart',
'add <productId> [count]': 'Add product to cart',
'remove <productId> [count]': 'Remove product from cart',
'clear': 'Clear the cart',
'slots': 'Show available delivery slots',
'set-slot <slotId>': 'Select a delivery slot',
'deliveries': 'List past/current deliveries',
'delivery <id>': 'Get delivery details',
'categories': 'List product categories'
}
});
break;
}
} catch (error) {
output({ error: error.message });
process.exit(1);
}
}
main();