-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathyoutube.js
66 lines (54 loc) · 1.81 KB
/
youtube.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
61
62
63
64
65
66
const { chain, compose, curry, map } = require('ramda')
const { safeProp, safeHead , maybeToTask, eitherToMaybe, maybeToEither, eitherToTask } = require('./utils')
const { getJSON } = require('./http')
const Task = require('data.task')
const Maybe = require('data.maybe')
const Either = require('data.either')
const { getFirstTrackArtist, getTrackName } = require('./spotify')
const YT_API_KEY = 'AIzaSyDg2dAih1lnlTL6kh6yuoEMnpnpcDntECk'
// youTubeSearchTerm :: {} -> Maybe String
const youTubeSearchTerm = response =>
Maybe.of(artist => trackName => `${artist} ${trackName}`)
.ap(getFirstTrackArtist(response))
.ap(getTrackName(response))
// youTubeSearchUrl :: String -> String
const youTubeSearchUrl = term => {
return `https://content.googleapis.com/youtube/v3/search?q=${term}&maxResults=5&part=snippet&key=${YT_API_KEY}`
}
// youTubeUrl :: String -> String
const youTubeUrl = id =>
`https://www.youtube.com/watch?v=${id}`
// responseToUrl :: {} -> Either URL String
const responseToUrl = compose(
maybeToEither('Unable to retrieve YouTube URL'),
map(youTubeUrl),
chain(safeProp('videoId')),
chain(safeProp('id')),
chain(safeHead),
safeProp('items')
)
// contactYouTube :: Either String URL -> Task Either String URL
const contactYouTube =
compose(
map(responseToUrl),
chain(getJSON({})),
map(youTubeSearchUrl),
eitherToTask
)
// searchOrElse :: Either String URL -> Task Either String URL
const searchOrElse = either =>
either.isLeft
? Task.of(either)
: contactYouTube(either)
// handleYouTubing :: String -> Task Either String URL
const handleYouTubing = compose(
searchOrElse,
maybeToEither('Unknown track id.'),
youTubeSearchTerm
)
// callYouTube :: [{}] -> Task [String]
const callYouTube = tracks =>
tracks.traverse(Task.of, handleYouTubing)
module.exports = {
callYouTube
}