-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
136 lines (118 loc) · 3 KB
/
index.js
File metadata and controls
136 lines (118 loc) · 3 KB
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
import Koa from 'koa'
import Router from 'koa-router'
import bodyParser from 'koa-bodyparser'
import { log } from 'winston'
import request from 'request-promise'
import { hash } from 'RSVP'
require('dotenv').config()
const {
APP_PORT: port = 8080
} = process.env
const app = new Koa()
const router = new Router()
router
.get('/', hello)
.get('/resolve', resolve)
async function hello (ctx, next) {
ctx.body = 'API is running.'
}
/**
* Resolve tracks by searching on multiple sources
* (soundcloud, youtube, mixcloud, spotify and more...)
*/
async function resolve (ctx, next) {
const {
q,
limit = 10,
sc = true,
yt = true,
spotify = false,
mixcloud = false
} = ctx.query
if (!q) {
ctx.status = 400
ctx.body = {
message: `Required ':q?' parameter is missing`
}
}
const query = encodeURIComponent(q)
const requests = []
if (sc !== 'false') {
let clientId = process.env.SC_CLIENT_ID
let maxResults = limit <= 100 ? limit : 100
requests.push({
name: 'soundcloud',
host: 'api.soundcloud.com',
path: `tracks/?q=${query}&limit=${maxResults}&client_id=${clientId}`
})
}
if (yt !== 'false') {
let key = process.env.YT_API_KEY
let maxResults = limit <= 50 ? limit : 50
requests.push({
name: 'youtube',
host: 'www.googleapis.com',
path: `youtube/v3/search?part=snippet&q=${query}&key=${key}&maxResults=${maxResults}`
})
}
if (mixcloud) {
let maxResults = limit <= 100 ? limit : 100
requests.push({
name: 'mixcloud',
host: 'api.mixcloud.com',
path: `search/?q=${query}&type=cloudcast&limit=${maxResults}`
})
}
if (spotify) {
let maxResults = limit <= 50 ? limit : 50
requests.push({
name: 'spotify',
host: 'api.spotify.com',
path: `v1/search/?q=${query}&type=track&limit=${maxResults}`
})
}
try {
// Create an object containing an hash of each resolved promises
// {
// 'soundcloud': Promise,
// 'youtube': Promise
// 'mixcloud': Promise,
// 'spotify': Promise
// }
const promises = requests.map((req) => {
return {
[req.name]: new Promise(async (resolve, reject) => {
try {
resolve(JSON.parse(await request(`https://${req.host}/${req.path}`)))
} catch (err) {
reject(err)
}
})
}
}).reduce((result, item) => {
const key = Object.keys(item)[0]
result[key] = item[key]
return result
}, {})
ctx.body = await hash(promises)
} catch (err) {
ctx.status = err.statusCode || err.status || 500
ctx.body = {
message: err.message
}
}
}
app.use(async (ctx, next) => {
const start = Date.now()
await next()
const ms = Date.now() - start
log('info', `${ctx.method} ${ctx.url} - ${ms}ms`)
})
app
.use(bodyParser())
.use(router.routes())
.use(router.allowedMethods())
app.listen(port, (err) => {
if (err) throw err
log('info', `Tracklist-API listening on port ${port}`)
})