-
Notifications
You must be signed in to change notification settings - Fork 762
/
YouTube.js
208 lines (187 loc) · 7 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
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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
{
"translatorID": "d3b1d34c-f8a1-43bb-9dd6-27aa6403b217",
"label": "YouTube",
"creator": "Sean Takats, Michael Berkowitz, Matt Burton, Rintze Zelle, and Geoff Banh",
"target": "^https?://([^/]+\\.)?youtube\\.com/",
"minVersion": "3.0",
"maxVersion": "",
"priority": 100,
"inRepository": true,
"translatorType": 4,
"browserSupport": "gcsibv",
"lastUpdated": "2024-04-05 04:04:37"
}
/*
***** BEGIN LICENSE BLOCK *****
Copyright © 2015-2024 Sean Takats, Michael Berkowitz, Matt Burton, Rintze Zelle, and Geoff Banh
This file is part of Zotero.
Zotero is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
Zotero is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with Zotero. If not, see <http://www.gnu.org/licenses/>.
***** END LICENSE BLOCK *****
*/
function detectWeb(doc, url) {
if (/\/watch\?(?:.*)\bv=[0-9a-zA-Z_-]+/.test(url)) {
return "videoRecording";
}
// Search results
/* Testurls:
http://www.youtube.com/user/Zoteron
http://www.youtube.com/playlist?list=PL793CABDF042A9514
http://www.youtube.com/results?search_query=zotero&oq=zotero&aq=f&aqi=g4&aql=&gs_sm=3&gs_upl=60204l61268l0l61445l6l5l0l0l0l0l247l617l1.2.1l4l0
*/
/* currently not working 2020-11-11
if ((url.includes("/results?") || url.includes("/playlist?") || url.includes("/user/"))
&& getSearchResults(doc, true)) {
return "multiple";
} */
return false;
}
function getSearchResults(doc, checkOnly) {
var links = doc.querySelectorAll('a.ytd-video-renderer, a.ytd-playlist-video-renderer');
var items = {},
found = false;
for (var i = 0, n = links.length; i < n; i++) {
var title = ZU.trimInternal(links[i].textContent);
var link = links[i].href;
if (!title || !link) continue;
if (checkOnly) return true;
found = true;
items[link] = title;
}
return found ? items : false;
}
function doWeb(doc, url) {
if (detectWeb(doc, url) != 'multiple') {
scrape(doc, url);
}
else {
Zotero.selectItems(getSearchResults(doc), function (items) {
if (!items) return;
var ids = [];
for (var i in items) {
ids.push(i);
}
ZU.processDocuments(ids, scrape);
});
}
}
function getMetaContent(doc, attrName, value) {
return attr(doc, 'meta[' + attrName + '="' + value + '"]', 'content');
}
function scrape(doc, url) {
var item = new Zotero.Item("videoRecording");
if (!Zotero.isServer) {
let jsonLD;
try {
jsonLD = JSON.parse(text(doc, 'script[type="application/ld+json"]'));
}
catch (e) {
jsonLD = {};
}
/* YouTube won't update the meta tags for the user,
* if they open e.g. a suggested video in the same tab.
* Thus we scrape them from screen instead.
*/
item.title = text(doc, '#info-contents h1.title') // Desktop
|| text(doc, '#title')
|| text(doc, '.slim-video-information-title'); // Mobile
// try to scrape only the canonical url, excluding additional query parameters
item.url = url.replace(/^(.+\/watch\?v=[0-9a-zA-Z_-]+).*/, "$1").replace('m.youtube.com', 'www.youtube.com');
item.runningTime = text(doc, '#movie_player .ytp-time-duration') // Desktop
|| text(doc, '.ytm-time-display .time-second'); // Mobile after unmute
if (!item.runningTime && jsonLD.duration) { // Mobile before unmute
let duration = parseInt(jsonLD.duration.substring(2));
let hours = String(Math.floor(duration / 3600)).padStart(2, '0');
let minutes = String(Math.floor(duration % 3600 / 60)).padStart(2, '0');
let seconds = String(duration % 60).padStart(2, '0');
if (duration >= 3600) { // Include hours
item.runningTime = `${hours}:${minutes}:${seconds}`;
}
else { // Just include minutes and seconds
item.runningTime = `${minutes}:${seconds}`;
}
}
item.date = ZU.strToISO(
text(doc, '#info-strings yt-formatted-string') // Desktop
|| attr(doc, 'ytm-factoid-renderer:last-child > div', 'aria-label') // Mobile if description has been opened
) || jsonLD.uploadDate; // Mobile on initial page load
var author = text(doc, '#meta-contents #text-container .ytd-channel-name') // Desktop
|| text(doc, '#upload-info #text-container .ytd-channel-name')
|| text(doc, '.slim-owner-channel-name'); // Mobile
if (author) {
item.creators.push({
lastName: author,
creatorType: "author",
fieldMode: 1
});
}
var description = text(doc, '#description .content')
|| text(doc, '#description')
|| text(doc, 'ytm-expandable-video-description-body-renderer .collapsed-string-container');
if (description) {
item.abstractNote = description;
}
}
else {
// required for translator server, which doesn't load the page's JS
item.title = getMetaContent(doc, 'name', 'title');
item.url = getMetaContent(doc, 'property', 'og:url');
let isoDuration = getMetaContent(doc, 'itemprop', 'duration');
// Convert ISO 8601 duration to HH:MM:SS
item.runningTime = isoDuration.replace(/^PT/, '').replace(/H/, ':').replace(/M/, ':')
.replace(/S/, '');
item.date = ZU.strToISO(getMetaContent(doc, 'itemprop', 'uploadDate'));
let author = attr(doc, 'link[itemprop="name"]', 'content');
if (author) {
item.creators.push({
lastName: author,
creatorType: "author",
fieldMode: 1
});
}
let description = getMetaContent(doc, 'name', 'description');
if (description) {
item.abstractNote = description;
}
}
item.complete();
}
/** BEGIN TEST CASES **/
var testCases = [
{
"type": "web",
"url": "https://www.youtube.com/watch?v=pq94aBrc0pY",
"defer": true,
"items": [
{
"itemType": "videoRecording",
"title": "Zotero Intro",
"creators": [
{
"lastName": "Zoteron",
"creatorType": "author",
"fieldMode": 1
}
],
"date": "2007-01-01",
"abstractNote": "Zotero is a free, easy-to-use research tool that helps you gather and organize resources (whether bibliography or the full text of articles), and then lets you to annotate, organize, and share the results of your research. It includes the best parts of older reference manager software (like EndNote)—the ability to store full reference information in author, title, and publication fields and to export that as formatted references—and the best parts of modern software such as del.icio.us or iTunes, like the ability to sort, tag, and search in advanced ways. Using its unique ability to sense when you are viewing a book, article, or other resource on the web, Zotero will—on many major research sites—find and automatically save the full reference information for you in the correct fields.",
"libraryCatalog": "YouTube",
"runningTime": "2:51",
"url": "https://www.youtube.com/watch?v=pq94aBrc0pY",
"attachments": [],
"tags": [],
"notes": [],
"seeAlso": []
}
]
}
]
/** END TEST CASES **/