forked from fredericd/coce
-
Notifications
You must be signed in to change notification settings - Fork 0
/
coce.js
296 lines (278 loc) · 8.35 KB
/
coce.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
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
/* eslint-disable no-undef */
/* eslint-disable no-eval */
const fs = require('fs');
const http = require('http');
const https = require('https');
const config = eval(`(${fs.readFileSync('config.json', 'utf8')})`);
exports.config = config;
const redis = require('redis').createClient(config.redis.port, config.redis.host);
/**
* CoceFetcher class
*
* @class CoceFetcher
* @module coce
* @constructor
* @param {int} timeout Timeout in miliseconds. After this delay, the fetching
* is aborted, even if some providers haven't yet responded. Without param, the
* timeout is retrieved from config.json file.
*/
const CoceFetcher = function CoceFetcher(timeout) {
this.timeout = timeout === undefined ? config.timeout : timeout;
this.count = 0;
this.finished = false;
/**
* URLs found in the cache (or from providers)
* @property url
* @type Object
*/
this.url = {};
};
exports.CoceFetcher = CoceFetcher;
CoceFetcher.RegGb = new RegExp('(zoom=5)', 'g');
/**
* Retrieve an ID from Amazon. Cover image direct URL are tested.
* @method aws_http
* @param {Array} ids The resource IDs to request to Amazon
*/
CoceFetcher.prototype.aws = function awsFetcher(ids) {
const repo = this;
let i = 0;
const checkoneurl = () => {
const id = ids[i];
let search = id;
// If ISBN13, transform it into ISBN10
search = search.replace(/-/g, '');
if (search.length === 13) {
search = search.substr(3, 9);
// Calculate checksum
let checksum = search.split('').reduce((s, c, ii) => s + parseInt(c, 10) * (ii + 1), 0);
checksum %= 11;
checksum = checksum === 10 ? 'X' : checksum;
search += checksum;
}
const opts = {
hostname: 'images-na.ssl-images-amazon.com',
method: 'HEAD',
headers: { 'user-agent': 'Mozilla/5.0' },
path: `/images/P/${search}.01.MZZZZZZZZZ.jpg`,
};
https.get(opts, (res) => {
const url = `https://${opts.hostname}${opts.path}`;
if (res.statusCode === 200 || res.statusCode === 403) repo.addurl('aws', id, url);
repo.increment();
i += 1;
// timeout for next request
if (i < ids.length) setTimeout(checkoneurl, 30);
});
};
checkoneurl();
};
/**
* Retrieve an ID from Google Books
* @method gb
* @param {Array} ids The resource IDs to request to Google Books
*/
CoceFetcher.prototype.gb = function gb(ids) {
const repo = this;
const opts = {
host: 'books.google.com',
port: 443,
path: `/books?bibkeys=${ids.join(',')}&jscmd=viewapi&hl=en`,
};
https.get(opts, (res) => {
res.setEncoding('utf8');
let store = '';
res.on('data', (data) => { store += data; });
res.on('end', () => {
eval(store);
// eslint-disable-next-line no-undef
Object.values(_GBSBookInfo).forEach((item) => {
const id = item.bib_key;
let url = item.thumbnail_url;
if (url === undefined) return;
// get the medium size cover image
url = url.replace(CoceFetcher.RegGb, 'zoom=1');
repo.addurl('gb', id, url);
});
repo.increment(ids.length);
});
});
};
/**
* Retrieve an ID from ORB
* @method orb
* @param {Array} ids The resource IDs to request to ORB
*/
CoceFetcher.prototype.orb = function orb(ids) {
const repo = this;
const opts = {
host: 'api.base-orb.fr',
auth: `${config.orb.user}:${config.orb.key}`,
port: 443,
path: `/v1/products?eans=${ids.join(',')}&sort=ean_asc`,
};
https.get(opts, (res) => {
res.setEncoding('utf8');
let store = '';
res.on('data', (data) => { store += data; });
res.on('end', () => {
const orbres = JSON.parse(store);
Object.values(orbres.data).forEach((item) => {
const id = item.ean13;
const url = item.images.front.thumbnail.src;
if (url === undefined) return;
repo.addurl('orb', id, url);
});
repo.increment(ids.length);
});
});
};
/**
* Retrieve an ID from Open Library
* @method ol
* @param {Array} ids The resource IDs to request to Open Library
*/
CoceFetcher.prototype.ol = function ol(ids) {
const repo = this;
const opts = {
host: 'openlibrary.org',
port: 80,
path: `/api/books?bibkeys=${ids.join(',')}&jscmd=data`,
};
http.get(opts, (res) => {
res.setEncoding('utf8');
let store = '';
res.on('data', (data) => { store += data; });
res.on('end', () => {
eval(store);
Object.keys(_OLBookInfo).forEach((id) => {
let url = _OLBookInfo[id].cover;
if (url === undefined) return;
url = url[config.ol.imageSize];
repo.addurl('ol', id, url);
});
repo.increment(ids.length);
});
});
};
/**
* Add an url to redis
* Cache locally a file if necessary
*/
CoceFetcher.prototype.addurl = function addurl(provider, id, url) {
let storedUrl;
if (config[provider].cache) {
storedUrl = `${config.cache.url}/${provider}/${id}.jpg`;
const dest = `${config.cache.path}/${provider}/${id}.jpg`;
const file = fs.createWriteStream(dest);
https.get(url, (response) => {
response.pipe(file);
file.on('finish', () => file.close());
}).on('error', () => fs.unlink(dest));
} else {
storedUrl = url;
}
redis.setex(`${provider}.${id}`, config[provider].timeout, storedUrl);
if (this.url[id] === undefined) this.url[id] = {};
this.url[id][provider] = storedUrl;
};
/**
* Increment the count of found URLs.
* Stop the timer if all URLs have been found.
* @param {int} increment Increment the number of found IDs. No parameter = 1.
*/
CoceFetcher.prototype.increment = function incr(increment = 1) {
this.count += increment;
if (this.count >= this.countMax) {
clearTimeout(this.timeoutId);
if (!this.finished) {
this.finished = true;
this.finish(this.url);
}
}
};
/**
* Retrieve IDs from a provider
* @method add
* @param {Array} ids Group of IDs
* @param {String} provider (aws|gb|ol) to search for
*/
CoceFetcher.prototype.add = function add(ids, provider) {
const repo = this;
const notcached = [];
let count = ids.length;
let timeoutId;
for (let i = 0; i < ids.length; i += 1) {
// eslint-disable-next-line no-loop-func
(function addId() {
const id = ids[i];
const key = `${provider}.${ids[i]}`;
redis.get(key, (err, reply) => {
count -= 1;
if (reply === null) {
// Not in the cache
notcached.push(id);
redis.setex(`${provider}.${id}`, config[provider].timeout, '');
} else if (reply === '') {
// In the cache, but no url via provider
repo.increment();
} else {
if (repo.url[id] === undefined) repo.url[id] = {};
repo.url[id][provider] = reply;
repo.increment();
}
if (count === 0 && timeoutId !== undefined) {
// We get all responses from Redis
clearTimeout(timeoutId);
if (notcached.length > 0) repo[provider](notcached);
}
});
}());
}
// Wait all Redis responses
timeoutId = setTimeout(() => {
if (notcached.length > 0) repo[provider](notcached);
}, config.redis.timeout);
};
/**
* Fetch all provided ID from cover image providers
* Wait for providers reponses, with a limitation of time
* @method fetch
* @param {Array} ids Array of images ID
* @param {Array} providers Array of images providers (gb, aws, ol)
* @param timeout {int} Max duration of the fetching
* @param finish {Function} Function to execute when all URLs are fetched or time has
* elasped
*/
CoceFetcher.prototype.fetch = function fetch(ids, providers, finish) {
this.count = 0;
this.countMax = ids.length * providers.length;
this.finish = finish;
this.finished = false;
this.url = {};
// Validate providers
if (providers === undefined) {
finish({ error: 'At least, one provider is required' });
return;
}
for (let i = 0; i < providers.length; i += 1) {
provider = providers[i];
if (config.providers.indexOf(provider) === -1) {
finish({ error: `Unavailable provider: ${provider}` });
return;
}
}
for (let i = 0; i < providers.length; i += 1) this.add(ids, providers[i]);
if (this.count !== this.countMax) {
const repo = this;
this.timeoutId = setTimeout(() => {
if (!repo.finished) {
repo.finished = true;
repo.finish(repo.url);
}
}, this.timeout);
}
};
exports.set = function setex(provider, id, url) {
redis.setex(`aws.${id}`, 315360000, url);
};