forked from hackclub/hackathons-v1
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gatsby-node.js
187 lines (173 loc) · 5.46 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
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
186
187
const { writeFile, existsSync, mkdirSync } = require('fs')
const axios = require('axios')
const path = require('path')
const readline = require('readline')
const regions = require('./src/regions.js')
const imageFolder = 'static/images/'
const downloadImage = (event, image, type = 'event') =>
new Promise((resolve, reject) => {
if (!image) resolve(null)
axios
.get(`https://api.hackclub.com${image.file_path}`, {
responseType: 'arraybuffer',
})
.then(res => {
let extension = ''
switch (res.headers['content-type']) {
case 'image/jpeg':
extension = '.jpg'
break
case 'image/png':
extension = '.png'
break
default:
throw `Invalid content-type: ${res.headers['content-type']}`
}
let updatedAt = Date.parse(image.updated_at)
const fileName = `${type}_${image.type}_${event.id}.${updatedAt}${extension}`
writeFile(imageFolder + fileName, res.data, 'binary', err => {
if (err) throw err
resolve(`images/${fileName}`)
})
})
.catch(err => reject(err))
})
const processEvent = async event => ({
...event,
id: event.id.toString(),
banner: await downloadImage(event, event.banner),
logo: await downloadImage(event, event.logo)
})
const processGroup = async group => ({
...group,
id: group.id.toString(),
banner: await downloadImage(group, group.banner, 'group'),
logo: await downloadImage(group, group.logo, 'group')
})
exports.onPreBootstrap = () => {
let startTime = Date.now()
const logMessage = (msg) => {
readline.clearLine(process.stdout)
readline.cursorTo(process.stdout, 0)
const elapsedTime = ((Date.now() - startTime).toFixed(2) / 1000)
console.log(` ${msg} – ${elapsedTime} s`)
startTime = Date.now()
}
// Download & process events
return axios
.get('https://api.hackclub.com/v1/events')
.then(eventsRes => {
logMessage('Fetched events data')
return axios.get('https://api.hackclub.com/v1/events/groups').then(groupsRes => {
logMessage('Fetched groups data')
if (!existsSync(imageFolder)) {
mkdirSync(imageFolder)
logMessage('Created image folder')
}
const groupsPromiseArray = groupsRes.data.map(group => processGroup(group))
return new Promise.all(groupsPromiseArray).then(groupsData => {
const eventsPromiseArray = eventsRes.data.map(event => processEvent(event))
return new Promise.all(eventsPromiseArray)
.then(eventsData => {
logMessage('Mapped through event data')
const writeGroups = new Promise((resolve, reject) => {
writeFile('data/groups.json', JSON.stringify(groupsData), err => {
if (err) return reject(err)
logMessage('Group data written to file')
resolve()
})
})
const writeEvents = new Promise((resolve, reject) => {
writeFile('data/events.json', JSON.stringify(eventsData), err => {
if (err) return reject(err)
logMessage('Event data written to file')
resolve()
})
})
return new Promise.all([writeEvents, writeGroups])
})
})
})
})
// Download & process event stats
.then(() => (
axios.get('https://api.hackclub.com/v1/event_email_subscribers/stats')
))
.then(res => (
new Promise((resolve, reject) => {
writeFile('data/stats.json', JSON.stringify(res.data), err => {
if (err) return reject(err)
logMessage('Event stats written to file')
resolve()
})
})
))
}
exports.createPages = ({ graphql, actions }) => {
const { createPage } = actions
return new Promise((resolve, reject) => {
const component = path.resolve('src/templates/region.js')
resolve(
graphql(`
{
allEventsJson {
edges {
node {
id
startHumanized: start(formatString: "MMMM D")
endHumanized: end(formatString: "D")
start
end
startYear: start(formatString: "YYYY")
parsed_city
parsed_state
parsed_state_code
parsed_country
parsed_country_code
name
website: website_redirect
latitude
longitude
banner
logo
mlh: mlh_associated
}
}
}
dataJson {
cities
countries
}
}
`).then(result => {
if (result.errors) {
console.error(result.errors)
reject(result.errors)
}
regions.map(region => {
const events = result.data.allEventsJson.edges.filter(edge =>
region.filter(edge.node)
)
const emailStats = result.data.dataJson
if (events.length > 3) {
createPage({
path: region.path,
component,
context: {
region,
events,
emailStats
},
})
}
})
})
)
locations.map(location => {
createPage({
path: location,
component,
})
})
})
}