Improved CiteULike import

Hello,

I wanted to get personal pdfs and more links from CiteULike import. I have modified the import code so now it detects the pdf if there is one and loads all the links. Note that CiteULike already includes DOI link in its RIS record so this link is included twice. This can be fixed easily.

For some reason I also had to modify the regular expression of the CiteULike import to:
https?://(?:www\.)?citeulike.org(?:.*/tag/[^/]*$|/search/|/user/|/journal/|/group/[0-9]+/library$|/\?page=[0-9]+$|/.*article/[0-9]+$|/$)
Without the "/user/" addition it would not match my recent items on CiteULike.

Here is my modified code:

function doWeb(doc, url){
var namespace = doc.documentElement.namespaceURI;
var nsResolver = namespace ? function(prefix) {
if (prefix == 'x') return namespace; else return null;
} : null;
var ns = nsResolver;
var articleRe = /\/article\/[0-9]+$/;
var m = url.match(articleRe);
var newUris = new Array();

if (m){
newUris.push(url);
} else {
var elmt;
var elmts = doc.evaluate('//a[@class="title"]', doc, nsResolver, XPathResult.ANY_TYPE, null);
var items = new Object();
while(elmt = elmts.iterateNext()) {
items[elmt.href] = Zotero.Utilities.trimInternal(elmt.textContent);
}
items = Zotero.selectItems(items);
if(!items) return true;
for(var uri in items) {
Zotero.debug(uri);
newUris.push(uri);
}
}

Zotero.Utilities.processDocuments(newUris, function(doc) {
var citeulikelink = doc.location.href;
var linkAttchs = new Array();
var linkTitles = new Array()
var pdflink = doc.evaluate('//a[contains(@href, ".pdf")]', doc, ns, XPathResult.ANY_TYPE, null).iterateNext();
if (doc.evaluate('//a[contains(@onClick,"/linkout/")]', doc, ns, XPathResult.ANY_TYPE, null).iterateNext()) {
var links = doc.evaluate('//a[contains(@onClick,"/linkout/")]', doc, ns, XPathResult.ANY_TYPE, null);
var link;
while(link = links.iterateNext()) {
linkAttchs.push(link.href);
linkTitles.push(link.text);
}
}
Zotero.Utilities.HTTP.doGet(citeulikelink.replace(/citeulike\.org\//, "citeulike.org/endnote/"), function(text) {
var trans = Zotero.loadTranslator("import");
trans.setTranslator("32d59d2d-b65a-4da4-b0a3-bdd3cfb979e7");
trans.setString(text);
trans.setHandler("itemDone", function(obj, item) {
if(pdflink) { item.attachments.push({url:pdflink.href, title:"Personal PDF (CiteULike)", mimeType:"application/pdf"}); }
var link;
while(link = linkAttchs.pop()) {
var title = linkTitles.pop();
item.attachments.push({url:link, title:title + " (CiteULike)", mimeType:"text/html"});
}
item.complete();
});
trans.translate();
});
}, function() {Zotero.done;});
Zotero.wait();
}
Sign In or Register to comment.