forked from shayaulman/kosher-code
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gridsome.server.js
185 lines (166 loc) Β· 5.44 KB
/
gridsome.server.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
const axios = require("axios");
const videoTutorials = require("./data/videoTutorials");
const redditChannels = require("./data/redditChannels");
const podcasts = require("./data/podcasts");
const blogs = require("./data/blogs");
const learningPlatforms = require("./data/learningPlatforms");
let id_list = videoTutorials.map((tech) =>
tech.videoTutorials.map((video) => video.url)
);
const ID_LIST = [].concat.apply([], id_list);
const CHUNK_SIZE = 50;
const CHUNKS_OF_FIFTY = ID_LIST.map((e, i) => {
return i % CHUNK_SIZE === 0 ? ID_LIST.slice(i, i + CHUNK_SIZE) : null;
}).filter((e) => e);
const formatted = CHUNKS_OF_FIFTY.map((chunk) => chunk.join("%2C"));
module.exports = function(api) {
api.loadSource(async (actions) => {
const BASE_URL = `https://www.googleapis.com/youtube/v3/videos?part=snippet&key=${
process.env.GRIDSOME_API_KEY
}&type=video&id=`;
const data = await axios
.all(formatted.map((req) => axios.get(`${BASE_URL}${req}`)))
.then((res) => res.map((r) => r.data));
const VideoCollection = actions.addCollection("Video");
for (const d of data) {
for (const [index, item] of d.items.entries()) {
// API returns reversed
const technologyName = videoTutorials.find((tech) =>
tech.videoTutorials.find((video) => video.url === item.id)
);
VideoCollection.addNode({
id: item.id,
index,
name: technologyName.name.toLowerCase(),
category: technologyName.category,
title: item.snippet.title,
description: item.snippet.description,
thumbnail: item.snippet.thumbnails.medium.url,
publishedAt: item.snippet.publishedAt,
color: technologyName.color,
});
}
}
const RedditChannels = actions.addCollection("Reddit");
redditChannels.forEach((channel) => {
RedditChannels.addNode({
name: channel.name,
color: channel.color,
});
});
const PodcastsCollection = actions.addCollection("Podcast");
podcasts.forEach((podcast) => {
PodcastsCollection.addNode({
name: podcast.name,
description: podcast.description,
link: podcast.link,
color: podcast.color,
image: podcast.image,
});
});
const BlogsCollection = actions.addCollection("Blog");
blogs.forEach((blog) => {
BlogsCollection.addNode({
name: blog.name,
desc: blog.desc,
link: blog.link,
color: blog.color,
isHebrew: blog.hebrew,
isMedium: isMediumBlog(blog.link),
});
});
const LearningPlatforms = actions.addCollection("LearningPlatform");
learningPlatforms.forEach((platform) => {
LearningPlatforms.addNode({
name: platform.name,
desc: platform.desc,
link: platform.link,
tags: platform.tags,
isHebrew: platform.isHebrew || false,
color: platform.color,
});
});
const Categories = actions.addCollection("Category");
videoTutorials.forEach((category) => {
Categories.addNode({
name: category.name,
hebrewName: category.hebrewName,
category: category.category,
tags: category.tags,
thumbnail: `https://i.ytimg.com/vi/${
category.videoTutorials[0].url
}/mqdefault.jpg`,
officialSite: category.officialSite,
color: category.color,
amountOfVideos: category.videoTutorials.length,
});
});
});
api.createPages(async ({ graphql, createPage }) => {
const { data } = await graphql(`
{
allVideo {
edges {
node {
name
id
title
description
category
publishedAt
}
}
}
}
`);
data.allVideo.edges.forEach(({ node }) => {
if (node.category !== "playlist") {
createPage({
path: `/video-tutorials/${node.name}/${node.id}`,
component: "./src/templates/VideoPage.vue",
context: {
id: node.id,
index: node.index,
name: node.name,
title: node.title,
description: node.description,
category: node.category,
publishedAt: node.publishedAt,
},
});
}
});
videoTutorials.forEach((category) => {
if (category.category !== "playlist") {
createPage({
path: `/video-tutorials/${category.name.toLowerCase()}`,
component: "./src/templates/TechnologyPage.vue",
context: {
technology: category.name.toLowerCase(),
category: category.category,
hebrewName: category.hebrewName,
amountOfVideos: category.videoTutorials.length,
color: category.color,
},
});
} else if (category.category === "playlist") {
const PRETTY_PATH = category.name.replace(" ", "-").toLowerCase();
createPage({
path: `/video-tutorials/playlists/${PRETTY_PATH}`,
component: "./src/templates/PlaylistPage.vue",
context: {
name: category.name.toLowerCase(),
hebrewName: category.hebrewName,
amountOfVideos: category.videoTutorials.length,
allVideos: category.videoTutorials,
color: category.color,
},
});
}
});
});
};
// helpers
function isMediumBlog(urlString) {
return urlString.includes("://medium.com");
}