-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathspotify.js
33 lines (25 loc) · 812 Bytes
/
spotify.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
const request = require('request')
const Task = require('data.task')
const Either = require('data.either')
const httpGet = url =>
new Task((rej, res) =>
request(url, (error, response, body) =>
error ? rej(error) : res(body)))
const getJSON = url =>
httpGet(url)
.map(parse)
.chain(eitherToTask)
const first = xs =>
Either.fromNullable(xs[0])
const eitherToTask = e =>
e.fold(Task.rejected, Task.of)
const parse = Either.try(JSON.parse)
const findArtist = name =>
getJSON(`https://api.spotify.com/v1/search?q=${name}&type=artist`)
.map(result => result.artists.items)
.map(first)
.chain(eitherToTask)
const relatedArtists = id =>
getJSON(`https://api.spotify.com/v1/artists/${id}/related-artists`)
.map(result => result.artists)
module.exports = {findArtist, relatedArtists}