-
Notifications
You must be signed in to change notification settings - Fork 20
/
prepare-json.js
156 lines (118 loc) · 4.28 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
const dotenv = require('dotenv').config()
const colors = require("colors")
const fs = require('fs')
const { Events, Videos, Source } = require('metagroup-schema-tools')
const moment = require('moment')
const SOURCE_JSON = process.env.VIGOTECH_MEMBERS_SOURCE_FILE
const GENERATED_JSON = 'static/vigotech-generated.json'
const JSON_SCHEMA = 'static/vigotech-schema.json'
function eventDate(date) {
return moment(date).format('dddd, D MMMM YYYY HH:mm')
}
function getNextEvents(data) {
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 {
if (nextEvents.length === undefined) {
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 root group next events
const rootNextEvents = Events.getGroupNextEvents(data.events, {
eventbriteToken: process.env.EVENTBRITE_OAUTH_TOKEN,
member: data
})
data.nextEvent = rootNextEvents[0]
// Get members next events
for(let memberKey in data.members) {
const member = data.members[memberKey]
const membersNextEvents = Events.getGroupNextEvents(member.events, {
eventbriteToken: process.env.EVENTBRITE_OAUTH_TOKEN,
member: member
})
data.members[memberKey].nextEvent = membersNextEvents[0]
}
return data
}
async function getMembersVideos(membersData) {
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();
})
// Get members videos
for(let memberKey in data.members) {
const member = data.members[memberKey]
data.members[memberKey].videoList = await Videos.getGroupVideos(member.videos, 6, {
youtubeApiKey: process.env.YOUTUBE_API_KEY,
member: member
})
}
return data
}
function saveJsonFile(data) {
fs.writeFileSync(GENERATED_JSON, JSON.stringify(data));
console.log(` ${colors.inverse(`Saving ${colors.yellow(`${GENERATED_JSON}`)}`)}`);
}
// Read and parse source data
console.log(`${colors.inverse("Getting vigotech.json file")}`);
console.log(` Getting members json from ${colors.underline(SOURCE_JSON)}`)
let data = {}
try {
const dataRaw = fs.readFileSync(SOURCE_JSON, '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 = fs.readFileSync(JSON_SCHEMA, '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')}`);
// Import events
console.log(`${colors.inverse("Preparing json files")}`);
console.log(`${colors.bold(" Import next events")}`);
data = getNextEvents(data)
console.log();
console.log();
//Import videos
console.log(`${colors.bold(" Import videos")}`);
getMembersVideos(data)
.then(data => {
saveJsonFile(data)
})