-
Notifications
You must be signed in to change notification settings - Fork 2
/
upload_to_es.js
78 lines (69 loc) · 1.91 KB
/
upload_to_es.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
var elasticsearch = require("elasticsearch");
var fs = require("fs");
global.Promise = require("bluebird");
let files = 0;
var client = new elasticsearch.Client({
host: process.env.ES_HOST,
});
function deleteClusterIndices() {
client.indices.delete({
index: "_all"
}, function(err, response) {
console.log(`Response from delete: ${response}`);
});
}
function testConnectionToClient() {
client.ping({
requestTimeout: 100000,
}, function (error) {
if (error) {
console.error("Elasticsearch cluster is down!");
} else {
console.log("Elasticsearch cluster is connected.");
}
});
}
function readFile(filename) {
return new Promise((resolve, reject) => {
fs.readFile("output/" + filename, "utf8", function(err, data) {
if (err) throw err;
var videoCaptionData = JSON.parse(data);
let caption_parts = videoCaptionData.map((captionData) => {
return new Promise((resolve, reject) => {
sendVideoDataToClient(captionData, resolve);
});
});
Promise.all(caption_parts)
.then(() => {
console.log("About to resolve upper promise");
resolve();
});
});
});
}
let writeStream = fs.createWriteStream("upload_log.txt");
function sendVideoDataToClient(data, resolve) {
if (!data) return resolve();
client.index({
index: "youtube-video-data-index",
type: "caption-data",
id: data.video_id,
body: JSON.stringify(data)
}, function (error, response) {
error ? writeStream.write(JSON.stringify(error)) : console.log(`Finished uploading ${++files}`);
resolve();
});
}
testConnectionToClient();
deleteClusterIndices();
fs.readdir(__dirname + "/output", (err, files) => {
(function loopRead(fileIndex = 0) {
if(fileIndex >= files.length) return console.log(files.length);
readFile(files[fileIndex])
.then(() => {
setTimeout(function() {
loopRead(++fileIndex);
}, 200);
});
})();
});