generated from queen-raae/gatsby-template-live-screencast
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgatsby-node.js
86 lines (76 loc) · 2.5 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
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
const axios = require("axios");
const scrapeCrowdcast = async () => {
try {
const { data } = await axios.get("https://app.scrapingbee.com/api/v1", {
params: {
api_key: process.env.SCRAPING_BEE_API_KEY,
url: "https://www.crowdcast.io/raae",
// Wait for there to be at least one
// non-empty .event-tile element
wait_for: ".event-tile",
extract_rules: {
webinars: {
// Lets create a list with data
// extracted from the .event-tile element
selector: ".event-tile",
type: "list",
// Each object in the list should
output: {
// have a title lifted from
// the .event-tile__title element
title: ".event-tile__title",
// and a path lifted from
// the href attribute of the first link element
path: {
selector: "a",
output: "@href",
},
style: {
selector: ".hero",
output: "@style",
},
},
},
},
},
});
return data;
} catch (error) {
throw new Error("ScrapingBee Error: " + error.message, { cause: error });
}
};
exports.sourceNodes = async (gatsbyUtils) => {
const { actions, createNodeId, createContentDigest, reporter } = gatsbyUtils;
const { createNode } = actions;
try {
reporter.info("SOURCE CROWDCAST >> Begin");
const data = await scrapeCrowdcast();
for (const webinar of data.webinars) {
reporter.info("Create CrowdcastWebinar for " + webinar.title);
// Remove the search params
let url = new URL("https://www.crowdcast.io" + webinar.path);
url = url.origin + url.pathname;
// Exctract the cover image src
const regex = new RegExp(/url\(\"(.*?)\"\)/g);
const result = regex.exec(webinar.style);
// Remove the cover image search params
let coverSrc = new URL(result[1]);
coverSrc = coverSrc.origin + coverSrc.pathname;
createNode({
id: createNodeId(url),
title: webinar.title,
url: url,
coverSrc: coverSrc,
rawScrape: webinar,
internal: {
type: `CrowdcastWebinar`,
mediaType: `text/json`,
content: JSON.stringify(webinar),
contentDigest: createContentDigest(webinar),
},
});
}
} catch (error) {
reporter.warn("SOURCE CROWDCAST >>> Failed >>> " + error.message);
}
};