summaryrefslogtreecommitdiff
path: root/EIDR.js
blob: b32892521b79b445eacdfb4e11c191c46b4db83f (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
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
{
	"translatorID": "79c3d292-0afc-42a1-bd86-7e706fc35aa5",
	"label": "EIDR",
	"creator": "Aurimas Vinckevicius",
	"target": "",
	"minVersion": "1.0",
	"maxVersion": "",
	"priority": 80,
	"inRepository": true,
	"translatorType": 8,
	"browserSupport": "gcsi",
	"lastUpdated": "2014-05-29 01:51:21"
}

var typeMap = {
//	'Series'
//	'Season'
//	'Supplemental'
//	'Composite'
//	'Compilation'
//	Interactive Material'
	'TV': 'tvBroadcast',
	'Movie': 'film',
	'Short': 'videoRecording',
	'Web': 'videoRecording'
};

var creatorMap = {
	'Director': 'director',
	'Actor': 'castMember'
};

function checkEIDR(eidr) {
	var suffix = eidr.trim().match(/10.5240\/((?:[0-9A-F]{4}-){5})([0-9A-Z])/i);
	if(!suffix) return false;

	//checksum
	//ISO 7064 Mod 37,36
	var id = suffix[1].replace(/-/g,'').toUpperCase().split('');
	var sum = 0;
	for(var i=0, n=id.length; i<n; i++) {
		sum += '0123456789ABCDEF'.indexOf(id[i]);
		sum = ( ((sum % 36) || 36) * 2 ) % 37;
	}

	sum += '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ'.indexOf(suffix[2]);

	return (sum % 36)===1;
}

function getValue(parentNode, node) {
	var n = parentNode.getElementsByTagName(node)[0];

	return n !== undefined ? n.textContent : undefined;
}

function detectSearch(item) {
	if(!item.DOI)
		return;

	//we should detect party and user but throw an error later
	//this way other translators don't need to process the DOI
	var prefix = item.DOI.split('/')[0];
	if([
			'10.5237', // Parties
			'10.5238', // Users
			'10.5239', // Video Services
			'10.5240'  // Content Records
		].indexOf(prefix) != -1)
	{
		return true;
	}
}

function  doSearch(searchItem) {
	if(!searchItem.DOI)
		throw new Error("EIDR not specified.");
	if(!checkEIDR(searchItem.DOI))
		throw new Error("EIDR not supported: " + searchItem.DOI);

	var request = 'https://resolve.eidr.org/EIDR/object/' + searchItem.DOI
					+ '/?type=Full&followAlias=true';
	ZU.doGet(request, function(text) {
		var parser = new DOMParser();
		var res = parser.parseFromString(text, "application/xml");

		var ns = {
			'n' : 'http://www.eidr.org/schema',
			'md': 'http://www.movielabs.com/schema/md/v2.1/md'
		};

		if(res.getElementsByTagName('Response').length) {

			throw new Error("Server returned error: ("
				+ getValue(res, 'Code') + ") "
				+ getValue(res, 'Type'));
		}

		var base = res.getElementsByTagName('BaseObjectData')[0];

		var type = typeMap[getValue(base,'ReferentType')];
		if(!type) {
			Z.debug("Unhandled ReferentType: " + getValue(base,'ReferentType'));
			return
		}
		var item = new Zotero.Item(type);

		//localize?
		item.title = getValue(base,'ResourceName');
		item.language = ZU.xpathText(base, './n:PrimaryLanguage/n:Language', ns);
		item.date = getValue(base,'ReleaseDate');
		item.place = getValue(base,'CountryOfOrigin');

		//running time
		var time = getValue(base,'ApproximateLength')
					.match(/PT(?:(\d+)H)?(?:(\d+)M)?(?:(\d+)S)?/i);
		item.runningTime = ((time[1] || 0) * 3600 +
							(time[2] || 0) * 60 +
							(time[3] || 0)) + 's';

		//creators
		var creators = base.getElementsByTagName('Credits')[0];
		var c, t;
		if(creators) {
			c = creators.firstChild;
			while(c) {
				t = creatorMap[c.nodeName];
				if(!t) continue;

				item.creators.push(
					ZU.cleanAuthor(getValue(c,'md:DisplayName'), t)
				);

				c = c.nextSibling;
			}
		}

		/**TODO: Handle producers*/

		item.complete();
	});
}

/** BEGIN TEST CASES **/
var testCases = [
	{
		"type": "search",
		"input": {
			"DOI": "10.5240/6F7E-EF59-329B-1F0A-8440-2"
		},
		"items": [
			{
				"itemType": "videoRecording",
				"creators": [
					{
						"lastName": "Rowland",
						"firstName": "Roy",
						"creatorType": "director"
					},
					{
						"firstName": "Byron",
						"lastName": "Shores",
						"creatorType": "castMember"
					},
					{
						"firstName": "C. Henry",
						"lastName": "Gordon",
						"creatorType": "castMember"
					}
				],
				"notes": [],
				"tags": [],
				"seeAlso": [],
				"attachments": [],
				"libraryCatalog": "EIDR",
				"title": "You, the People",
				"date": "1940",
				"place": "US",
				"runningTime": "1260s"
			}
		]
	}
]
/** END TEST CASES **/