/
youtube.js
47 lines (42 loc) · 1.19 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
import { compose, prop, addIndex, map } from 'ramda';
import { serialize } from 'utils/';
const mapIndexed = addIndex(map);
const GAPI_URL = 'https://www.googleapis.com/youtube/v3';
const GAPI_KEY = 'AIzaSyArV70XKUil3cEj4nKn1yuMXCHiuK2AytI';
const GAPI_OPTS_SEARCH = {
key: GAPI_KEY,
maxResults: 15,
part: 'snippet',
type: 'video'
};
const GAPI_OPTS_LIST = {
key: GAPI_KEY,
part: 'contentDetails,statistics,status'
};
export const getStatistics = (resolve, reject, videos) => {
const params = {
...GAPI_OPTS_LIST,
...{ id: videos.map(_i => _i.id.videoId).join(',') }
};
fetch(`${GAPI_URL}/videos${serialize(params)}`)
.then(resp => resp.json())
.then(
compose(
resolve,
mapIndexed((_v, i) => ({
...videos[i],
duration: _v.contentDetails.duration,
viewCount: _v.statistics.viewCount
})),
prop('items')
)
)
.catch(reject);
};
export const getYoutubeSearch = q =>
new Promise((resolve, reject) =>
fetch(`${GAPI_URL}/search${serialize({ ...GAPI_OPTS_SEARCH, q })}`)
.then(resp => resp.json())
.then(({ items }) => getStatistics(resolve, reject, items))
.catch(reject)
);