-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathindex.js
63 lines (52 loc) · 1.53 KB
/
index.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
'use strict';
const got = require('got');
const cheerio = require('cheerio');
const url = 'https://youtube.com/watch?v=';
const tag = {
name: 'data-title',
url: 'data-video-id',
id: 'data-video-id'
};
const splitCurrentPlay = str => {
return str.indexOf('watch') === -1 ? `${str}&disable_polymer=1` : `https://www.youtube.com/playlist?list=${str.split('&list=')[1].split('&t=')[0]}&disable_polymer=1`;
};
const isPrivateVideo = tr => {
const isOwnerFieldExist = Boolean(cheerio('.pl-video-owner', tr).length);
return !isOwnerFieldExist;
};
module.exports = (data, opt) => {
return got(splitCurrentPlay(data)).then(res => {
const $ = cheerio.load(res.body);
const thumb = $('tr');
const arr = {
name: $('.pl-header-title').text().trim(),
playlist: []
};
if (!opt) {
opt = Object.keys(tag);
}
const prefixUrl = (holder, marks) => holder === 'url' ? `${url}${marks}` : marks;
const getDuration = el => {
const raw = $(el).find('.timestamp').text().split(':');
return (parseInt(raw[0], 10) * 60) + parseInt(raw[1], 10);
};
const multipleDetails = Array.isArray(opt);
arr.playlist = thumb.map((index, el) => {
if (multipleDetails) {
return opt.reduce((prev, holder) => {
prev[holder] = prefixUrl(holder, holder === 'duration' ? getDuration(el) : el.attribs[tag[holder]]);
return prev;
}, {
isPrivate: isPrivateVideo(el)
});
}
if (opt === 'duration') {
return getDuration(el);
}
return prefixUrl(opt, el.attribs[tag[opt]]);
}).get();
return {
data: arr
};
});
};