-
Notifications
You must be signed in to change notification settings - Fork 786
/
Copy pathVimeo.js
188 lines (173 loc) · 4.97 KB
/
Vimeo.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
{
"translatorID": "1b0ffe71-1c2f-4a79-b894-40b990b3e491",
"label": "Vimeo",
"creator": "Sebastian Karcher",
"target": "^https?://(www\\.)?vimeo\\.com",
"minVersion": "3.0",
"maxVersion": "",
"priority": 100,
"inRepository": true,
"translatorType": 4,
"browserSupport": "gcsibv",
"lastUpdated": "2019-06-11 13:37:31"
}
/*
Vimeo Translator
Copyright (C) 2012 Sebastian Karcher
This program 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.
This program 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 this program. If not, see <http://www.gnu.org/licenses/>.
*/
function detectWeb(doc, url) {
// the meta properties are missing once you're logged in
var xpath = '//meta[@property="og:video:type"]|//div[@class="video_meta"]';
if (ZU.xpath(doc, xpath).length > 0) {
return "videoRecording";
}
if (url.includes('vimeo.com/search?q=') && getSearchResults(doc, true)) {
return "multiple";
}
return false;
}
function getSearchResults(doc, checkOnly) {
var items = {};
var found = false;
var rows = ZU.xpath(doc, '//div[contains(@class, "iris_p_infinite__item")]//a[div/h5 and contains(@href, "//vimeo.com/")]');
for (var i = 0; i < rows.length; i++) {
var href = rows[i].href;
var title = ZU.xpathText(rows[i], './/h5');
if (!href || !title) continue;
if (checkOnly) return true;
found = true;
items[href] = title;
}
// Due to some dynamic loading the DOM might not be ready for the method above
// but the data is also saved in some JSON object in a script tag of the website.
if (!found) {
var script = ZU.xpathText(doc, '//body/script[contains(., "vimeo.config")]');
if (script) {
var start = script.indexOf('vimeo.config');
var stop = script.indexOf('\n', start);
var data = script.substring(start + 45, stop - 2);
var json = JSON.parse(data);
if (json && json.api && json.api.initial_json && json.api.initial_json.data) {
var results = json.api.initial_json.data;
for (var entry of results) {
if (entry.clip && entry.clip.link && entry.clip.name) {
items[entry.clip.link] = entry.clip.name;
found = true;
}
}
}
}
}
return found ? items : false;
}
function doWeb(doc, url) {
if (detectWeb(doc, url) == "multiple") {
Zotero.selectItems(getSearchResults(doc, false), function (items) {
if (!items) return;
var articles = [];
for (var i in items) {
articles.push(i);
}
ZU.processDocuments(articles, scrape);
});
}
else {
scrape(doc, url);
}
}
function scrape(doc, _url) {
var json = ZU.xpathText(doc, '//script[@type="application/ld+json"]');
var objects = JSON.parse(json);
var videoObject;
for (var i = 0; i < objects.length; i++) {
if (objects[i]["@type"] == "VideoObject") {
videoObject = objects[i];
}
}
var item = new Zotero.Item("videoRecording");
item.title = videoObject.name;
item.url = videoObject.url;
item.abstractNote = videoObject.description;
item.runningTime = videoObject.duration;
item.date = videoObject.uploadDate;
var author = videoObject.author;
if (author) {
item.creators.push(ZU.cleanAuthor(author.name, "author"));
}
var keywords = videoObject.keywords;
if (keywords) {
var tags = keywords.replace('[', '').replace(']', '').split(',');
for (var j = 0; j < tags.length; j++) {
item.tags.push(tags[j]);
}
}
item.attachments.push({
title: "Snapshot",
document: doc
});
item.complete();
}/** BEGIN TEST CASES **/
var testCases = [
{
"type": "web",
"url": "http://vimeo.com/search?q=cello",
"items": "multiple"
},
{
"type": "web",
"url": "https://vimeo.com/31179423",
"items": [
{
"itemType": "videoRecording",
"title": "Strings: J.S. Bach - Cello Suite No. 1 - Prelude",
"creators": [
{
"firstName": "Alexander",
"lastName": "Chen",
"creatorType": "author"
}
],
"date": "2011-10-26T22:29:03-04:00",
"abstractNote": "Strings (2011) by Alexander Chen visualizes the first Prelude from Bach's Cello Suites. Using the math behind string length and pitch, it came from a simple…",
"libraryCatalog": "Vimeo",
"runningTime": "PT00H02M57S",
"shortTitle": "Strings",
"url": "https://vimeo.com/31179423",
"attachments": [
{
"title": "Snapshot"
}
],
"tags": [
"art",
"audio",
"classical",
"experimental",
"html5",
"interactive",
"music",
"sound",
"visualization"
],
"notes": [],
"seeAlso": []
}
]
},
{
"type": "web",
"url": "https://vimeo.com/search?q=zotero",
"items": "multiple"
}
]
/** END TEST CASES **/