-
Notifications
You must be signed in to change notification settings - Fork 50
/
generate.js
235 lines (209 loc) · 7.06 KB
/
generate.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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
/* eslint-disable camelcase */
const fs = require('fs')
const got = require('got')
const spreadsheetId = process.env.SPREADSHEET_ID || '1x_W7Z2o_TGmEjL5cLTFbjO1R3KzQOqIhQKu9RQ4a_P4'
const apiKey = process.env.API_KEY || 'AIzaSyA5el9Fo8rMSYkcMjUqLfJi4tDB5_n0bzY'
const slugify = require('slugify')
const githubApiKey = process.env.GH_API_KEY
const getContributors = require('./getContributors')
if (!githubApiKey) {
console.log('Please provide active github api key from https://github.com/settings/tokens')
process.exit(1)
}
const slugger = (text) =>
slugify(text, {
replacement: '-',
lower: true,
locale: 'tr',
})
const mapper = (posts) => {
const keys = posts.slice(0, 1)[0]
return posts.slice(1).map((row) => {
const rowData = {}
keys.map((key, index) => {
key = key.toLowerCase().replace(/\W/g, '_')
rowData[key] = row[index]
})
return rowData
})
}
const generateAvatar = ({ name, github }) => {
let avatar
if (github) {
const regex = /github.com\/([\w\d-]+)(. +)?/
const response = github.match(regex)
if (!response) {
return ''
}
avatar = `https://avatars.githubusercontent.com/${response[1]}`
} else {
avatar = `https://ui-avatars.com/api/?name=${name}&size=256`
}
return avatar
}
const fixProtocol = (url) => {
return url ? 'https://' + url.replace(/https?:\/\/|(www.)/gi, '').replace(/\/$/gi, '') : ''
}
const clearTwitterAdr = (url) => {
return url.replace(/@/g, '').trim()
}
const clearData = (posts) => {
return posts.map((post) => {
post.slug = slugger(post.name)
post.github = fixProtocol(post.github)
post.twitter_handle = fixProtocol(post.twitter_handle)
post.twitter_handle = clearTwitterAdr(post.twitter_handle)
post.linkedin = fixProtocol(post.linkedin)
post.avatar = generateAvatar(post)
post.displayInterests = post.interests
post.interests = post.interests.toLowerCase()
return post
})
}
const clearMentorships = (posts) => {
return posts.map((post) => {
post.mentor = post.mentor.trim()
post.project_adress = fixProtocol(post.project_adress.trim())
const projectName = post.project_adress.split('/').pop()
post.slug = projectName
return post
})
}
const getData = async () => {
try {
// request datas
const attendies_url = `https://sheets.googleapis.com/v4/spreadsheets/${spreadsheetId}/values:batchGet?key=${apiKey}&fields=valueRanges(range,values)&ranges=Mentees&ranges=Aktif%20Mentorluklar&ranges=Jobs&ranges=Interns`
const contribs_url = 'https://api.github.com/repos/findmentor-network/find-mentor/contributors'
const [attendies, contribs] = await Promise.all([
got(attendies_url).then((res) => JSON.parse(res.body)),
got(contribs_url, {
headers: {
Authorization: `token ${githubApiKey}`,
},
}).then((res) => JSON.parse(res.body)),
])
// clear data
let [people, activeMentorships, jobs, hireable] = attendies.valueRanges
jobs = mapper(jobs.values.slice(1).filter((r) => r.length))
jobs = jobs.map((job, id) => {
job.isRemoveAllowed = job.remote === 'Evet'
job.tags = job.tags.split(',').map((p) => p.trim())
return job
})
hireable = mapper(hireable.values.slice(1).filter((r) => r.length))
people = clearData(mapper(people.values.slice(4).filter((r) => r.length)))
activeMentorships = clearMentorships(mapper(activeMentorships.values.slice(1).filter((r) => r.length)))
activeMentorships = await getContributors(activeMentorships, people)
let [menteeCount, mentorCount, both, total] = [0, 0, 0, 0]
// 500 x 4 = 2000 kere array icinde arama
// find and place mentorships
people.map((person) => {
if (person.mentor === 'Mentee') {
menteeCount++
} else if (person.mentor === 'İkisi de' || person.mentor === 'Both') {
both++
mentorCount++
menteeCount++
} else if (person.mentor === 'Mentor') {
mentorCount++
}
total++
const isHireable = hireable.find((p) => p.profile.includes(person.slug))
person.isHireable = isHireable ? true : false
if (isHireable) {
person.mail = isHireable.mail
isHireable.person = person
}
person.mentorships = activeMentorships.filter((mentorship) => {
if (mentorship.mentor.endsWith(person.slug)) {
return mentorship
}
})
person.contributions = activeMentorships.filter((mentorship) => {
const gh = person.github
if (gh) {
const amIExists = mentorship.contributors.find((contributor) => {
if (contributor.github_address === gh) {
return contributor
}
})
if (amIExists) {
return mentorship
}
}
})
})
// remove the bot
contribs.splice(0, 1)
// check if contributor has find-mentor profile
contribs.forEach((contrib) => {
for (const person of people) {
if (person.github === contrib.html_url) {
contrib.fmn_url = `https://findmentor.network/peer/${person.slug}`
break
}
}
})
const data = {
persons: people,
activeMentorships,
contribs,
jobs,
hireable,
counts: { menteeCount, mentorCount, both, total },
}
return { status: 200, data }
} catch (err) {
// eslint-disable-next-line no-console
console.log(err)
return { status: 404 }
}
}
// writes collected data to disk
async function makeContent(name, data) {
const serialized_data = JSON.stringify(data, null, 2)
fs.writeFileSync(`content/${name}.json`, serialized_data)
fs.writeFileSync(`static/${name}.json`, serialized_data)
}
function shuffle(array) {
var currentIndex = array.length,
temporaryValue,
randomIndex
// While there remain elements to shuffle...
while (0 !== currentIndex) {
// Pick a remaining element...
randomIndex = Math.floor(Math.random() * currentIndex)
currentIndex -= 1
// And swap it with the current element.
temporaryValue = array[currentIndex]
array[currentIndex] = array[randomIndex]
array[randomIndex] = temporaryValue
}
return array
}
// entry point
getData().then(({ status, data: { persons, activeMentorships, contribs, counts, jobs, hireable } }) => {
if (status !== 200) {
throw new Error('Error when fetching data from spreadsheet')
}
const mentorships = activeMentorships
makeContent('persons', shuffle(persons))
makeContent('activeMentorships', { mentorships })
makeContent('contribs', { contribs })
makeContent('jobs', { jobs })
makeContent('hireable', { hireable })
makeContent('info', counts)
})
// entry point
getData().then(({ status, data: { persons, activeMentorships, contribs, counts, jobs, hireable } }) => {
if (status !== 200) {
throw new Error('Error when fetching data from spreadsheet')
}
const mentorships = activeMentorships
makeContent('persons', shuffle(persons))
makeContent('activeMentorships', { mentorships })
makeContent('contribs', { contribs })
makeContent('jobs', { jobs })
makeContent('hireable', { hireable })
makeContent('info', counts)
})