Skip to content

Instantly share code, notes, and snippets.

@egh

egh/gist:4580965

Created Jan 20, 2013
Embed
What would you like to do?
{
"translatorID": "9fa7d89b-d5ad-4c68-a834-f96fbc8376fa",
"translatorType": 4,
"label": "Chista",
"creator": "CRCIS",
"target": "http://journals\\.ut\\.ac\\.ir/page/main-page.html",
"minVersion": "1.0",
"maxVersion": "",
"priority": 500,
"inRepository": true,
"browserSupport": "g",
"lastUpdated": "2012-08-29 10:49:53"
}
function flatten(a){var d=[],c;for(c in a){var b=a[c];b instanceof Array?d=d.concat(flatten(b)):d.push(b)}return d}
var FW={_scrapers:[],_Base:function(){this.callHook=function(a,d,c,b){"object"===typeof this.hooks&&(a=this.hooks[a],"function"===typeof a&&a(d,c,b))};this.evaluateThing=function(a,d,c){var b=typeof a;if("object"===b){if(a instanceof Array){var e=this.evaluateThing;a=a.map(function(a){return e(a,d,c)});return flatten(a)}return a.evaluate(d,c)}return"function"===b?a(d,c):a};this.makeItems=function(a,d,c,b,e){e()}},Scraper:function(a){FW._scrapers.push(new FW._Scraper(a))},_Scraper:function(a){for(x in a)this[x]=
a[x];this._singleFieldNames="abstractNote applicationNumber archive archiveLocation artworkMedium artworkSize assignee audioFileType audioRecordingType billNumber blogTitle bookTitle callNumber caseName code codeNumber codePages codeVolume committee company conferenceName country court date dateDecided dateEnacted dictionaryTitle distributor docketNumber documentNumber DOI edition encyclopediaTitle episodeNumber extra filingDate firstPage forumTitle genre history institution interviewMedium ISBN ISSN issue issueDate issuingAuthority journalAbbreviation label language legalStatus legislativeBody letterType libraryCatalog manuscriptType mapType medium meetingName nameOfAct network number numberOfVolumes numPages pages patentNumber place postType presentationType priorityNumbers proceedingsTitle programTitle programmingLanguage publicLawNumber publicationTitle publisher references reportNumber reportType reporter reporterVolume rights runningTime scale section series seriesNumber seriesText seriesTitle session shortTitle studio subject system thesisType title type university url version videoRecordingType volume websiteTitle websiteType".split(" ");
this._makeAttachments=function(d,a,b,e){if(b instanceof Array)b.forEach(function(b){this._makeAttachments(d,a,b,e)},this);else if("object"===typeof b){var g=b.types||b.type,f=b.titles||b.title,h=b.snapshots||b.snapshot;b=this.evaluateThing(b.urls||b.url,d,a);f=this.evaluateThing(f,d,a);g=this.evaluateThing(g,d,a);h=this.evaluateThing(h,d,a);b instanceof Array||(b=[b]);for(var j in b)e.attachments.push({url:b[j],title:f instanceof Array?f[j]:f,type:g instanceof Array?g[j]:g,snapshot:h instanceof Array?
h[j]:h})}};this.makeItems=function(a,c,b,e,g){b=new Zotero.Item(this.itemType);b.url=c;for(var f in this._singleFieldNames){var h=this._singleFieldNames[f];if(this[h]){var j=this.evaluateThing(this[h],a,c);b[h]=j instanceof Array?j[0]:j}}f=["creators","tags"];for(var l in f)if(h=f[l],j=this.evaluateThing(this[h],a,c))for(var k in j)b[h].push(j[k]);this._makeAttachments(a,c,this.attachments,b);e(b,this,a,c);g()}}};FW._Scraper.prototype=new FW._Base;FW.MultiScraper=function(a){FW._scrapers.push(new FW._MultiScraper(a))};
FW._MultiScraper=function(a){for(x in a)this[x]=a[x];this._mkSelectItems=function(a,c){var b={},e;for(e in a)b[c[e]]=a[e];return b};this._selectItems=function(a,c,b){var e=[];Zotero.selectItems(this._mkSelectItems(a,c),function(a){for(var d in a)e.push(d);b(e)})};this._mkAttachments=function(a,c,b){a=this.evaluateThing(this.attachments,a,c);c={};if(a)for(var e in b)c[b[e]]=a[e];return c};this._makeChoices=function(a,c,b,e,g){if(a instanceof Array)a.forEach(function(a){this._makeTitlesUrls(a,c,b,e,
g)},this);else if("object"===typeof a){var f=a.titles||a.title;a=this.evaluateThing(a.urls||a.url,c,b);var f=this.evaluateThing(f,c,b),h=f instanceof Array;a instanceof Array||(a=[a]);for(var j in a){var l=a[j],k;k=h?f[j]:f;g.push(l);e.push(k)}}};this.makeItems=function(a,c,b,e,g){if(this.beforeFilter){var f=this.beforeFilter(a,c);if(f!=c){this.makeItems(a,f,b,e,g);return}}b=[];f=[];this._makeChoices(this.choices,a,c,b,f);var h=this._mkAttachments(a,c,f),j=this.itemTrans;this._selectItems(b,f,function(a){a?
Zotero.Utilities.processDocuments(a,function(a){var b=a.documentURI,c=j;void 0===c&&(c=FW.getScraper(a,b));void 0!==c&&c.makeItems(a,b,h[b],e,function(){})},g):g()})}};FW._MultiScraper.prototype=new FW._Base;FW.WebDelegateTranslator=function(a){return new FW._WebDelegateTranslator(a)};
FW._WebDelegateTranslator=function(a){for(x in a)this[x]=a[x];this.makeItems=function(a,c,b,e,g){var f=this;b=Zotero.loadTranslator("web");b.setTranslator(this.translatorId);b.setHandler("itemDone",function(b,g){e(g,f,a,c)});b.setDocument(a);b.translate();g()}};FW._WebDelegateTranslator.prototype=new FW._Base;
FW._StringMagic=function(){this._filters=[];this.addFilter=function(a){this._filters.push(a);return this};this.split=function(a){return this.addFilter(function(d){return d.split(a).filter(function(a){return""!=a})})};this.replace=function(a,d,c){return this.addFilter(function(b){return b.match(a)?b.replace(a,d,c):b})};this.prepend=function(a){return this.replace(/^/,a)};this.append=function(a){return this.replace(/$/,a)};this.remove=function(a,d){return this.replace(a,"",d)};this.trim=function(){return this.addFilter(function(a){return Zotero.Utilities.trim(a)})};
this.trimInternal=function(){return this.addFilter(function(a){return Zotero.Utilities.trimInternal(a)})};this.match=function(a,d){d||(d=0);return this.addFilter(function(c){c=c.match(a);return void 0===c||null===c?void 0:c[d]})};this.cleanAuthor=function(a,d){return this.addFilter(function(c){return Zotero.Utilities.cleanAuthor(c,a,d)})};this.key=function(a){return this.addFilter(function(d){return d[a]})};this.capitalizeTitle=function(){return this.addFilter(function(a){return Zotero.Utilities.capitalizeTitle(a)})};
this.unescapeHTML=function(){return this.addFilter(function(a){return Zotero.Utilities.unescapeHTML(a)})};this.unescape=function(){return this.addFilter(function(a){return unescape(a)})};this._applyFilters=function(a,d){for(i in this._filters){a=flatten(a);a=a.filter(function(a){return void 0!==a&&null!==a});for(var c=0;c<a.length;c++)try{void 0===a[c]||null===a[c]||(a[c]=this._filters[i](a[c],d))}catch(b){a[c]=void 0,Zotero.debug("Caught exception "+b+"on filter: "+this._filters[i])}a=a.filter(function(a){return void 0!==
a&&null!==a})}return flatten(a)}};FW.PageText=function(){return new FW._PageText};FW._PageText=function(){this._filters=[];this.evaluate=function(a){var d=[a.documentElement.innerHTML],d=this._applyFilters(d,a);return 0==d.length?!1:d}};FW._PageText.prototype=new FW._StringMagic;FW.Url=function(){return new FW._Url};FW._Url=function(){this._filters=[];this.evaluate=function(a,d){var c=[d],c=this._applyFilters(c,a);return 0==c.length?!1:c}};FW._Url.prototype=new FW._StringMagic;FW.Xpath=function(a){return new FW._Xpath(a)};
FW._Xpath=function(a){this._xpath=a;this._filters=[];this.text=function(){this.addFilter(function(a){return"object"===typeof a&&a.textContent?a.textContent:a});return this};this.sub=function(a){this.addFilter(function(c,b){var e=b.evaluate(a,c,null,XPathResult.ANY_TYPE,null);if(e)return e.iterateNext()});return this};this.evaluate=function(a){var c=a.evaluate(this._xpath,a,null,XPathResult.ANY_TYPE,null),b=c.resultType,e=[];if(b==XPathResult.STRING_TYPE)e.push(c.stringValue);else if(b==XPathResult.BOOLEAN_TYPE)e.push(c.booleanValue);
else if(b==XPathResult.NUMBER_TYPE)e.push(c.numberValue);else if(b==XPathResult.ORDERED_NODE_ITERATOR_TYPE||b==XPathResult.UNORDERED_NODE_ITERATOR_TYPE)for(;b=c.iterateNext();)e.push(b);e=this._applyFilters(e,a);return 0==e.length?!1:e}};FW._Xpath.prototype=new FW._StringMagic;FW.detectWeb=function(a,d){for(var c in FW._scrapers){var b=FW._scrapers[c],e=b.evaluateThing(b.itemType,a,d),b=b.evaluateThing(b.detect,a,d);if(0<b.length&&b[0])return e}};
FW.getScraper=function(a,d){var c=FW.detectWeb(a,d);return FW._scrapers.filter(function(b){return b.evaluateThing(b.itemType,a,d)==c&&b.evaluateThing(b.detect,a,d)})[0]};FW.doWeb=function(a,d){FW.getScraper(a,d).makeItems(a,d,[],function(a,b,d,g){b.callHook("scraperDone",a,d,g);a.title||(a.title="");a.complete()},function(){Zotero.done()});Zotero.wait()};
/*Created By A. R. Salarmehr in CRSIS*/
/*Version 91-5-10 0.2 */
function detectWeb(doc, url) {
return FW.detectWeb(doc, url);
}
function doWeb(doc, url) {
return FW.doWeb(doc, url);
}
FW.MultiScraper({
itemType : 'multiple',
detect : FW.Xpath("boolean(//a[contains(@href,'page/article-frame.html?')]) and boolean(//meta[contains(@content,'Chista')])"),
itemTrans : FW.WebDelegateTranslator({
translatorId : "951c027d-74ac-47d4-a107-9c3069ab7b48"
}),
choices : {
titles : FW.Xpath("//a[contains(@href,'page/article-frame.html?')]").text().trim(),
urls : FW.Xpath("//a[contains(@href,'page/article-frame.html?')]").key("href")
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment