forked from internet4000/media-now
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
47 lines (40 loc) · 1.25 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
require('dotenv').config()
const opbeat = require('opbeat').start({
active: process.env.NODE_ENV === 'production'
})
const {send} = require('micro')
const microCors = require('micro-cors')
const youtube = require('./src/serializer-youtube')
const vimeo = require('./src/serializer-vimeo')
const discogs = require('./src/serializer-discogs')
const spotify = require('./src/serializer-spotify')
const spotifySearch = require('./src/serializer-spotify-search')
const analyse = require('./src/serializer-analyse')
const serializers = {
youtube, vimeo, discogs, spotify,
'spotify-search': spotifySearch,
analyse
}
const cors = microCors({allowMethods: ['GET']})
module.exports = cors(async (req, res) => {
const args = req.url.split('/')
const provider = args[1]
const id = args[2]
const serializer = serializers[provider]
opbeat.setTransactionName(req.url)
if (!serializer) {
send(res, 404, {
error: `Hello. No serializer found for '${provider}'.`,
help: 'https://github.com/internet4000/media-now'
})
return
}
// Fetch the data, convert to json, extract what we need.
try {
const data = await serializer.fetchData(id)
const json = await data.json()
return serializer.serialize(json)
} catch (err) {
send(res, 404, {error: err.message})
}
})