summaryrefslogtreecommitdiff
path: root/Vimeo.js
blob: 0aa562e3770a6e996c15cd348dd28fe98b264539 (plain)
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
{
	"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": "2013-07-25 23:57:48"
}

/*
   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.match(/vimeo\.com\/search\?q=/)) {
		return "multiple";
	}
	return false;
}


function doWeb(doc, url) {
	if (detectWeb(doc, url) == "multiple") {
		var hits = {};
		var urls = [];
		var results = ZU.xpath(doc, "//li[contains(@id, 'clip_')]/a");

		for (var i in results) {
			hits[results[i].href] = results[i].title;
		}
		Z.selectItems(hits, function (items) {
			if (items == null) return true;
			for (var j in items) {
				urls.push(j);
			}
			ZU.processDocuments(urls, doWeb);
		});
	} else {
		// We call the Embedded Metadata translator to do the actual work
		var creator = ZU.xpathText(doc, '//div[@class="byline"]/a[1]');
		var date = ZU.xpathText(doc, '//meta[@itemprop="dateCreated"]/@content');
		var duration = ZU.xpathText(doc, '//meta[@itemprop="duration"]/@content');
		var translator = Zotero.loadTranslator("web");
		translator.setTranslator("951c027d-74ac-47d4-a107-9c3069ab7b48");
		translator.setDocument(doc);
		translator.setHandler("itemDone", function (obj, item) {
			item.itemType= "videoRecording";
			item.title = item.title.replace(/\s*on Vimeo$/, "");
			item.creators = ZU.cleanAuthor(creator, "author");
			if (date) item.date = date.replace(/T.+/, "");
			if (duration) item.runningTime = duration;
			item.extra = '';
			item.complete();
		});
		translator.translate();
	}
}
/** BEGIN TEST CASES **/
var testCases = [
	{
		"type": "web",
		"url": "http://vimeo.com/search?q=cello",
		"items": "multiple"
	},
	{
		"type": "web",
		"url": "http://vimeo.com/31179423",
		"items": [
			{
				"itemType": "videoRecording",
				"creators": {
					"firstName": "Alexander",
					"lastName": "Chen",
					"creatorType": "author"
				},
				"notes": [],
				"tags": [],
				"seeAlso": [],
				"attachments": [
					{
						"title": "Snapshot"
					}
				],
				"title": "Strings: J.S. Bach - Cello Suite No. 1 - Prelude",
				"url": "http://vimeo.com/31179423",
				"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 idea:…",
				"libraryCatalog": "vimeo.com",
				"date": "2011-10-26",
				"runningTime": "PT00H02M57S",
				"shortTitle": "Strings"
			}
		]
	}
]
/** END TEST CASES **/