-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
280 lines (244 loc) · 8.21 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
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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
var http = require('http');
var accesslog = require('access-log');
var genericPool = require('generic-pool');
var nconf = require('nconf');
var noise = require('noise-search');
var async = require('async');
var fs = require("fs");
nconf.use('file', { file: './config.json' });
nconf.load();
if (typeof nconf.get("last_indexed") == "undefined") {
nconf.set("last_indexed", 0);
nconf.save(function (err) {
if (err) {
console.error(err.message);
return;
}
});
}
// open the index, create if missing
let index = noise.open('show_index', true);
// create a queue of http fetches.
var httpQ = async.queue((task, callback) => {
task(callback);
}, 1);
function massageShow(s) {
s._id = s.id.toString();
delete s.weight;
if (s.network) {
delete s.network.id;
}
delete s.externals;
delete s.image;
delete s.updated;
delete s._links;
s.episodes = s._embedded.episodes;
s.cast = s._embedded.cast;
delete s._embedded;
s.episodes = s.episodes.map(e => {
delete e.id;
delete e.image;
delete e.airstamp
delete e._links;
return e;
});
s.cast.map(c => {
delete c.character.id;
delete c.character.image;
delete c.character._links;
delete c.person.id;
delete c.person.image;
delete c.person._links;
return c;
});
}
function indexShow(showListing) {
httpQ.push(callback => {
var options = {
host: 'api.tvmaze.com',
path: '/shows/' + showListing.id.toString() + "?embed[]=episodes&embed[]=cast"
};
http.get(options, response => {
var str = '';
//another chunk of data has been recieved, so append it to `str`
response.on('data', function (chunk) {
str += chunk;
});
response.on('end', function () {
let show = JSON.parse(str);
if (!show.id) {
callback();
return;
}
massageShow(show);
index.add(show).then(id => {
console.log("Indexed " + show.name +' (' + id + ')');
nconf.set('last_indexed', id);
nconf.save();
}).catch(e => {
console.log(e);
});
callback();
});
}).on('error', e => {
// unsure what to do
console.log(e);
});
});
}
function getLatestShows(pageNum) {
let getLatest = callback => {
var options = {
host: 'api.tvmaze.com',
path: '/shows?page=' + pageNum
};
http.get(options, response => {
var str = '';
//another chunk of data has been recieved, so append it to `str`
response.on('data', function (chunk) {
str += chunk;
});
response.on('error', function (e) {
console.log(e);
});
response.on('end', function () {
let shows = JSON.parse(str);
shows.forEach(showListing => {
if (nconf.get("last_indexed") < showListing.id) {
indexShow(showListing);
}
});
if (shows.length != 0) {
getLatestShows(pageNum + 1);
} else {
// try again for latest in 12 hrs
setInterval(getLatestShows, 1000*60*60*12, Math.floor(nconf.get("last_indexed") / 250));
}
callback();
});
});
};
httpQ.push(getLatest);
}
// get the shows to index
// the show listings limits 250 shows per page. So we divide by 250
// to get the necessary page to for the next shows
getLatestShows(Math.floor(nconf.get("last_indexed") / 250));
// start the webserver
const hostname = '127.0.0.1';
const port = 3000;
const maxPostSize = 4 * 1024;
const accessLogFormat = ':Xip - :userID [:endDate] ":method :url :protocol/:httpVersion" :statusCode :contentLength ":referer" ":userAgent"';
const escapeNewlineRegexp = /\n|\r\n|\r/g;
const headers = {
'Content-Type': "application/json",
'Access-Control-Allow-Origin': "*",
'Access-Control-Allow-Methods': "POST"
};
// Number of open indexes for querying
const numQueryIndexes = process.env.NOISE_QUERY_INDEXES || 4;
const maxWaitingClients = process.env.NOISE_MAX_WAITING_CLIENTS || 10;
const acquireTimeoutMillis = process.env.NOISE_ACQUIRE_TIMEOUT || 5000;
// Setup a pool for indexes for some parallelism
const factory = {
create: function() {
return new Promise((resolve, reject) => {
let index = noise.open('show_index', false);
resolve(index);
});
},
destroy: function(index) {
return new Promise(resolve => {
index.close();
resolve();
});
}
}
const opts = {
max: numQueryIndexes,
min: numQueryIndexes,
maxWaitingClients: maxWaitingClients,
acquireTimeoutMillis: acquireTimeoutMillis,
fifo: false,
}
const noisePool = genericPool.createPool(factory, opts)
const sendErrorResponse = (res, statusCode, error) => {
res.writeHead(statusCode, headers);
res.end(JSON.stringify({error: error.toString()}));
};
function sendResponseAsync(res, results, index) {
try {
const next = results.next();
if (next.value !== undefined) {
res.write(',\n' + JSON.stringify(next.value, null, 2), () => {
sendResponseAsync(res, results, index);
});
} else {
res.end('\n]');
noisePool.release(index);
}
} catch(e) {
noisePool.release(index);
results.unref();
}
}
const server = http.createServer((req, res) => {
if (req.method == 'GET') {
res.statusCode = 200;
res.setHeader('Content-Type', 'text/html');
var buf = fs.readFileSync('./index.html');
res.end(buf);
} else if (req.method == 'POST') {
var str = '';
req.on('data', chunk => {
str += chunk;
if (str.length > maxPostSize) {
req.removeAllListeners('data');
req.removeAllListeners('end');
sendErrorResponse(res, 413, 'The query was too long.');
}
});
req.on('end', () => {
const indexPromise = noisePool.acquire();
indexPromise.then(index => {
accesslog(req, res, accessLogFormat, line => {
console.log(line,
'|',
str.replace(escapeNewlineRegexp, '\\n'));
});
return index.query(str).then(results => {
res.writeHead(200, headers);
res.write('[');
// First result is a special case to get the commas right
const first = results.next();
if (first.value !== undefined) {
try {
res.write('\n' + JSON.stringify(first.value, null, 2), 'utf8', () => {
sendResponseAsync(res, results, index);
});
} catch (e) {
results.unref();
throw e;
}
} else {
res.write('\n]');
res.end();
noisePool.release(index);
}
}).catch(error => {
noisePool.release(index);
sendErrorResponse(res, 400, error);
});
}).catch(error => {
sendErrorResponse(
res, 503, 'Too much load on the server, please try again');
});
});
}
});
server.listen(port, hostname, () => {
console.log(`Server running at http://${hostname}:${port}/`);
console.log(`Settings: {"NOISE_QUERY_INDEXES": ${numQueryIndexes}, ` +
`"NOISE_MAX_WAITING_CLIENTS": ${maxWaitingClients}, ` +
`"NOISE_ACQUIRE_TIMEOUT": ${acquireTimeoutMillis}}`);
});