-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
163 lines (146 loc) · 4.68 KB
/
index.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
var fs = require("fs"),
clc = require('cli-color'),
jsonfile = require('jsonfile'),
util = require('util'),
waveform = require('waveform');
var EPISODE_DIR = 'episodes';
var colors = {
error: clc.red,
notice: clc.blue,
warn: clc.yellow,
info: clc.cyan,
success: clc.green
};
/*
* Reads a directory for files
*/
function readDirectory(dir, readFileCallback) {
process.stdout.write(clc.erase.screen);
console.log(colors.info("Reading Directory '" +
colors.notice(dir) +
"' for episodes"));
spacer();
// start reading the directory
fs.readdir(dir, function(err, files) {
console.log(colors.notice("\nFile Names Obtained : "));
console.log(colors.success(files));
spacer();
readFileCallback(dir, files);
});
}
function readAndProcessFiles(dir, files) {
console.log(colors.info("\n\nProcessing Episodes"));
spacer();
var numOfFilesReady = 0;
files.forEach(function(filename, index) {
var fileData = jsonfile.readFileSync(dir + '/' + filename);
if (validateJSONFile(dir, fileData, filename)) {
numOfFilesReady++;
}
});
if (numOfFilesReady == files.length) {
contentInit();
}
}
function validateJSONFile(dir, fileData, filename) {
var isValid = true;
console.log(colors.notice('Reading file for episode ' +
colors.success(fileData.episode) +
' with episode title : ' +
colors.success(fileData.title)));
if (!fileData.localmp3) {
console.log(colors.error("There is no mp3 associated with " +
fileData.episode +
" episode"));
}
if (fileData.datapoints) {
console.log(colors.notice("This episode has valid data"));
}
if (fileData.localmp3 && !fileData.datapoints) {
console.log(colors.warn("The episode does not have a waveform generated " +
"for its mp3 file.\nStarting waveform generation " +
"process in background.\nPlease be patient..."));
generateWaveform(dir, fileData, filename);
isValid = false;
}
spacer();
return isValid;
}
/*
* Generates Waveform for a given file
*/
function generateWaveform(dir, fileData, filename) {
waveform(dir + '/' + fileData.localmp3, {
// waveform.js options
waveformjs: '-', // path to output-file or - for stdout
'wjs-width': 800, // width in samples
'wjs-precision': 4, // how many digits of precision
'wjs-plain': false
}, function(err, output) {
if (err) {
console.log(colors.error(err));
}
if (output) {
console.log(colors.success("Waveform generated for " +
filename));
appendWaveformToEpisode(dir + '/' + filename, output, fileData);
}
});
}
/*
* Appends Waveform data to the episode file
*/
function appendWaveformToEpisode(filename, output, fileData) {
console.log(colors.notice("Adding waveform to episode file " +
filename));
fileData['datapoints'] = JSON.parse(output);
jsonfile.writeFileSync(filename, fileData, {
spaces: 2
});
console.log(colors.success(filename + ' now has all the data points for mp3.'));
spacer();
contentInit();
}
/*
* Initializes Content file creation process
*/
function contentInit() {
fs.readdir(EPISODE_DIR, function(err, files) {
var filesWithDataPoints = 0;
files.forEach(function(filename, index) {
var fileData = jsonfile.readFileSync(EPISODE_DIR + '/' + filename);
if (fileData.datapoints) {
filesWithDataPoints++;
}
});
if (filesWithDataPoints == files.length) {
console.log(colors.info("\n\nAll checks complete.\nStarting content creation process"));
spacer();
createContentFile();
}
});
}
function createContentFile() {
fs.readdir(EPISODE_DIR, function(err, files) {
var finalContent = [];
console.log(colors.notice("Merging File Data"));
files.forEach(function(filename, index) {
var fileData = jsonfile.readFileSync(EPISODE_DIR + '/' + filename);
finalContent.push(fileData);
});
console.log(colors.notice("Creating content file"));
jsonfile.writeFileSync('content.json', finalContent, {
spaces: 2
});
console.log(colors.success("Content file is complete\nPlease see " +
colors.notice("content.json") +
" file."));
});
}
/*
* Adds a spacer line
*/
function spacer() {
console.log(colors.warn("=================================================="));
}
readDirectory(EPISODE_DIR, readAndProcessFiles);