-
Notifications
You must be signed in to change notification settings - Fork 3
/
prepare-json.js
226 lines (178 loc) · 6.57 KB
/
prepare-json.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
const dotenv = require('dotenv').config()
const colors = require("colors")
const fs = require('fs')
const request = require('sync-request')
const { Events, Videos, Source } = require('metagroup-schema-tools')
const moment = require('moment')
const yaml = require('json2yaml')
const lowdb = require('lowdb')
const fileSync = require('lowdb/adapters/FileSync')
const SOURCE_JSON = 'https://vigotech.org/vigotech.json'
const GENERATED_JSON = process.env.GENERATED_JSON_PATH
const JSON_SCHEMA = 'https://vigotech.org/vigotech-schema.json'
const MEMBER_KEY = process.env.MEMBER
const VIDEOS_YML = 'source/_data/videos.yml'
const PREVIOUS_EVENTS_JSON = 'source/events.json'
const PREVIOUS_EVENTS_YML = 'source/_data/events.yml'
function eventDate(date) {
return moment(date).format('dddd, D MMMM YYYY HH:mm')
}
function getNextEvents(member) {
const eventEmitter = Events.getEventsEmitter()
eventEmitter.on('getNextFromSourceInit', (source, options) => {
console.log(` · Getting upcoming events json for ${colors.green(options.member.name)} from ${colors.underline(source.type)}`);
})
eventEmitter.on('getNextFromSourceCompleted', (nextEvents, options) => {
if (nextEvents.length === 0) {
console.log(` ${colors.yellow(`No upcoming events found`)}`)
} else {
nextEvents = Array.isArray(nextEvents) ? nextEvents : [nextEvents]
const nextEvent = nextEvents[0]
console.log(` ${colors.cyan(`Upcoming event found:`)} ${colors.blue(`${colors.bold(`${nextEvent.title}`)} ${eventDate(nextEvent.date)}`)}`)
}
console.log();
})
// Get members next events
const membersNextEvents = Events.getGroupNextEvents(member.events, {
eventbriteToken: process.env.EVENTBRITE_OAUTH_TOKEN,
member: member
})
member.nextEvent = membersNextEvents[0]
member.nextEvents = membersNextEvents
return member;
}
function getPreviousEvents(member) {
const eventEmitter = Events.getEventsEmitter()
eventEmitter.on('getPrevFromSourceInit', (source, options) => {
console.log(` · Getting previous events json for ${colors.green(options.member.name)} from ${colors.underline(source.type)}`);
})
eventEmitter.on('getPrevFromSourceCompleted', (previousEvents, options) => {
if (previousEvents.length === 0) {
console.log(` ${colors.yellow(`No previous events found`)}`)
} else {
previousEvents = Array.isArray(previousEvents) ? previousEvents : [previousEvents]
previousEvents.forEach(previousEvent => {
console.log(` ${colors.cyan(`Previous event found:`)} ${colors.blue(`${colors.bold(`${previousEvent.title}`)} ${eventDate(previousEvent.date)}`)}`)
})
}
console.log();
})
// Get members previous events
return membersPrevEvents = Events.getGroupPrevEvents(member.events, {
eventbriteToken: process.env.EVENTBRITE_OAUTH_TOKEN,
member: member
})
}
async function getMembersVideos(member) {
const eventEmitter = Videos.getEventsEmitter()
eventEmitter.on('getVideosFromSourceInit', (source, options) => {
console.log(` · Getting member videos for ${colors.green(options.member.name)} from ${colors.underline(source.type)}`);
})
eventEmitter.on('getVideosFromSourceCompleted', (videos, options) => {
if (videos.length === 0) {
console.log(` ${colors.yellow(`No videos found`)}`)
}
else {
console.log(` ${colors.cyan(`Imported ${videos.length} videos`)}`)
}
console.log();
})
console.log('Get members videos')
// Get members videos
try {
member.videoList = await Videos.getGroupVideos(member.videos, 50, {
youtubeApiKey: process.env.YOUTUBE_API_KEY,
member: member
})
} catch(e) {
console.error(e.errors);
process.exit(1);
};
return member
}
function saveJsonFile(data) {
fs.writeFileSync(GENERATED_JSON, JSON.stringify(data));
console.log(` ${colors.inverse(`Saving ${colors.yellow(`${GENERATED_JSON}`)}`)}`);
}
function saveVideosYmlFile(videos) {
fs.writeFileSync(VIDEOS_YML, yaml.stringify(videos));
console.log(` ${colors.inverse(`Saving ${colors.yellow(`${VIDEOS_YML}`)}`)}`);
}
function savePrevEventsToYmlFile(events) {
fs.writeFileSync(PREVIOUS_EVENTS_YML, yaml.stringify(events));
console.log(` ${colors.inverse(`Saving ${colors.yellow(`${PREVIOUS_EVENTS_YML}`)}`)}`);
}
// Read and parse source data
console.log(process.env)
if (!MEMBER_KEY) {
console.log(`${colors.bgRed("Error: MEMBER env variable not defined")}`);
process.exit(1)
}
console.log(`${colors.inverse("Getting vigotech.json file")}`);
console.log(` Getting members json from ${colors.underline(SOURCE_JSON)}`)
let data = {}
try {
const dataRaw = request('GET', SOURCE_JSON).getBody("utf8")
data = JSON.parse(dataRaw)
} catch(e) {
console.log(`${colors.red.inverse(e)}`);
process.exit(1)
}
// Validate data schema
console.log(`${colors.inverse("Validate vigotech.json file")}`);
console.log(` Getting json schema from ${colors.underline(JSON_SCHEMA)}`);
let schema = {}
try {
const schemaRaw = request('GET', JSON_SCHEMA).getBody("utf8")
schema = JSON.parse(schemaRaw)
} catch(e) {
console.log(`${colors.red.inverse(e)}`);
process.exit(1)
}
const validationResult = Source.validate(data, schema)
if (validationResult.errors.length > 0) {
console.log(`${colors.red.inverse('Error validating source data')}`);
console.log(typeof validationResult.errors)
validationResult.errors.forEach( (error) => {
console.log(` ${colors.red(error.property + ' ' + error.message)}`)
console.log(error.instance)
})
process.exit(1);
}
console.log(` ${colors.green.inverse('OK')}`);
const memberData = getNextEvents(data.members[MEMBER_KEY])
// Import previous events
const prevEvents = getPreviousEvents(memberData)
const adapter = new fileSync(PREVIOUS_EVENTS_JSON)
const db = lowdb(adapter)
db.defaults({ events: []}).value()
// Add new avents to db
prevEvents.forEach(event => {
if (!db.get('events').find({ date: event.date }).value()) {
db
.get('events')
.push(event)
.write();
} else {
db
.get('events')
.find({ date: event.date })
.assign(event)
.write();
}
})
// Convert to json
savePrevEventsToYmlFile(db.get('events').orderBy('date', 'desc').value())
// Import next events
console.log(`${colors.inverse("Preparing json files")}`);
console.log(`${colors.bold(" Import next events")}`);
console.log();
console.log();
//Import videos
console.log(`${colors.bold(" Import videos")}`);
getMembersVideos(memberData)
.then(memberData => {
saveJsonFile(memberData)
//Save videos to _data/videos.yml
saveVideosYmlFile(memberData.videoList)
})