-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgatsby-node.js
51 lines (48 loc) · 1.61 KB
/
gatsby-node.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
const jsdom = require("jsdom")
const { JSDOM } = jsdom
exports.sourceNodes = async (gatsby, options) => {
const { actions, createNodeId, createContentDigest } = gatsby
const { createNode } = actions
try {
// const url = `https://www.tusclasesparticulares.cl/profesores/${options.username}/opiniones`
const url = `https://www.tusclasesparticulares.cl/partialview/reviewslist.aspx?p=${options.username}`
const selector = ".review-item"
const { window } = await JSDOM.fromURL(url, {
runScripts: "dangerously",
resources: "usable",
})
const nodes = await new Promise(resolve => {
window.addEventListener("load", () => {
const nodeList = [...window.document.querySelectorAll(selector)]
resolve(nodeList)
})
})
const opiniones = nodes.map(item => {
return {
name: item.querySelector(".review-name").textContent,
image: item.querySelector(".review-header")?.querySelector("img")?.src,
score: [...item.querySelector(".review-score").children].reduce(
(acc, star) => (star.classList.contains("star_y") ? (acc += 1) : acc),
0
),
text: item.querySelector(".reviewtxt").textContent,
}
})
opiniones.map((metadata, idx) => {
const nodeData = {
...metadata,
id: createNodeId(`opinion-${idx}`),
parent: null,
internal: {
type: `Opinion`,
content: JSON.stringify(metadata),
contentDigest: createContentDigest(metadata),
},
}
createNode(nodeData) // That's it
})
return
} catch (e) {
console.error(e)
}
}