forked from fredericd/coce
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
60 lines (55 loc) · 1.75 KB
/
app.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
const express = require('express')
const app = express()
const coce = require('./coce')
app.listen(coce.config.port)
app.get('/', function (req, res) {
res.send('Welcome')
})
const isbnRE = /([0-9X]{10,13})/
app.get('/cover', (req, res) => {
let ids = req.query.id;
const api_key = req.query.api_key;
if( !api_key || api_key !== process.env.API_KEY ) {
return res.status(401).json( { error: 'Unauthorized'} )
}
if (ids === undefined || ids.length < 8) {
var fail = {}
fail.error = "ID parameter is missing"
res.send(fail)
return
}
ids = ids.split(',')
if (ids.length === 0) {
var fail = {}
fail.error = "Bad id parameter"
res.send(fail)
return
}
let providers = req.query.provider
providers = providers == undefined ? coce.config.providers : providers.split(',')
const callback = req.query.callback
var fetcher = new coce.CoceFetcher()
fetcher.fetch(ids, providers, function (url) {
if (req.query.all === undefined) {
// Not &all param: URL are picked up by provider priority order
// Just the first available url
var ret = {}
for (var id in url)
for (var j = 0, provider; provider = providers[j]; j++) {
var u = url[id][provider]
if (u !== undefined) {
ret[id] = u;
break;
}
}
url = ret
}
if (callback) {
res.contentType("application/javascript")
url = callback + '(' + JSON.stringify(url) + ')'
} else {
res.contentType("application/json")
}
res.send(url)
})
})