1 | const qs = require('querystring');
|
2 | const url = require('url');
|
3 | const Entities = require('html-entities').AllHtmlEntities;
|
4 | const util = require('./util');
|
5 | const parseTime = require('m3u8stream/dist/parse-time');
|
6 |
|
7 |
|
8 |
|
9 | const VIDEO_URL = 'https://www.youtube.com/watch?v=';
|
10 | const getMetaItem = (body, name) => {
|
11 | return util.between(body, `<meta itemprop="${name}" content="`, '">');
|
12 | };
|
13 |
|
14 |
|
15 |
|
16 |
|
17 |
|
18 |
|
19 |
|
20 |
|
21 | exports.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 |
|
30 |
|
31 |
|
32 |
|
33 |
|
34 | exports.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 |
|
77 |
|
78 |
|
79 |
|
80 |
|
81 | const userRegexp = /<a href="\/user\/([^"]+)/;
|
82 | const verifiedRegexp = /<span .*?(aria-label="Verified")(.*?(?=<\/span>))/;
|
83 | exports.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 |
|
112 |
|
113 |
|
114 |
|
115 |
|
116 | exports.getPublished = (body) => {
|
117 | return Date.parse(getMetaItem(body, 'datePublished'));
|
118 | };
|
119 |
|
120 |
|
121 |
|
122 |
|
123 |
|
124 |
|
125 |
|
126 |
|
127 |
|
128 | exports.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 |
|
175 |
|
176 |
|
177 |
|
178 |
|
179 | const getLikesRegex = /\\"likeCount\\":(\d+?),\\"likeCountText\\"/;
|
180 | exports.getLikes = (body) => {
|
181 | const likes = body.match(getLikesRegex);
|
182 | return likes ? parseInt(likes[1]) : null;
|
183 | };
|
184 |
|
185 |
|
186 |
|
187 |
|
188 |
|
189 |
|
190 |
|
191 | const getDislikesRegex = /\\"dislikeCount\\":(\d+?),\\"dislikeCountText\\"/;
|
192 | exports.getDislikes = (body) => {
|
193 | const dislikes = body.match(getDislikesRegex);
|
194 | return dislikes ? parseInt(dislikes[1]) : null;
|
195 | };
|