-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.ts
150 lines (141 loc) · 5.61 KB
/
index.ts
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
import fetch from 'node-fetch';
import { JSDOM } from 'jsdom';
import * as puppeteer from 'puppeteer';
const INIT_URL = `http://www.jacksdonuts.com/donuts-coffee`;
let browser;
const runJacksDonuts = async () => {
let dom = await getPage(INIT_URL);
let types = [];
dom.window.document.querySelectorAll('.element').forEach(node => {
let titleNode = node.querySelector('.slide-title');
if (!titleNode) return;
let listNode = titleNode.nextSibling;
if (!listNode) return;
const type = {
name: titleNode.innerHTML,
id:
listNode.getAttribute('id') ||
titleNode.innerHTML.toLowerCase(),
items: [],
};
listNode.querySelectorAll('ul > li').forEach(n => {
let item: any = {};
item.name = n.querySelector('p').innerHTML;
item.alt = n.querySelector('img').getAttribute('alt');
item.img = n.querySelector('img').getAttribute('src');
type.items.push(item);
});
console.log(type);
types.push(type);
});
};
const runKrispyKreme = async () => {
// let page = await getPage2(`https://www.krispykreme.com/menu/doughnuts/`);
// let donuts = await page.evaluate(selector => {
// return Array.from(document.querySelectorAll(selector)).map(row => {
// let donut: any = {};
// donut.title = row.querySelector('.title').innerHTML;
// let url = row.querySelector(`a[href]`).getAttribute('href');
// donut.url = `https://www.krispykreme.com${url}`;
// donut.img = row.querySelector('img').getAttribute('src');
// donut.id = url.split('/').pop();
// donut.types = row.getAttribute('data-filter-names').split('|');
// return donut;
// });
// }, 'section.menu-list .container-fluid .menu-items-container .kk-filter-items li');
// let donutsDetails = [];
// // for (let i = 0; i < 5; i++) {
// for (let i = 0; i < donuts.length; i++) {
// let donut = donuts[i];
// console.log('Getting Details for ', donut.title);
// await page.goto(donut.url);
// let donutDetails = await page.evaluate(() => {
// let dd: any = {};
// let node: any = document.querySelector('.menu-detail');
// dd.bannerUrl = node
// .querySelector('.hero > img')
// .getAttribute('src');
// dd.name = node.querySelector('h1').innerText;
// dd.nutritionalFacts = node
// .querySelector('[data-track="item-detail-nutrition"]')
// .getAttribute('href');
// dd.description = node.querySelector('p').innerText;
// dd.id = window.location.href.split('/').pop();
// return dd;
// });
// console.log(donutDetails);
// donutsDetails.push(donutDetails);
// }
// console.log(donuts);
// let donutNodes = dom.window.document.querySelectorAll(
// // '.menu-list .container-fluid .row:nth-child(2) .menu-items-container ul.kk-filter-list li',
// 'section',
// );
// console.log(donutNodes.length);
// donutNodes.forEach(node => {
// console.log('node');
// let donut: any = {};
// donut.title = node.querySelector('.title').innerHTML;
// let url = node.querySelector(`a[href]`).getAttribute('href');
// donut.url = `https://www.krispykreme.com${url}`;
// donut.img = node.querySelector('img').getAttribute('src');
// donuts.push(donut);
// });
// console.log(donuts);
const page = await getPage2(`https://www.krispykreme.com/menu/doughnuts/`);
const content = await page.content();
const dom = new JSDOM(content);
const donuts = Array.from(
dom.window.document.querySelectorAll(
'section.menu-list .container-fluid .menu-items-container .kk-filter-items li',
),
).map((node: any) => {
let donut: any = {};
donut.title = node.querySelector('.title').innerHTML;
let url = node.querySelector(`a[href]`).getAttribute('href');
donut.url = `https://www.krispykreme.com${url}`;
donut.img = node.querySelector('img').getAttribute('src');
donut.id = url.split('/').pop();
donut.types = node.getAttribute('data-filter-names').split('|');
return donut;
});
const types = Array.from(
donuts.reduce((acc, donut) => {
donut.types.forEach(t => acc.add(t));
acc.delete('');
return acc;
}, new Set()),
);
for (let i = 0; i < donuts.length; i++) {
let donut = donuts[i];
// }
// await donuts.map(async donut => {
console.log('Visiting', donut.url);
await page.goto(donut.url);
const content = await page.content();
const dom = new JSDOM(content);
let node: any = dom.window.document.querySelector('.menu-detail');
donut.bannerUrl = node.querySelector('.hero > img').getAttribute('src');
donut.nutritionalFacts = node
.querySelector('[data-track="item-detail-nutrition"]')
.getAttribute('href');
donut.description = node.querySelector('p').innerText;
}
// });
console.log(donuts, types);
};
(async () => {
browser = await puppeteer.launch();
await runKrispyKreme();
browser.close();
})();
async function getPage(url) {
return await fetch(url)
.then(res => res.text())
.then(html => new JSDOM(html));
}
async function getPage2(url) {
let page = await browser.newPage();
await page.goto(url);
return page;
}