/
info-extras.js
171 lines (157 loc) · 5.6 KB
/
info-extras.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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
const qs = require('querystring');
const url = require('url');
const Entities = require('html-entities').AllHtmlEntities;
const util = require('./util');
const parseTime = require('m3u8stream/lib/parse-time');
const VIDEO_URL = 'https://www.youtube.com/watch?v=';
const getMetaItem = (body, name) => {
return util.between(body, `<meta itemprop="${name}" content="`, '">');
};
/**
* Get video description from html
*
* @param {string} html
* @return {string}
*/
exports.getVideoDescription = (html) => {
const regex = /<p.*?id="eow-description".*?>(.+?)<\/p>[\n\r\s]*?<\/div>/im;
const description = html.match(regex);
return description ?
Entities.decode(util.stripHTML(description[1])) : '';
};
/**
* Get video media (extra information) from html
*
* @param {string} body
* @return {Object}
*/
exports.getVideoMedia = (body) => {
let mediainfo = util.between(body,
'<div id="watch-description-extras">',
'<div id="watch-discussion" class="branded-page-box yt-card">');
if (mediainfo === '') {
return {};
}
const regexp = /<h4 class="title">([\s\S]*?)<\/h4>[\s\S]*?<ul .*?class=".*?watch-info-tag-list">[\s\S]*?<li>([\s\S]*?)<\/li>(?:\s*?<li>([\s\S]*?)<\/li>)?/g;
const contentRegexp = /(?: - (\d{4}) \()?<a .*?(?:href="([^"]+)")?.*?>(.*?)<\/a>/;
const imgRegexp = /<img src="([^"]+)".*?>/;
const media = {};
const image = imgRegexp.exec(mediainfo);
if (image) {
media.image = url.resolve(VIDEO_URL, image[1]);
}
let match;
while ((match = regexp.exec(mediainfo)) != null) {
let [, key, value, detail] = match;
key = Entities.decode(key).trim().replace(/\s/g, '_').toLowerCase();
const content = contentRegexp.exec(value);
if (content) {
let [, year, mediaUrl, value2] = content;
if (year) {
media.year = parseInt(year);
} else if (detail) {
media.year = parseInt(detail);
}
value = value.slice(0, content.index);
if (key !== 'game' || value2 !== 'YouTube Gaming') {
value += value2;
}
media[key + '_url'] = url.resolve(VIDEO_URL, mediaUrl);
}
media[key] = Entities.decode(value);
}
return media;
};
/**
* Get video Owner from html.
*
* @param {string} body
* @return {Object}
*/
const userRegexp = /<a href="\/user\/([^"]+)/;
const verifiedRegexp = /<span .*?(aria-label="Verified")(.*?(?=<\/span>))/;
exports.getAuthor = (body) => {
let ownerinfo = util.between(body,
'<div id="watch7-user-header" class=" spf-link ">',
'<div id="watch8-action-buttons" class="watch-action-buttons clearfix">');
if (ownerinfo === '') {
return {};
}
const channelName = Entities.decode(util.between(util.between(
ownerinfo, '<div class="yt-user-info">', '</div>'), '>', '</a>'));
const userMatch = ownerinfo.match(userRegexp);
const verifiedMatch = ownerinfo.match(verifiedRegexp);
const channelID = getMetaItem(body, 'channelId');
const username = userMatch ? userMatch[1] : util.between(
util.between(body, '<span itemprop="author"', '</span>'), '/user/', '">');
return {
id: channelID,
name: channelName,
avatar: url.resolve(VIDEO_URL, util.between(ownerinfo,
'data-thumb="', '"')),
verified: !!verifiedMatch,
user: username,
channel_url: 'https://www.youtube.com/channel/' + channelID,
user_url: 'https://www.youtube.com/user/' + username,
};
};
/**
* Get video published at from html.
*
* @param {string} body
* @return {string}
*/
exports.getPublished = (body) => {
return Date.parse(getMetaItem(body, 'datePublished'));
};
/**
* Get video published at from html.
* Credits to https://github.com/paixaop.
*
* @param {string} body
* @return {Array.<Object>}
*/
exports.getRelatedVideos = (body) => {
let jsonStr = util.between(body, '\'RELATED_PLAYER_ARGS\': ', ',\n');
let watchNextJson, rvsParams, secondaryResults;
try {
jsonStr = JSON.parse(jsonStr);
watchNextJson = JSON.parse(jsonStr.watch_next_response);
rvsParams = jsonStr.rvs.split(',').map((e) => qs.parse(e));
secondaryResults = watchNextJson.contents.twoColumnWatchNextResults.secondaryResults.secondaryResults.results;
}
catch (err) {
return [];
}
let videos = [];
for (let result of secondaryResults) {
let details = result.compactVideoRenderer;
if (details) {
try {
let viewCount = details.viewCountText.simpleText;
let shortViewCount = details.shortViewCountText.simpleText;
let rvsDetails = rvsParams.find((elem) => elem.id === details.videoId);
if (!/^\d/.test(shortViewCount)) {
shortViewCount = rvsDetails && rvsDetails.short_view_count_text || '';
}
viewCount = (/^\d/.test(viewCount) ? viewCount : shortViewCount).split(' ')[0];
videos.push({
id: details.videoId,
title: details.title.simpleText,
author: details.shortBylineText.runs[0].text,
ucid: details.shortBylineText.runs[0].navigationEndpoint.browseEndpoint.browseId,
author_thumbnail: details.channelThumbnail.thumbnails[0].url,
short_view_count_text: shortViewCount.split(' ')[0],
view_count: viewCount.replace(',', ''),
length_seconds: details.lengthText ?
Math.floor(parseTime.humanStr(details.lengthText.simpleText) / 1000) :
rvsParams && rvsParams.length_seconds + '',
video_thumbnail: details.thumbnail.thumbnails[0].url,
});
} catch (err) {
continue;
}
}
}
return videos;
};