UNPKG

6.15 kBJavaScriptView Raw
1const qs = require('querystring');
2const url = require('url');
3const Entities = require('html-entities').AllHtmlEntities;
4const util = require('./util');
5const parseTime = require('m3u8stream/dist/parse-time');
6
7
8
9const VIDEO_URL = 'https://www.youtube.com/watch?v=';
10const getMetaItem = (body, name) => {
11 return util.between(body, `<meta itemprop="${name}" content="`, '">');
12};
13
14
15/**
16 * Get video description from html
17 *
18 * @param {string} html
19 * @return {string}
20 */
21exports.getVideoDescription = (html) => {
22 const regex = /<p.*?id="eow-description".*?>(.+?)<\/p>[\n\r\s]*?<\/div>/im;
23 const description = html.match(regex);
24 return description ?
25 Entities.decode(util.stripHTML(description[1])) : '';
26};
27
28/**
29 * Get video media (extra information) from html
30 *
31 * @param {string} body
32 * @return {Object}
33 */
34exports.getVideoMedia = (body) => {
35 let mediainfo = util.between(body,
36 '<div id="watch-description-extras">',
37 '<div id="watch-discussion" class="branded-page-box yt-card">');
38 if (mediainfo === '') {
39 return {};
40 }
41
42 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;
43 const contentRegexp = /(?: - (\d{4}) \()?<a .*?(?:href="([^"]+)")?.*?>(.*?)<\/a>/;
44 const imgRegexp = /<img src="([^"]+)".*?>/;
45 const media = {};
46
47 const image = imgRegexp.exec(mediainfo);
48 if (image) {
49 media.image = url.resolve(VIDEO_URL, image[1]);
50 }
51
52 let match;
53 while ((match = regexp.exec(mediainfo)) != null) {
54 let [, key, value, detail] = match;
55 key = Entities.decode(key).trim().replace(/\s/g, '_').toLowerCase();
56 const content = contentRegexp.exec(value);
57 if (content) {
58 let [, year, mediaUrl, value2] = content;
59 if (year) {
60 media.year = parseInt(year);
61 } else if (detail) {
62 media.year = parseInt(detail);
63 }
64 value = value.slice(0, content.index);
65 if (key !== 'game' || value2 !== 'YouTube Gaming') {
66 value += value2;
67 }
68 media[key + '_url'] = url.resolve(VIDEO_URL, mediaUrl);
69 }
70 media[key] = Entities.decode(value);
71 }
72 return media;
73};
74
75/**
76 * Get video Owner from html.
77 *
78 * @param {string} body
79 * @return {Object}
80 */
81const userRegexp = /<a href="\/user\/([^"]+)/;
82const verifiedRegexp = /<span .*?(aria-label="Verified")(.*?(?=<\/span>))/;
83exports.getAuthor = (body) => {
84 let ownerinfo = util.between(body,
85 '<div id="watch7-user-header" class=" spf-link ">',
86 '<div id="watch8-action-buttons" class="watch-action-buttons clearfix">');
87 if (ownerinfo === '') {
88 return {};
89 }
90 const channelName = Entities.decode(util.between(util.between(
91 ownerinfo, '<div class="yt-user-info">', '</div>'), '>', '</a>'));
92 const userMatch = ownerinfo.match(userRegexp);
93 const verifiedMatch = ownerinfo.match(verifiedRegexp);
94 const channelID = getMetaItem(body, 'channelId');
95 const username = userMatch ? userMatch[1] : util.between(
96 util.between(body, '<span itemprop="author"', '</span>'), '/user/', '">');
97 return {
98 id: channelID,
99 name: channelName,
100 avatar: url.resolve(VIDEO_URL, util.between(ownerinfo,
101 'data-thumb="', '"')),
102 verified: !!verifiedMatch,
103 user: username,
104 channel_url: 'https://www.youtube.com/channel/' + channelID,
105 user_url: 'https://www.youtube.com/user/' + username,
106 };
107};
108
109
110/**
111 * Get video published at from html.
112 *
113 * @param {string} body
114 * @return {string}
115 */
116exports.getPublished = (body) => {
117 return Date.parse(getMetaItem(body, 'datePublished'));
118};
119
120
121/**
122 * Get video published at from html.
123 * Credits to https://github.com/paixaop.
124 *
125 * @param {string} body
126 * @return {Array.<Object>}
127 */
128exports.getRelatedVideos = (body) => {
129 let jsonStr = util.between(body, '\'RELATED_PLAYER_ARGS\': ', /,[\n\r]/);
130 let watchNextJson, rvsParams, secondaryResults;
131 try {
132 jsonStr = JSON.parse(jsonStr);
133 watchNextJson = JSON.parse(jsonStr.watch_next_response);
134 rvsParams = jsonStr.rvs.split(',').map((e) => qs.parse(e));
135 secondaryResults = watchNextJson.contents.twoColumnWatchNextResults.secondaryResults.secondaryResults.results;
136 }
137 catch (err) {
138 return [];
139 }
140 let videos = [];
141 for (let result of secondaryResults) {
142 let details = result.compactVideoRenderer;
143 if (details) {
144 try {
145 let viewCount = details.viewCountText.simpleText;
146 let shortViewCount = details.shortViewCountText.simpleText;
147 let rvsDetails = rvsParams.find((elem) => elem.id === details.videoId);
148 if (!/^\d/.test(shortViewCount)) {
149 shortViewCount = rvsDetails && rvsDetails.short_view_count_text || '';
150 }
151 viewCount = (/^\d/.test(viewCount) ? viewCount : shortViewCount).split(' ')[0];
152 videos.push({
153 id: details.videoId,
154 title: details.title.simpleText,
155 author: details.shortBylineText.runs[0].text,
156 ucid: details.shortBylineText.runs[0].navigationEndpoint.browseEndpoint.browseId,
157 author_thumbnail: details.channelThumbnail.thumbnails[0].url,
158 short_view_count_text: shortViewCount.split(' ')[0],
159 view_count: viewCount.replace(',', ''),
160 length_seconds: details.lengthText ?
161 Math.floor(parseTime.humanStr(details.lengthText.simpleText) / 1000) :
162 rvsParams && rvsParams.length_seconds + '',
163 video_thumbnail: details.thumbnail.thumbnails[0].url,
164 });
165 } catch (err) {
166 continue;
167 }
168 }
169 }
170 return videos;
171};
172
173/**
174 * Get like count from html.
175 *
176 * @param {string} body
177 * @return {number}
178 */
179const getLikesRegex = /\\"likeCount\\":(\d+?),\\"likeCountText\\"/;
180exports.getLikes = (body) => {
181 const likes = body.match(getLikesRegex);
182 return likes ? parseInt(likes[1]) : null;
183};
184
185/**
186 * Get dislike count from html.
187 *
188 * @param {string} body
189 * @return {number}
190 */
191const getDislikesRegex = /\\"dislikeCount\\":(\d+?),\\"dislikeCountText\\"/;
192exports.getDislikes = (body) => {
193 const dislikes = body.match(getDislikesRegex);
194 return dislikes ? parseInt(dislikes[1]) : null;
195};