Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

arXiv support #12

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,4 @@
build
build
.gitignore
.DS_Store
*.xpi
1 change: 1 addition & 0 deletions src/bootstrap.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ async function startup({ id, version, rootURI }) {
Services.scriptloader.loadSubScript(rootURI + 'chrome/content/threadpool.js');
Services.scriptloader.loadSubScript(rootURI + 'chrome/content/journal.js');
Services.scriptloader.loadSubScript(rootURI + 'chrome/content/book.js');
Services.scriptloader.loadSubScript(rootURI + 'chrome/content/arxiv.js');
Services.scriptloader.loadSubScript(rootURI + 'chrome/content/zotmeta.js');
ZotMeta.init({ id, version, rootURI });
ZotMeta.addToAllWindows();
Expand Down
87 changes: 87 additions & 0 deletions src/chrome/content/arxiv.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
Arxiv = {
generateAuthors(authors) {
var newAuthorList = [];
if (authors) {
authors.forEach(author => {
newAuthorList.push(
{
"firstName": author["given"] || "",
"lastName": author["family"] || "",
"creatorType": "author"
}
);
});
}
return newAuthorList;
},

generateDate(date) {
if (!date) {
return null;
}
return date.split("T")[0]; // 提取日期部分 (YYYY-MM-DD 格式)
},

getMetaData(item) {
var arxivID = item.getField('archiveID').split(":")[1];
if (!arxivID) {
return null; // ArXiv ID 是必需的
}

var url = `https://export.arxiv.org/api/query?id_list=${arxivID}`;
return Utilities.fetchWithTimeout(url, { method: 'GET' }, 3000)
.then(response => {
if (!response.ok) {
Utilities.publishError("Error retrieving metadata",
"Please check if the ArXiv ID is correct and if you have network access to arxiv.org.");
return null;
}
return response.text();
})
.then(data => {
try {
// 解析 ArXiv API 返回的 XML 数据
let parser = new DOMParser();
let xmlDoc = parser.parseFromString(data, "application/xml");

let title = xmlDoc.querySelector("entry > title")?.textContent.trim();
let authors = Array.from(xmlDoc.querySelectorAll("entry > author")).map(author => ({
given: author.querySelector("name").textContent.split(" ").slice(0, -1).join(" "),
family: author.querySelector("name").textContent.split(" ").slice(-1).join(" ")
}));
let published = xmlDoc.querySelector("entry > published")?.textContent.trim();
let summary = xmlDoc.querySelector("entry > summary")?.textContent.trim();
let journalRef = xmlDoc.querySelector("entry > arxiv\\:journal_ref")?.textContent.trim();
let doi = xmlDoc.querySelector("entry > arxiv\\:doi")?.textContent.trim();

return {
"Title": title || "",
"Authors": this.generateAuthors(authors),
"PublishDate": this.generateDate(published),
"Abstract": summary || "",
"JournalRef": journalRef || "",
"DOI": doi || ""
};
} catch (error) {
Utilities.publishError("Error parsing metadata", "Unable to parse metadata from ArXiv.");
return null;
}
});
},

async updateMetadata(item) {
var metaData = await this.getMetaData(item);
if (!metaData) {
return 1;
}

if (!Utilities.isEmpty(metaData["Title"])) item.setField('title', metaData["Title"]);
if (!Utilities.isEmpty(metaData["Authors"])) item.setCreators(metaData["Authors"]);
if (!Utilities.isEmpty(metaData["PublishDate"])) item.setField('date', metaData["PublishDate"]);
if (!Utilities.isEmpty(metaData["Abstract"])) item.setField('abstractNote', metaData["Abstract"]);
if (!Utilities.isEmpty(metaData["JournalRef"])) item.setField('publicationTitle', metaData["JournalRef"]);
if (!Utilities.isEmpty(metaData["DOI"])) item.setField('DOI', metaData["DOI"]);
await item.saveTx();
return 0;
}
};
1 change: 1 addition & 0 deletions src/chrome/content/overlay.xul
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
<script src="threadpool.js"/>
<script src="utilities.js"/>
<script src="journal.js"/>
<script src="arxiv.js"/>
<script src="book.js"/>
<script src="zotmeta.js"/>
</overlay>
2 changes: 2 additions & 0 deletions src/chrome/content/zotmeta.js
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,8 @@ ZotMeta = {
status = await Book.updateMetadata(item);
} else if (item.itemTypeID === Zotero.ItemTypes.getID('journalArticle')) {
status = await Journal.updateMetadata(item);
} else if (item.itemTypeID === Zotero.ItemTypes.getID('preprint') && item.repository == 'arXiv') {
status = await Arxiv.updateMetadata(item);
} else {
status = 1;
}
Expand Down