summaryrefslogtreecommitdiff
path: root/Wanfang Data.js
blob: ef68dd58d8af57e13fa08ebf683eac4cfc119091 (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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
{
	"translatorID": "eb876bd2-644c-458e-8d05-bf54b10176f3",
	"label": "Wanfang Data",
	"creator": "Ace Strong <acestrong@gmail.com>",
	"target": "^https?://[ds]\\.(?:g\\.)?wanfangdata\\.com\\.cn",
	"minVersion": "2.0rc1",
	"maxVersion": "",
	"priority": 100,
	"inRepository": true,
	"translatorType": 4,
	"browserSupport": "gcs",
	"lastUpdated": "2014-07-24 01:37:16"
}

/*
   Wanfang Data Translator
   Copyright (C) 2010 TAO Cheng, acestrong@gmail.com

   This program is free software: you can redistribute it and/or modify
   it under the terms of the GNU 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 General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

// #######################
// ##### Sample URLs #####
// #######################

/*
 * The starting point for an search is the URL below.
 * In testing, I tried the following:
 *
 *   - A search listing of journals
 *   - A search listing of thesis
 *   - A search listing of conference papers
 *   - A search listing of foreign literatures(for chinese)
 *   - A journal paper page
 *   - A thesis page
 *   - A conference paper page
 *   - A foreign literature page
 */
// http://g.wanfangdata.com.cn/Default.aspx


// #################################
// #### Local utility functions ####
// #################################

//sets the rs cookie value to the provided ID and returns the old value
function setCookie(doc, rsId) {
	if(!rsId) return null;

	var matches = doc.cookie.match(/(?:$|; )rs=([^;]*)/);
	var oldCookie = matches ? unescape(matches[1]) : null;

	var domain = escape('wanfangdata.com.cn');
	var id = escape( rsId );

	doc.cookie = 'rs=' + id + ';domain=' + domain;

	return oldCookie;
}

function detectCode(url) {
	var pattern = /[ds]\.(?:g\.)?wanfangdata\.com\.cn\/([A-Za-z]*?)_/;
	if (pattern.test(url)) {
		var code = pattern.exec(url)[1];
		return code;
	}
	return null;
}

function detectType(code) {
	if (code == "Periodical" ||
		code == "OAPaper") {
		return "journalArticle";
	} else if (code == "Thesis") {
		return "thesis";
	} else if (code == "Conference") {
		return "conferencePaper";
	} else if (code == "NSTLHY") {
		return "conferencePaper";
	} else if (code == "NSTLQK") {
		return "journalArticle";
	} else {
		return false;
	}
}

function getSearchResults(doc, checkOnly) {
	var items = [], found = false
		lis = ZU.xpath(doc, '//li[contains(@class,"title_li")]');
	for(var i=0, n=lis.length; i<n; i++) {
		var a = lis[i].getElementsByTagName("a")[1];
		var title = ZU.trimInternal( ZU.cleanTags(a.textContent) );
		var link = a.getAttribute("href").match(/\/([^\/]+)\.aspx/)[1];
		if (!link) continue;
		
		if(checkOnly) return true;
		
		items[link] = title;
		found = true;
	}
	
	return found ? items : false;
}

function getItemId(doc) {
	var action = ZU.xpathText(doc, '//form[@id="aspnetForm"]/@action');
	if(!action) return;
	
	var id = action.match(/(?:\?|&)ID=([^&]+)/);
	if(!id) return;
	
	return id[1];
}

// #############################
// ##### Scraper functions #####
// #############################

function scrape(doc, id) {
	id = '|' + id + '|';
	var oldCookie = setCookie(doc, id);

	var exportUrl = 'http://s.wanfangdata.com.cn/Export/Export.aspx?scheme=EndNote';

	ZU.doGet(exportUrl, function(text) {
		var matches = text.match(/<div\s+id=["']export_container["']>((?:.|[\r\n])+?)<\/div>/i);
		if(!matches) return false;

		text = ZU.cleanTags( matches[1].replace(/[\r\n]/g,'') );

		var translator = Zotero.loadTranslator('import');
		translator.setTranslator('881f60f2-0802-411a-9228-ce5f47b64c7d');
		translator.setString(text);
		translator.setHandler('itemDone', function(obj, item) {
			//author first and last names are mixed up
			for(var i=0, n=item.creators.length; i<n; i++) {
				if(!item.creators[i].firstName) continue;
				var first = item.creators[i].lastName;
				item.creators[i].lastName = item.creators[i].firstName;
				item.creators[i].firstName = first;
			}

			//type is actually DOI
			if(item.type) {
				item.DOI = item.type;
				delete item.type;
			}

			//tags are messed up
			item.tags = [];

			item.complete();
		});

		translator.setHandler('done', function(obj, item) {
			setCookie(doc, oldCookie);
		});

		translator.translate();
	});
}

// #########################
// ##### API functions #####
// #########################

function detectWeb(doc, url) {
	if (url.toLowerCase().indexOf('paper.aspx') != -1
		&& getSearchResults(doc, true)
	) {
		return "multiple";
	}
	
	pattern = /[ds]\.(?:g\.)?wanfangdata\.com\.cn/;
	if (pattern.test(url) && getItemId(doc)) {
		var code = detectCode(url);
		return detectType(code);
	}

	return false;
}

function doWeb(doc, url) {
	if (detectWeb(doc, url) == "multiple") {
		// search page
		var items = getSearchResults(doc);

		Zotero.selectItems(items, function(selectedItems) {
			if (!selectedItems) return true;
		
			var urls = new Array();
			for(var i in selectedItems) {
				urls.push(i);
			}

			var ids = '|' + urls.join('|') + '|';
			scrape(doc, ids);
		});
	} else {
		var id = getItemId(doc);
		scrape(doc, id);
	}
}
/** BEGIN TEST CASES **/
var testCases = [
	{
		"type": "web",
		"url": "http://d.wanfangdata.com.cn/Periodical_xdqb200902027.aspx",
		"items": [
			{
				"itemType": "journalArticle",
				"title": "参考文献管理工具研究",
				"creators": [
					{
						"firstName": "",
						"lastName": "余敏",
						"creatorType": "author"
					},
					{
						"firstName": "",
						"lastName": "朱江",
						"creatorType": "author"
					},
					{
						"firstName": "",
						"lastName": "丁照蕾",
						"creatorType": "author"
					}
				],
				"date": "2009",
				"DOI": "10.3969/j.issn.1008-0821.2009.02.027",
				"abstractNote": "介绍了参考文献管理的基本方法,对参考文献管理工具的主要功能进行了对比,最后分析了参考文献管理的趋势.",
				"archiveLocation": "北京万方数据股份有限公司",
				"extra": "Yu Min\nZhu Jiang\nDing Zhaolei",
				"issue": "2",
				"libraryCatalog": "Wanfang Data",
				"pages": "94-98,93",
				"publicationTitle": "JOURNAL OF MODERN INFORMATION",
				"url": "http://d.wanfangdata.com.cn/Periodical_xdqb200902027.aspx",
				"volume": "29",
				"attachments": [],
				"tags": [],
				"notes": [],
				"seeAlso": []
			}
		]
	},
	{
		"type": "web",
		"url": "http://d.wanfangdata.com.cn/NSTLQK_NSTL_QKJJ0216348353.aspx",
		"items": [
			{
				"itemType": "journalArticle",
				"title": "Zotero: harnessing the power of a personal bibliographic manager.",
				"creators": [
					{
						"firstName": "JT",
						"lastName": "Coar",
						"creatorType": "author"
					},
					{
						"firstName": "JP",
						"lastName": "Sewell",
						"creatorType": "author"
					}
				],
				"date": "2010",
				"ISBN": "0363-3624",
				"abstractNote": "Zotero is a powerful free personal bibliographic manager (PBM) for writers. Use of a PBM allows the writer to focus on content, rather than the tedious details of formatting citations and references. Zotero 2.0 (http://www.zotero.org) has new features including the ability to synchronize citations with the off-site Zotero server and the ability to collaborate and share with others. An overview on how to use the software and discussion about the strengths and limitations are included.",
				"accessDate": "CURRENT_TIMESTAMP",
				"archiveLocation": "北京万方数据股份有限公司",
				"issue": "5",
				"libraryCatalog": "Wanfang Data",
				"pages": "205-207",
				"publicationTitle": "Nurse educator",
				"shortTitle": "Zotero",
				"url": "http://d.wanfangdata.com.cn/NSTLQK_NSTL_QKJJ0216348353.aspx",
				"volume": "35",
				"attachments": [],
				"tags": [],
				"notes": [],
				"seeAlso": []
			}
		]
	},
	{
		"type": "web",
		"url": "http://s.wanfangdata.com.cn/Paper.aspx?q=zotero+DBID%3A%28NSTL_QK+OR+NSTL_HY%29&f=d.top",
		"items": "multiple"
	}
]
/** END TEST CASES **/