-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfetchData.js
More file actions
148 lines (118 loc) · 4 KB
/
fetchData.js
File metadata and controls
148 lines (118 loc) · 4 KB
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
const dotenv = require('dotenv');
const fetch = require('node-fetch');
const Octokit = require('@octokit/rest')
// pull in local .env variables
dotenv.config();
// create github client
const octokit = new Octokit({
auth: process.env.GITHUB_TOKEN
})
// default params
const PARAMS = {
owner: "vtcodecamp",
repo: "2018.vtcodecamp.org",
branch: "feature/fetch_sync",
committer: {
name: "sessionize-bot",
email: "admin@vtcodecamp.org"
}
};
let UPDATE_COUNT = 0
// run on functions, attach handler
exports.handler = fetchData;
async function fetchData(event, context) {
const response = await fetch('https://sessionize.com/api/v2/bm8zoh0m/view/all');
const sessionize = await response.json();
const [levels, formats] = parseCategories(sessionize.categories);
const speakers = buildSpeakers(sessionize.speakers);
const sessions = buildSessions(sessionize.sessions, levels, formats);
await writeDataFile('sessions.json', sessions);
await writeDataFile('speakers.json', speakers);
await writeDataFile('rooms.json', sessionize.rooms);
return {
statusCode: 200,
body: `Fetched ${sessions.length} session(s) and ${speakers.length} speakers(s) from Sessionize. \r\n` +
`Compared to existing files in github and updated ${UPDATE_COUNT} files`
}
}
function parseCategories(categories) {
var levels = {};
var formats = {};
for (let category of categories) {
if (category.title == 'Level') {
for (level of category.items) {
levels[level.id] = level;
}
} else if (category.title == 'Session format') {
for (format of category.items) {
formats[format.id] = format;
}
}
}
return [levels, formats]
}
function buildSpeakers(speakersData) {
for (let speaker of speakersData) {
for (let link of speaker.links) {
link.name = link.title;
switch (link.linkType) {
case 'Twitter':
link.name = '@' + link.url.replace(/https*:\/\/(www\.)*twitter.com\//gi, '')
.replace(/\/?(\?.*)?$/, '');
break;
case 'Blog':
case 'Company_Website':
link.name = link.url.replace(/https*:\/\/(www\.)*/gi, '')
.replace(/\/?(\?.*)?$/, '')
.replace(/\/.*/, '');
break;
}
}
}
return speakersData
}
function buildSessions(sessionsData, levels, formats) {
for (let session of sessionsData) {
for (let categoryId of session.categoryItems) {
if (categoryId in levels) {
session.level = levels[categoryId].name;
} else if (categoryId in formats) {
session.format = formats[categoryId].name;
}
}
}
return sessionsData;
}
async function writeDataFile(filename, array) {
let data = flattenArrayToObj(array)
let filePath = `src/_data/${filename}`;
let content = JSON.stringify(data, null, 4);
let base64 = Buffer.from(content).toString('base64')
const readFileParams = {
...PARAMS,
path: filePath,
}
// READ file
const file = await octokit.repos.getContents(readFileParams)
// git file adds line breaks - remove and compare
let dataHasChanged = file.data.content.replace(/(\r\n|\n|\r)/gm,"") != base64
if (dataHasChanged) {
const writeFileParams = {
...PARAMS,
path: filePath,
sha: file.data.sha || "",
message: "Update sessionize data.",
content: base64
}
// WRITE file
await octokit.repos.createOrUpdateFile(writeFileParams)
UPDATE_COUNT++;
}
}
function flattenArrayToObj(array) {
let object = {};
for (let item of array) {
object[item.id] = item;
}
return object;
}