Last active
December 13, 2015 18:08
-
-
Save briantjacobs/4953063 to your computer and use it in GitHub Desktop.
Node.io triple query to the Encyclopedia of Life for taxonomy entries based on species names
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
var nodeio = require('node.io'), options = {timeout: 10}; | |
exports.job = new nodeio.Job(options, { | |
input: 'data/haeckel-plates.csv', | |
run: function(input) { | |
var self = this; | |
if (!input.type) { | |
var values = this.parseValues(input); | |
var keyword = values[2]; // hardcoded csv column | |
this.get('http://eol.org/api/search/1.0.json?q='+encodeURIComponent(keyword)+'&page=1&exact=true', function(err, data, headers) { | |
console.log(keyword, err, count++); | |
var json = JSON.parse(data); | |
if (json.results[0] === undefined) { | |
var output = JSON.stringify({name: keyword, errorType: 'noPage'})+','; | |
this.emit(output); | |
} else { | |
self.add({ type: 'pageRequest', id: json.results[0].id}); | |
this.skip(); | |
} | |
}); | |
} | |
else if (input.type == 'pageRequest') { | |
this.get('http://eol.org/api/pages/1.0/'+input.id+'.json?images=2&videos=0&sounds=0&maps=0&text=2&iucn=false&subjects=overview&licenses=all&details=false&common_names=true&references=false&vetted=1', function(err, data, headers) { | |
var json = JSON.parse(data); | |
var taxonMember; | |
if (json.taxonConcepts[0] === undefined) { | |
var output = JSON.stringify({name: input.id, errorType: 'noTaxa'})+','; | |
this.emit(output); | |
} else { | |
json.taxonConcepts.forEach(function(item){ | |
if (item.nameAccordingTo == 'Integrated Taxonomic Information System (ITIS)') { | |
self.add({ type: 'taxonRequest', id: item.identifier}); | |
taxonMember = 'yes'; | |
} | |
}); | |
if (taxonMember == 'yes') { | |
this.skip(); | |
} else { | |
var newOutput = JSON.stringify({name: input.id, errorType: 'noTaxa-ITIS'})+','; | |
self.emit(newOutput); | |
} | |
} | |
}); | |
} | |
else if (input.type == 'taxonRequest') { | |
this.get('http://eol.org/api/hierarchy_entries/1.0/'+input.id+'.json?common_names=false&synonyms=false', function(err, data, headers) { | |
//var json = JSON.parse(data); | |
var output = data+','; //add trailing comma for json | |
this.emit(output); | |
}); | |
} | |
}, | |
output: 'output.json' | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment