Skip to content

Instantly share code, notes, and snippets.

@robheittman
Created November 14, 2010 04:39
Show Gist options
  • Save robheittman/675915 to your computer and use it in GitHub Desktop.
Save robheittman/675915 to your computer and use it in GitHub Desktop.
Examples of consuming the IUCN Red List HTML5 API
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.4.3/jquery.min.js"></script>
<script>
function check(){
var species = $('#how_threatened_is').get(0).value;
species = species.replace(' ','-');
$.ajax({
dataType: "jsonp",
url: 'http://api.iucnredlist.org/index/species/'+species+".js",
success: function (row) {
var id = row[0].species_id
$("#summary").get(0).innerHTML = row[0].category + "...";
$.ajax({
dataType: "jsonp",
url: 'http://api.iucnredlist.org/details/'+id+"/0.js",
success: function (data) {
var blob = data[0].species_account;
$("#full_result").get(0).innerHTML = blob;
var cat = $('#red_list_category_title').get(0).innerHTML;
var code = $('#red_list_category_code').get(0).innerHTML;
var year = $('#modified_year').get(0).innerHTML;
var summary = ""+code+" - "+cat+" (updated "+year+")";
$('#full_result').hide()
$("#summary").get(0).innerHTML = summary;
$("#summary_justification").get(0).innerHTML = $('#justification').get(0).innerHTML;
$('#full_button').show()
}
});
},
failure: function () {
alert("No species found.");
$("#result").get(0).innerHTML = "---";
}
});
}
</script>
<style>
body{
padding: 5em;
}
body,div,input{
font-family: sans-serif;
font-size: large;
}
#summary{
font-size: xx-large;
font-weight: bold;
}
#show_all{
display: none;
}
#full_result, #full_button{
display: none;
}
h1,h2,.x_label{
color: #000044;
}
</style>
</head>
<body>
<label for="how_threatened_is">How Threatened Is...</label>
<input id="how_threatened_is" type="text" size="25" />
<input type="button" value="Find Out" onclick="check()"/>
<div id="summary">???</div>
<div id="summary_justification"></div>
<input id="full_button" type="button" value="Show Full Account" onclick="$('#full_result').show(); $('#full_button').hide()"/>
<div id="full_result"></div>
</body>
</html>
#!/usr/bin/perl
use strict;
use HTML::Selector::XPath::Simple;
use LWP::UserAgent;
use Text::Wrap;
my $species = $ARGV[0] || die "Species name is required";
print "\n$species\n";
$species =~ s/\s+/-/g;
my $response = LWP::UserAgent->new->request(
HTTP::Request->new( GET => "http://api.iucnredlist.org/go/$species" )
);
my $selector = HTML::Selector::XPath::Simple->new($response->content);
my $cat = $selector->select('#red_list_category_title');
my $code = $selector->select('#red_list_category_code');
my $year = $selector->select('#modified_year');
print "IUCN Red List Status (updated $year): $code - $cat\n";
print "--------------------------------------------------------------------------------\n\n";
my $justification = $selector->select('#justification');
print wrap("","",$justification);
print "\n\n";
require 'rubygems'
require 'nokogiri'
require 'open-uri'
ARGV.each do |arg|
puts '',arg
doc = Nokogiri::HTML(open "http://api.iucnredlist.org/go/#{arg.sub(' ','-')}")
cat = doc.at_css('#red_list_category_title').content
code = doc.at_css('#red_list_category_code').content
year = doc.at_css('#modified_year').content
puts "IUCN Red List Status (updated #{year}): #{code} - #{cat}\n"
80.times{print '-'}
2.times{puts}
puts doc.at_css('#justification').text
.gsub(/(.{1,80})( +|$\n?)|(.{1,80})/,"\\1\\3\n"), '' # wrap lines
end
@cboettig
Copy link

@rfc2616, others: The API documentation does not appear to cover the case of querying for JSON data by species ID. This is rather frustrating, since not all of the species names returned by the API can actually be converted into valid queries (e.g. Aplocheilichthys sp. nov. 'Naivasha'). (This is perhaps related to @Edild 's question too - did you ever find a solution?)

It would be great if there was a way to construct a query by species_id instead of taxon level, e.g. something like: http://api.iucnredlist.org/index/species_id/22823.json but clearly that doesn't work. Any ideas?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment