-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathspotify.js
58 lines (46 loc) · 1.61 KB
/
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
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
const { getJSON, postJSON } = require('./http')
const { chain, curry, compose } = require('ramda')
const { safeProp, safeHead } = require('./utils')
const Task = require('data.task')
const { maybeToTask } = require('./utils')
const CLIENT_ID = 'b03740660cad4846b28ed45a6760ab69'
const CLIENT_SECRET = '406d9c93c11b417b9ab3f9f9d59b9247'
const authHeader =
Buffer.from(`${CLIENT_ID}:${CLIENT_SECRET}`).toString('base64')
const cacheAccessToken = res => res
// getAccessToken :: () -> Task String
const getAccessToken = () =>
postJSON({Authorization: `Basic ${authHeader}`},
'https://accounts.spotify.com/api/token',
{grant_type: 'client_credentials'})
.map(cacheAccessToken)
const trackUrl = id => `https://api.spotify.com/v1/tracks/${id}`
// bearerTokenHeader :: String -> {}
const bearerTokenHeader = token =>
({
Authorization: `Bearer ${token}`
})
// getTrackInfo :: String -> String -> Task JSON
const getTrackInfo = curry((trackId, accessToken) =>
getJSON(bearerTokenHeader(accessToken), trackUrl(trackId)))
// contactSpotify :: String -> Task JSON
const contactSpotify = trackId =>
getAccessToken()
.map(safeProp('access_token'))
.chain(maybeToTask)
.chain(getTrackInfo(trackId))
// getTrackName :: {} -> Maybe String
const getTrackName = safeProp('name')
// getFirstTrackArtist :: {} -> Maybe String
const getFirstTrackArtist = compose(
chain(safeProp('name')),
chain(safeHead),
safeProp('artists'))
// callSpotify :: [String] -> Task [String]
const callSpotify = songs =>
songs.traverse(Task.of, contactSpotify)
module.exports = {
callSpotify,
getFirstTrackArtist,
getTrackName,
}