-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmayesh.js
408 lines (351 loc) · 17 KB
/
mayesh.js
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
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
const puppeteer = require('puppeteer');
// parse command line arguments
const args = process.argv.slice(2);
let deliveryDate = '';
let flowerNames = [];
// get argument by flag
function getArgValue(flag) {
const index = args.indexOf(flag);
return index > -1 ? args[index + 1] : null;
}
// extract values
// HARDCODE DEBUG
// deliveryDate = "2025-01-16"
// flowerNames = ["STOCK", "SNAPDRAGON", "SALAL", "DELPHINIUM", "ROSE", "CARNATION", "LISIANTHUS", "SCABIOSA", "MUMS", "RANUNCULUS", "ANEMONE", "EUCALYPTUS", "RUSCUS"];
deliveryDate = getArgValue('--deliveryDate') || '';
flowerNames = getArgValue('--flowerNames') ? getArgValue('--flowerNames').split(',') : [
"STOCK", "SNAPDRAGON", "SALAL", "DELPHINIUM", "ROSE", "CARNATION", "LISIANTHUS", "SCABIOSA", "MUMS", "RANUNCULUS", "ANEMONE", "EUCALYPTUS", "RUSCUS"
];
let numPages = 0;
(async () => {
let flowers = [];
let browser = null;
try {
//browser = await puppeteer.launch(); // launches puppeteer browser instance
const browser = await puppeteer.launch({
headless: true,
args: ['--no-sandbox', '--disable-setuid-sandbox'],
});
const page = await browser.newPage(); // opens new browser tab
//console.log("loaded browser")
// print browser console messages
// page.on('console', async msg => {
// const args = await Promise.all(msg.args().map(arg => arg.jsonValue()));
// if (args.length > 0 && args[0].includes("console:")) {
// console.log(`${args}`);
// }
// });
// login to mayesh
const loginUrl = "https://www.mayesh.com/login";
const username = "[email protected]";
const password = "MSHmorehappydayz5!";
await page.goto(loginUrl);
await page.waitForSelector('#input-email'); // wait for load
await page.type('#input-email', username);
await page.waitForSelector('#input-password'); // wait for load
await page.type('#input-password', password);
// submit login form
await page.click('.btn-primary');
//console.log("filled login info")
await page.waitForNavigation(); // wait for login
//console.log("login success")
// scrape ALL
let currentDate = new Date(deliveryDate);
numPages = 0;
try {
// format current date
const dateString = formatDate(currentDate);
//console.log("dateString:", dateString)
// navigate in current date url
const productPageUrl = `https://www.mayesh.com/shop?perPage=100&sortBy=Name-ASC&pageNumb=1&date=${dateString}&is_sales_rep=0&is_e_sales=0&criteria=%7B%7D&criteriaInt=%7B%7D&search=&s_search=`;
await page.goto(productPageUrl);
//await page.waitForNavigation();
//console.log("navigated to product page");
// access date that actually populates at url (will default to actual date if invalid)
await page.waitForSelector('div.input-group.flex-nowrap input.form-control');
let inputValue = await page.evaluate(() => {
const input = document.querySelector('div.input-group.flex-nowrap input.form-control');
//console.log("console: input element", input);
return input ? input.value : null;
});
const [month, day, year] = inputValue.split('-');
inputValue = `${year}-${month}-${day}`;
if (inputValue == dateString) {
//console.log("valid date");
flowers = await scrapeAllDate(page, dateString, flowers);
//console.log("scraped date:", dateString);
}
} catch (err) {
console.error(`error when scraping date ${formatDate(currentDate)}:`, err);
}
// scrape DUTCH
currentDate = new Date(deliveryDate);
numPages = 0;
//console.log("scraping dutch")
try {
//format current date
const dateString = formatDate(currentDate);
//console.log("dateString:", dateString)
// navigate in current date url
const productPageUrl = `https://www.mayesh.com/shop?perPage=100&sortBy=Name-ASC&pageNumb=1&date=${dateString}&is_sales_rep=0&is_e_sales=0&criteria=%7B%7D&criteriaInt=%7B%7D&search=&s_search=`;
await page.goto(productPageUrl);
//await page.waitForNavigation();
//console.log("navigated to product page");
// access date that actually populates at url (will default to actual date if invalid)
await page.waitForSelector('div.input-group.flex-nowrap input.form-control');
let inputValue = await page.evaluate(() => {
//const input = document.querySelector('div.input-group.flex-nowrap input.form-control');
const input = document.querySelector('div.input-group.flex-nowrap input.form-control');
//console.log("input element:", input);
return input ? input.value : null;
});
const [month, day, year] = inputValue.split('-');
inputValue = `${year}-${month}-${day}`;
//console.log("inputValue:", inputValue)
if (inputValue == dateString) {
//console.log("valid date");
flowers = await scrapeDutchDate(page, dateString, flowers);
//console.log("scraped date:", dateString);
}
} catch (err) {
console.error(`error when scraping date ${formatDate(currentDate)}:`, err);
}
} catch (err) {
console.error("error during login or page load:", err);
} finally {
if (browser) {
await browser.close();
//console.log("closed browser");
}
// shutdown
//console.log("scraped all data");
console.log(JSON.stringify(flowers));
}
})();
// format date object to YYYY-MM-DD
function formatDate(date) {
//console.log("format Date object", date);
const year = date.getUTCFullYear();
const month = String(date.getUTCMonth() + 1).padStart(2, '0');
const day = String(date.getUTCDate()).padStart(2, '0');
return `${year}-${month}-${day}`;
}
async function scrapeAllDate(page, date, flowers) {
// LIVE & LOCAL, MIAMI BOXLOT, FARM DIRECT
//console.log("scraping all at date:", date);
let hasNextPage = true;
while (hasNextPage) {
try {
//console.log("entered page loop")
await page.waitForSelector('#view-list'); // wait for the product list to load
const newFlowers = await extractFlowerData(page, flowerNames, date);
flowers = flowers.concat(newFlowers);
// check for next page
const isLastPage = await page.evaluate(() => {
const pagination = document.querySelector('ul.pagination');
const activePage = pagination.querySelector('li.page-item.active');
const nextPage = activePage.nextElementSibling;
return nextPage === null;
});
//console.log("is last page?", isLastPage);
if (isLastPage) {
hasNextPage = false;
}
else {
numPages += 1;
await page.click('ul.pagination li.page-item.active + li.page-item a.page-link');
await page.waitForNavigation();
//console.log("next page", numPages);
}
} catch (err) {
console.error("error during ALL pagination or scraping:", err);
hasNextPage = false;
}
}
return flowers;
}
async function extractFlowerData(page, flowerNames, currentDate) {
//console.log("extractFlowerData date arg:", date);
try {
await page.waitForSelector('.card-body.position-relative');
//console.log("product loaded")
// format date to MM/DD/YYYY
const [year, month, day] = currentDate.split('-');
date = `${month}/${day}/${year}`;
return await page.evaluate((flowerNames, date) => {
//console.log("console: extractFlowerData date arg:", date);
const items = document.querySelectorAll('.card-body.position-relative');
//console.log("console: items selected")
let flowersData = [];
// find farm corresponding to delivery date
let farm = '';
const checkboxElements = document.querySelectorAll('ul.filters li input[type="checkbox"]');
checkboxElements.forEach(checkbox => {
const parentLi = checkbox.parentElement;
if (parentLi) {
const text = parentLi.textContent.trim();
if (text.includes('Live & Local')) {
farm = 'LIVE & LOCAL';
} else if (text.includes('Farm Direct Boxlots')) {
farm = 'FARM DIRECT';
} else if (text.includes('Miami Direct Boxlots')) {
farm = 'MIAMI DIRECT';
}
}
});
items.forEach(item => {
// extracts flower name in all caps
const flowerNameElement = item.querySelector('.product-name a');
const flowerName = flowerNameElement ? flowerNameElement.innerText.trim().toUpperCase() : '';
// check if current name matches name from flowerNames list
const containsFlowerName = flowerNames.some(name => flowerName.includes(name));
// scrapes matching flowers
if (containsFlowerName) {
//console.log("console: name ", flowerName)
const flowerImageElement = item.querySelector('.product-img-wrap img');
const flowerImage = flowerImageElement ? flowerImageElement.getAttribute('src') : '';
//console.log("console: img link ", flowerImage)
const avail = item.querySelectorAll('.lot-size span');
const available = avail[0] ? avail[0].innerText.trim() + ' BU' : '';
//console.log("console: avail ", available)
let stemsPer = '';
if (avail[1]) {
stemsPer = avail[1].innerText.trim();
stemsPer = stemsPer.replace(/[()]/g, '');
stemsPer = stemsPer.replace(' stems', ' ST/BU');
}
//console.log("console: stems per ", stemsPer)
const pricePerUnitElement = item.querySelector('.price strong');
const pricePerUnitVal = pricePerUnitElement ? pricePerUnitElement.innerText.trim() : '0';
const numericValue = pricePerUnitVal.match(/[\d.]+/);
if (numericValue) {
stemPrice = parseFloat(numericValue[0]).toFixed(2);
} else {
stemPrice = '0';
}
let prices = '0';
const priceElement = item.querySelector('.lot-price');
const price = priceElement ? priceElement.innerText.trim().replace('Price', '').trim() : '';
if (price) prices = price.replace(' / bunch', ' /BU');
//console.log("console: stem and price", stemPrice, prices);
// delivery date
const delivery = date;
flowersData.push({
flowerName,
flowerImage,
prices,
stemPrice,
color: " ",
height: " ",
stemsPer,
seller: "Mayesh",
farm,
available,
delivery
});
}
});
return flowersData;
}, flowerNames, date);
} catch (err) {
console.error("error during all data extraction:", err);
return [];
}
}
async function scrapeDutchDate(page, date, flowers) {
// navigate to product page
//const productUrl = `https://www.mayesh.com/dutch-direct-boxlots?perPage=100&sortBy=Name-ASC&pageNumb=1&date=${date}&is_sales_rep=0&is_e_sales=0&criteria=%7B%7D&criteriaInt=%7B%7D&search=`;
//await page.goto(productUrl);
//console.log("navigated to dutch direct page")
let nextPageExist = true;
while (nextPageExist) {
try {
//console.log("entered page loop")
await page.waitForSelector('#view-list'); // wait for the product list to load
//console.log("page loaded")
const newFlowers = await extractDutchData(page, flowerNames, date);
flowers = flowers.concat(newFlowers);
// check for next page
const isLastPage = await page.evaluate(() => {
const pagination = document.querySelector('ul.pagination');
const activePage = pagination.querySelector('li.page-item.active');
const nextPage = activePage.nextElementSibling;
return nextPage === null;
});
//console.log("is last page?", isLastPage);
if (isLastPage) {
nextPageExist = false;
}
else {
numPages += 1;
await page.click('ul.pagination li.page-item.active + li.page-item a.page-link');
await page.waitForNavigation();
//console.log("next page", numPages);
}
} catch (err) {
console.error("error during dutch direct pagination or scraping:", err);
nextPageExist = false;
}
}
return flowers;
}
async function extractDutchData(page, flowerNames, currentDate) {
try {
await page.waitForSelector('.card-body.position-relative');
//console.log("product loaded")
// format date to MM/DD/YYYY
const [year, month, day] = currentDate.split('-');
date = `${month}/${day}/${year}`;
return await page.evaluate((flowerNames, date) => {
const items = document.querySelectorAll('.card-body.position-relative');
//console.log("console: items selected")
let flowersData = [];
items.forEach(item => {
// extracts flower name in all caps
const flowerNameElement = item.querySelector('.product-name');
let flowerName = flowerNameElement ? flowerNameElement.innerText.trim().toUpperCase() : '';
flowerName = flowerName.replace(/HOLEX FLOWER\s*$/i, '').trim();
// check if current name matches name from flowerNames list
const containsFlowerName = flowerNames.some(name => flowerName.includes(name));
// scrapes matching flowers
if (containsFlowerName) {
//console.log("console: name ", flowerName);
const flowerImageElement = item.querySelector('.product-img-wrap img');
const flowerImage = flowerImageElement ? flowerImageElement.getAttribute('src') : '';
//console.log("console: img link ", flowerImage)
const stemsElement = item.querySelector('.lot-size span');
let stemsPer = '';
if (stemsElement) {
stemsPer = stemsElement.innerText.trim();
stemsPer = stemsPer.replace(/[()]/g, '');
stemsPer = stemsPer.replace(' stems', ' ST/BX');
}
//console.log("console: stems per ", stemsPer);
const pricePerUnitElement = item.querySelector('.price b');
const pricePerUnitVal = pricePerUnitElement ? pricePerUnitElement.innerText.trim() : '0';
const numericValue = pricePerUnitVal.match(/[\d.]+/);
if (numericValue) {
stemPrice = parseFloat(numericValue[0]).toFixed(2);
} else {
stemPrice = '0';
}
let prices = '0';
const priceElement = item.querySelector('.lot-price');
const price = priceElement ? priceElement.innerText.trim().replace('Price', '').trim() : '';
if (price) prices = (price.replace(' / box', ' /BX'));
//console.log("console: stem and price", stemPrice, prices);
// missing
// scrape height
// const nameParts = flowerName.split(' ');
// const height = nameParts[nameParts.length - 1] || '';
const delivery = date;
//console.log("console: delivery, date", date);
}
});
return flowersData;
}, flowerNames, date);
} catch (err) {
console.error("error during dutch direct data extraction:", err);
return [];
}
}