-
Notifications
You must be signed in to change notification settings - Fork 527
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
2f41eb1
commit 4b6ceac
Showing
3 changed files
with
166 additions
and
166 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,162 +1,162 @@ | ||
{ | ||
"translatorID": "d611008a-850d-4860-b607-54e1ecbcc592", | ||
"label": "E-Tiller", | ||
"creator": "jiaojiaodubai23", | ||
"target": "^https?://.*(/ch/)?.*(.aspx)?", | ||
"minVersion": "5.0", | ||
"maxVersion": "", | ||
"priority": 100, | ||
"inRepository": true, | ||
"translatorType": 4, | ||
"browserSupport": "gcsibv", | ||
"lastUpdated": "2023-12-16 19:56:45" | ||
} | ||
|
||
/* | ||
***** BEGIN LICENSE BLOCK ***** | ||
Copyright © 2022 [email protected] | ||
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 ***** | ||
*/ | ||
|
||
const paths = [ | ||
'reader/view_abstract.aspx?file_no=', | ||
'/article/abstract/' | ||
]; | ||
|
||
function detectWeb(doc, url) { | ||
Z.debug('---------- E-Tiller ----------'); | ||
let insite = Array.from(doc.querySelectorAll(`div[class*="foot"], div[id*="foot"]`)) | ||
.some(foot => (/北京勤云科技/.test(foot.textContent))); | ||
Z.debug(`incite: ${insite}`); | ||
if (!insite) return false; | ||
for (let path of paths) { | ||
if (url.includes(path) && doc.querySelector('meta[name="citation_title"]')) { | ||
Z.debug(`match path: ${path}`); | ||
return 'journalArticle'; | ||
} | ||
else if (doc.querySelector('meta[name]') && getSearchResults(doc, true)) { | ||
Z.debug(`match path: ${path}`); | ||
return 'multiple'; | ||
} | ||
} | ||
return false; | ||
} | ||
|
||
function getSearchResults(doc, checkOnly) { | ||
var items = {}; | ||
var found = false; | ||
var rows = Array.from(doc.querySelectorAll(paths.map(path => `a[href*="${path}"]`).join(','))) | ||
.filter(element => !(/^[[【〔]?\s*摘要/.test(ZU.trimInternal(element.textContent)))); | ||
for (let row of rows) { | ||
let href = row.href; | ||
let title = ZU.trimInternal(row.textContent); | ||
if (!href || !title) continue; | ||
if (checkOnly) return true; | ||
found = true; | ||
items[href] = title; | ||
} | ||
return found ? items : false; | ||
} | ||
|
||
async function doWeb(doc, url) { | ||
if (detectWeb(doc, url) == 'multiple') { | ||
let items = await Zotero.selectItems(getSearchResults(doc, false)); | ||
Z.debug('selected items:'); | ||
Z.debug(items); | ||
if (!items) return; | ||
for (let url of Object.keys(items)) { | ||
await scrape(await requestDocument(url)); | ||
} | ||
} | ||
else { | ||
await scrape(doc, url); | ||
} | ||
} | ||
|
||
async function scrape(doc, url = doc.location.href) { | ||
let translator = Zotero.loadTranslator('web'); | ||
// Embedded Metadata | ||
translator.setTranslator('951c027d-74ac-47d4-a107-9c3069ab7b48'); | ||
translator.setDocument(doc); | ||
translator.setHandler('itemDone', (_obj, item) => { | ||
item.extra = ''; | ||
if (item.title.includes('(网络首发、推荐阅读)')) { | ||
item.extra += addExtra('available-date', item.date); | ||
delete item.date; | ||
item.extra += addExtra('Status', 'advance online publication'); | ||
item.title = item.title.replace(/(网络首发、推荐阅读)$/, ''); | ||
} | ||
item.language = { | ||
cn: 'zh-CN', | ||
en: 'en-US' | ||
}[item.language]; | ||
item.extra += addExtra('original-title', attr(doc, 'meta[name="citation_title"]', 'content', 1)); | ||
let creators = doc.querySelector('meta[name="citation_author"]') | ||
? Array.from(doc.querySelectorAll('meta[name="citation_author"]')).map(element => element.content) | ||
: attr(doc, 'meta[name="citation_authors"]', 'content', 0).split(/[,;,;]/); | ||
if (creators.length) { | ||
Z.debug(creators); | ||
item.creators = creators | ||
.map(creator => creator.replace(/(<.+>)?[\d,\s]+(<.+>)?$/, '')) | ||
.filter(creator => creator) | ||
.map(creator => ZU.cleanAuthor(creator, 'author')); | ||
} | ||
item.creators.forEach((creator) => { | ||
if (/[\u4e00-\u9fa5]/.test(creator.lastName)) { | ||
creator.lastName = creator.firstName + creator.lastName; | ||
creator.firstName = ''; | ||
creator.fieldMode = 1; | ||
} | ||
}); | ||
let enCreators = attr(doc, 'meta[name="citation_authors"]', 'content', 1).split(/[,;,;]/); | ||
let creatorsExt = JSON.parse(JSON.stringify(item.creators)); | ||
for (let i = 0; i < item.creators.length; i++) { | ||
creatorsExt[i].original = enCreators[i]; | ||
} | ||
if (item.tags.length == 1) { | ||
item.tags = item.tags[0].split(/[;;]\s*/).map(tag => ({ tag: tag })); | ||
} | ||
item.extra += addExtra('creatorsExt', JSON.stringify(creatorsExt)); | ||
let pdfLink = attr(doc, 'a[href*="create_pdf"]', 'href'); | ||
if (pdfLink && !item.attachments.some(attachment => attachment.mimeType == 'application/pdf')) { | ||
item.attachments.push({ | ||
url: pdfLink, | ||
title: 'Full Text PDF', | ||
mimeType: 'application/pdf' | ||
}); | ||
} | ||
item.complete(); | ||
}); | ||
|
||
let em = await translator.getTranslatorObject(); | ||
em.itemType = 'journalArticle'; | ||
await em.doWeb(doc, url); | ||
} | ||
|
||
function addExtra(key, value) { | ||
return value | ||
? `${key}: ${value}\n` | ||
: ''; | ||
} | ||
|
||
/** BEGIN TEST CASES **/ | ||
{ | ||
"translatorID": "d611008a-850d-4860-b607-54e1ecbcc592", | ||
"label": "E-Tiller", | ||
"creator": "jiaojiaodubai23", | ||
"target": "^https?://.*(/ch/)?.*(.aspx)?", | ||
"minVersion": "5.0", | ||
"maxVersion": "", | ||
"priority": 100, | ||
"inRepository": true, | ||
"translatorType": 4, | ||
"browserSupport": "gcsibv", | ||
"lastUpdated": "2023-12-16 19:56:45" | ||
} | ||
|
||
/* | ||
***** BEGIN LICENSE BLOCK ***** | ||
Copyright © 2022 [email protected] | ||
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 ***** | ||
*/ | ||
|
||
const paths = [ | ||
'reader/view_abstract.aspx?file_no=', | ||
'/article/abstract/' | ||
]; | ||
|
||
function detectWeb(doc, url) { | ||
Z.debug('---------- E-Tiller ----------'); | ||
let insite = Array.from(doc.querySelectorAll(`div[class*="foot"], div[id*="foot"]`)) | ||
.some(foot => (/北京勤云科技/.test(foot.textContent))); | ||
Z.debug(`incite: ${insite}`); | ||
if (!insite) return false; | ||
for (let path of paths) { | ||
if (url.includes(path) && doc.querySelector('meta[name="citation_title"]')) { | ||
Z.debug(`match path: ${path}`); | ||
return 'journalArticle'; | ||
} | ||
else if (doc.querySelector('meta[name]') && getSearchResults(doc, true)) { | ||
Z.debug(`match path: ${path}`); | ||
return 'multiple'; | ||
} | ||
} | ||
return false; | ||
} | ||
|
||
function getSearchResults(doc, checkOnly) { | ||
var items = {}; | ||
var found = false; | ||
var rows = Array.from(doc.querySelectorAll(paths.map(path => `a[href*="${path}"]`).join(','))) | ||
.filter(element => !(/^[[【〔]?\s*摘要/.test(ZU.trimInternal(element.textContent)))); | ||
for (let row of rows) { | ||
let href = row.href; | ||
let title = ZU.trimInternal(row.textContent); | ||
if (!href || !title) continue; | ||
if (checkOnly) return true; | ||
found = true; | ||
items[href] = title; | ||
} | ||
return found ? items : false; | ||
} | ||
|
||
async function doWeb(doc, url) { | ||
if (detectWeb(doc, url) == 'multiple') { | ||
let items = await Zotero.selectItems(getSearchResults(doc, false)); | ||
Z.debug('selected items:'); | ||
Z.debug(items); | ||
if (!items) return; | ||
for (let url of Object.keys(items)) { | ||
await scrape(await requestDocument(url)); | ||
} | ||
} | ||
else { | ||
await scrape(doc, url); | ||
} | ||
} | ||
|
||
async function scrape(doc, url = doc.location.href) { | ||
let translator = Zotero.loadTranslator('web'); | ||
// Embedded Metadata | ||
translator.setTranslator('951c027d-74ac-47d4-a107-9c3069ab7b48'); | ||
translator.setDocument(doc); | ||
translator.setHandler('itemDone', (_obj, item) => { | ||
item.extra = ''; | ||
if (item.title.includes('(网络首发、推荐阅读)')) { | ||
item.extra += addExtra('available-date', item.date); | ||
delete item.date; | ||
item.extra += addExtra('Status', 'advance online publication'); | ||
item.title = item.title.replace(/(网络首发、推荐阅读)$/, ''); | ||
} | ||
item.language = { | ||
cn: 'zh-CN', | ||
en: 'en-US' | ||
}[item.language]; | ||
item.extra += addExtra('original-title', attr(doc, 'meta[name="citation_title"]', 'content', 1)); | ||
let creators = doc.querySelector('meta[name="citation_author"]') | ||
? Array.from(doc.querySelectorAll('meta[name="citation_author"]')).map(element => element.content) | ||
: attr(doc, 'meta[name="citation_authors"]', 'content', 0).split(/[,;,;]/); | ||
if (creators.length) { | ||
Z.debug(creators); | ||
item.creators = creators | ||
.map(creator => creator.replace(/(<.+>)?[\d,\s]+(<.+>)?$/, '')) | ||
.filter(creator => creator) | ||
.map(creator => ZU.cleanAuthor(creator, 'author')); | ||
} | ||
item.creators.forEach((creator) => { | ||
if (/[\u4e00-\u9fa5]/.test(creator.lastName)) { | ||
creator.lastName = creator.firstName + creator.lastName; | ||
creator.firstName = ''; | ||
creator.fieldMode = 1; | ||
} | ||
}); | ||
let enCreators = attr(doc, 'meta[name="citation_authors"]', 'content', 1).split(/[,;,;]/); | ||
let creatorsExt = JSON.parse(JSON.stringify(item.creators)); | ||
for (let i = 0; i < item.creators.length; i++) { | ||
creatorsExt[i].original = enCreators[i]; | ||
} | ||
if (item.tags.length == 1) { | ||
item.tags = item.tags[0].split(/[;;]\s*/).map(tag => ({ tag: tag })); | ||
} | ||
item.extra += addExtra('creatorsExt', JSON.stringify(creatorsExt)); | ||
let pdfLink = doc.querySelector('a[href*="create_pdf"]'); | ||
if (pdfLink && !item.attachments.some(attachment => attachment.mimeType == 'application/pdf')) { | ||
item.attachments.push({ | ||
url: pdfLink.href, | ||
title: 'Full Text PDF', | ||
mimeType: 'application/pdf' | ||
}); | ||
} | ||
item.complete(); | ||
}); | ||
|
||
let em = await translator.getTranslatorObject(); | ||
em.itemType = 'journalArticle'; | ||
await em.doWeb(doc, url); | ||
} | ||
|
||
function addExtra(key, value) { | ||
return value | ||
? `${key}: ${value}\n` | ||
: ''; | ||
} | ||
|
||
/** BEGIN TEST CASES **/ | ||
var testCases = [ | ||
{ | ||
"type": "web", | ||
|
@@ -349,5 +349,5 @@ var testCases = [ | |
"url": "http://wltb.ijournal.cn/ch/index.aspx", | ||
"items": "multiple" | ||
} | ||
] | ||
/** END TEST CASES **/ | ||
] | ||
/** END TEST CASES **/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters