forked from revtel/reactconf.tv
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gatsby-node.js
96 lines (86 loc) · 2.48 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
87
88
89
90
91
92
93
94
95
96
const fs = require('fs');
const path = require('path');
const util = require('util');
const readFile = util.promisify(fs.readFile);
const {matchConferenceByTitle} = require('./src/utils/matchConferenceByTitle');
const {transformAllChannelsData} = require('./src/utils/transformData');
const {
getMostViewed,
getNewReleased,
getRecentViewed,
} = require('./src/utils/getVideoByStat');
exports.createPages = async ({graphql, actions}) => {
const {createPage} = actions;
const ytChannelsNode = (
await graphql(
`
query MyQuery {
allFile(filter: {name: {eq: "ytChannels"}}) {
edges {
node {
name
dir
relativePath
}
}
}
}
`,
)
).data.allFile.edges[0].node;
const ytChannels = JSON.parse(
(
await readFile(path.join(ytChannelsNode.dir, ytChannelsNode.relativePath))
).toString(),
);
const confChannelMap = {};
for (const ytChannel of ytChannels) {
const ytChannelPlaylist = JSON.parse(
await readFile(path.join('data/playlist', ytChannel.name + '.json')),
);
// every ytChannelPlaylist is a conference at a particular time, ex: "React Europe 2019",
// try to find out which "conference channel" (ex: "React Europ") it belongs to
for (const confEvent of ytChannelPlaylist.items) {
const matched = matchConferenceByTitle({
title: confEvent.snippet.title,
conferences: ytChannel.conferences,
});
if (matched) {
if (!confChannelMap[matched.name]) {
confChannelMap[matched.name] = {
name: matched.name,
display: matched.display,
items: [],
};
}
confChannelMap[matched.name].items.push(confEvent);
}
}
}
const baseContext = {
ytChannels,
channels: transformAllChannelsData(
Object.keys(confChannelMap).map((key) => confChannelMap[key]),
),
};
createPage({
path: `/`,
component: path.resolve(`src/templates/LandingPage.js`),
context: {
...baseContext,
classicVideos: getMostViewed(10),
trendingNowVideos: getRecentViewed(10),
newReleasedVideos: getNewReleased(10),
},
});
createPage({
path: `/player`,
component: path.resolve(`src/templates/PlayerPage.js`),
context: baseContext,
});
createPage({
path: `/favorites`,
component: path.resolve(`src/templates/FavoritePage.js`),
context: baseContext,
});
};