-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathgatsby-node.js
132 lines (116 loc) · 3.34 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
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
const path = require("path")
const {
writeFileSync,
readFileSync,
createWriteStream,
existsSync,
} = require("fs")
const { parse } = require("date-fns")
const glob = require("glob")
const axios = require("axios")
exports.onCreateNode = ({ node, actions }) => {
const { createNodeField } = actions
if (node.internal.type === `EventsJson`) {
const parsedDate = parse(node.date, "yyyy-MM-dd HH:mm", new Date())
createNodeField({
node,
name: "timestamp",
value: parsedDate.getTime(),
})
const parsedTickets = parse(
node.tickets_released,
"yyyy-MM-dd HH:mm",
new Date()
)
createNodeField({
node,
name: "ticket_timestamp",
value: parsedTickets.getTime(),
})
}
}
exports.onCreatePage = ({ page, actions }) => {
const { createPage, deletePage } = actions
deletePage(page)
createPage({
...page,
context: {
...page.context,
today: new Date().getTime(),
},
})
}
exports.onPreBootstrap = async () => {
const getPosters = id => [
`https://img.youtube.com/vi/${id}/maxresdefault.jpg`,
`https://img.youtube.com/vi/${id}/mqdefault.jpg`,
]
const imagePath = path.join(__dirname, "assets", "images", "posters")
const fetchPoster = (url, id) =>
new Promise(resolve =>
axios
.head(url)
.then(() =>
axios
.get(url, { responseType: "stream" })
.then(({ data }) =>
data.pipe(createWriteStream(path.join(imagePath, `${id}.jpg`)))
)
.then(() => {
resolve(url)
})
.catch(e => {
resolve(false)
})
)
.catch(() => {
resolve(false)
})
)
// Scan event json and attempt to fetch youtube_ids
const events = glob.sync(path.join("content", "events", "*.json"), {
absolute: true,
})
const eventData = events.map(path => readFileSync(path)).map(JSON.parse)
let foundPosters = 0
eventData.forEach((event, eventIndex) => {
event.speakers.forEach(async (speaker, speakerIndex) => {
if (!speaker.video_url || speaker.poster) {
return
}
const videoUrl = speaker.video_url
// Check if it's a youtube url
const variant = ["//www.youtube.com/watch?v=", "//youtu.be/"].find(
match => videoUrl.indexOf(match) !== -1
)
if (!variant) {
console.log(`${event.date} - ${speaker.name} - missing thumbnail`)
return
}
const id = videoUrl.replace(variant, "").split("&")[0]
if (id && !existsSync(path.join(imagePath, `${id}.jpg`))) {
const posters = getPosters(id)
let found = false
for (let i = 0; i < posters.length; i += 1) {
found = await fetchPoster(posters[i], id)
if (found) {
foundPosters += 1
// Add poster to json and write back
const newEventJson = { ...event }
newEventJson.speakers[speakerIndex].poster = path.join(
imagePath.replace(__dirname, path.join("..", "..")),
`${id}.jpg`
)
writeFileSync(
events[eventIndex],
JSON.stringify(newEventJson, null, 2),
"utf8"
)
break
}
}
}
})
})
console.log(`Added ${foundPosters} new Youtube poster/s.`)
}