Skip to content
This repository has been archived by the owner on Apr 8, 2020. It is now read-only.

Commit

Permalink
get count by species, need to fix callback problem
Browse files Browse the repository at this point in the history
  • Loading branch information
Mingfei Yin authored and Mingfei Yin committed Mar 17, 2018
1 parent cb54a86 commit cde7a19
Show file tree
Hide file tree
Showing 2 changed files with 88 additions and 15 deletions.
98 changes: 86 additions & 12 deletions getJson/api.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,78 @@
$(document).ready(function(){
/*$(document).ready(function(){
$("button").click(function(){
var readers =[];
var tag_reader =[];
var tags = [];
var reader_counts={};
$.getJSON("https://head.ouetag.org/api/etag/readers/", function(t){
$.each(t.results, function(m, get){
readers.push(get.reader_id);
});
$("div").append("reader number: " + readers.length + '</br> ');
$.getJSON("https://head.ouetag.org/api/etag/tag_reads/?search&reader="+get.reader_id, function(y){
$("div").append("reader id: "+get.reader_id +' '+ " counts: "+ y.count +"<br>");
});
//$("div").append("reader number: " + readers.length + '</br> ');
for (var i =0; i< readers.length; i++){
$("div").append("reader ID: " + readers[i]+ '</br>');
//$("div").append("reader ID: " + readers[i]+ '</br>');
};
});
});
});*/

$(document).ready(function(){
$("button").click(function(){
var bird_species = [];
var species;
var species_counts = [];
//var count={};
$.getJSON("https://head.ouetag.org/api/etag/reader_location/", function(result){
$.each(result.results, function(i,l){
var reader = l.reader;
var lat = l.latitude;
var lon = l.longitude;
var location = [lat, lon];
var active = l.active;
$("div").append("locations: "+location+ '<br>');
if (active == true){
$.getJSON("https://head.ouetag.org/api/etag/tag_reads/?search&reader="+reader, function(get){
var count = get.count;
var time = get.tag_timestamp;
$("div").append("reader id: "+reader +' '+ " counts: "+ count + " locations: " +lat +", "+ lon+ '<br>');
$.each(get.results, function(t,bird){
var tag = bird.tag;
$.getJSON("https://head.ouetag.org/api/etag/tags/"+tag, function(l){
species = l.name;
//$("div").append(species+'<br>');
});
bird_species.push(species);
});
//$("div").append(bird_species+'<br>');
var birds = bird_species;
for(var s=0; s < birds.length; s++){
if (species_counts[birds[s]]){
species_counts[birds[s]]+=1;
}
else{
species_counts[birds[s]]=1;
}
};
for (s in species_counts){
if(s!="undefined"){ // undefined is caused by getJSON callback, species was called before getJSON finishes running
$("div").append("species: " +s+ ", counts: "+ species_counts[s]+'</br>');
}
};

});
}
});
});

});

});

//for (i in count)$("div").append('species: '+i + ' count: '+count[i]+'</br>');

$.getJSON("https://head.ouetag.org/api/etag/tag_reads/", function(r){
$("div").append('Total reads count: ' + r.count+'</br>');
/*$.getJSON("https://head.ouetag.org/api/etag/tag_reads/?page=5", function(r){
//$("div").append('Total reads count: ' + r.count+'</br>');
$.each(r.results, function(j, get){
tag_reader.push(get.reader);
tags.push(get.tag);
Expand All @@ -28,10 +86,26 @@ $(document).ready(function(){
count[tag_reader[i]]=1;
}
}
for (i in count)$("div").append('reader: '+i + ' count: '+count[i]+'</br>');
//for (i in count)$("div").append('reader: '+i + ' count: '+count[i]+'</br>');
});*/

/*$.getJSON("https://head.ouetag.org/api/etag/tag_reads/", function(l){ // read the url and then loop it until hit the last page
$.each(l.results, function(j, get){
if (isNaN(reader_counts[get.reader])){
reader_counts[get.reader]=1
} else{
reader_counts[get.reader]+=1
}
});
for (i in reader_counts){
if (i == "12007"){
$("div").append("reader: "+i+ " counts: "+reader_counts[i]);
};
};
}); */



});
});

});

5 changes: 2 additions & 3 deletions getJson/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,8 @@

<h2>Get JSON data from API</h2>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script src="api.js">

</script>
<script src="api.js"></script>
<script src="https://head.ouetag.org/api/etag/tag_reads/?format=jsonp"></script>
</head>
<body>

Expand Down

0 comments on commit cde7a19

Please sign in to comment.