lib: Create base library fork with axios

This commit is contained in:
Samuel Ortion 2022-08-27 12:09:26 +02:00
parent 9c95f6c220
commit 223632442b
12 changed files with 3480 additions and 7392 deletions

2
.gitignore vendored
View File

@ -1 +1,3 @@
node_modules
lib
.env

View File

@ -1,14 +0,0 @@
{
"curly": true,
"eqeqeq": true,
"immed": true,
"latedef": "nofunc",
"newcap": true,
"noarg": true,
"sub": true,
"undef": true,
"unused": true,
"boss": true,
"eqnull": true,
"node": true
}

2
.npmignore Normal file
View File

@ -0,0 +1,2 @@
src
test

View File

@ -1,48 +0,0 @@
'use strict';
module.exports = function(grunt) {
// Project configuration.
grunt.initConfig({
nodeunit: {
files: ['test/**/*_test.js'],
},
jshint: {
options: {
jshintrc: '.jshintrc'
},
gruntfile: {
src: 'Gruntfile.js'
},
lib: {
src: ['lib/**/*.js']
},
test: {
src: ['test/**/*.js']
},
},
watch: {
gruntfile: {
files: '<%= jshint.gruntfile.src %>',
tasks: ['jshint:gruntfile']
},
lib: {
files: '<%= jshint.lib.src %>',
tasks: ['jshint:lib', 'nodeunit']
},
test: {
files: '<%= jshint.test.src %>',
tasks: ['jshint:test', 'nodeunit']
},
},
});
// These plugins provide necessary tasks.
grunt.loadNpmTasks('grunt-contrib-nodeunit');
grunt.loadNpmTasks('grunt-contrib-jshint');
grunt.loadNpmTasks('grunt-contrib-watch');
// Default task.
grunt.registerTask('default', ['jshint', 'nodeunit']);
};

View File

@ -2,6 +2,7 @@ Xeno-canto
A node.js implementation of Xeno-canto API 2.0
Copyright (c) 2013 Patrick De Marta
Copyright (c) 2022 Samuel ORTION
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by

View File

@ -19,7 +19,7 @@ listings relevant to the species named in the request."
## Installation
```
$ npm install xeno-canto
$ npm install @unclesamulus/xeno-canto.js
```
## Usage
```javascript

View File

@ -1,116 +0,0 @@
/*
* xeno-canto
*
*
* Copyright (c) 2013 Patrick De Marta
* Licensed under the GNU GPL license.
*/
'use strict';
/** @dependencies */
var request = require('request');
/** @constructor */
function XenoCanto(){
this.entity = {};
};
/** Search for name of advanced options*/
XenoCanto.prototype.search = function(query, success){
if (!!query) {
var url = 'http://www.xeno-canto.org/api/2/recordings?query=';
// Duck-typing args
if (typeof(query) === 'string') {
// Normal query search name in English, Latin, Family latin
url = url + query;
} else if (typeof(query) === 'object') {
/*
Advanced search parameters:
---------------------------
Genus gen:zonotrichia
Recordist rec:John
Country cnt:brazil.
Location loc:tambopata.
Recordist Remarks rmk:playback
Geographic Coordinates
lat, lon
box:LAT_MIN,LON_MIN,LAT_MAX,LON_MAX
Background Species also:formicariidae
Sound type type:song
Catalog number nr:76967 or range: nr:88888-88890
Recording Quality
q:A will return recordings with a quality rating of A.
q<:C will return recordings with a quality rating of D or E.
World area area:africa (, america, asia, australia, europe)
*/
var getCoords = function() {
if (!!query.coords) {
if (!!query.coords.lat && !!query.coords.lon) {
return 'lat:' + query.coords.lat + 'lon:' + query.coords.lon
} else if (!!query.coords.box) {
/** TODO box coordinates*/
};
};
};
var params_string = '';
var params = [
{ name:"" },
{ genus: 'gen:' },
{ recordist: 'rec:' },
{ country: 'cnt:' },
{ location: 'loc:' },
{ remarks: 'rmk:' },
{ coords: getCoords() },
{ also:'also:' },
{ type: 'type:' },
{ nr: 'nr:' },
{ quality: 'q:' },
{ qualitylt: 'q<:' },
{ area: 'area:' }
];
params.map(function(p){
var key = Object.keys(p)[0];
if (!!query[key]) {
params_string = params_string + p[key] + query[key] + " ";
};
});
url = url + params_string;
};
// Make the request
get(this, url, success);
} else {
throw("Null Query Error.")
}
};
function get(that, url, success){
var opts = {
url: url,
method: 'GET',
timeout: (10*1000),
headers:
{
'User-Agent': 'Mozilla/5.0 (Windows NT 6.2; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/61.0.3163 Safari/537.36'
}
};
request(opts, function (err, res, url) {
// store response in instance var
that.entity = JSON.parse(res.body);
success(that);
});
}
module.exports = XenoCanto;

10509
package-lock.json generated

File diff suppressed because it is too large Load Diff

View File

@ -1,47 +1,32 @@
{
"name": "xeno-canto",
"description": "Node.js API for xeno-canto database webservices",
"version": "0.0.5",
"homepage": "https://github.com/UncleSamulus/xeno-canto",
"name": "@unclesamulus/xeno-canto-api",
"version": "0.0.0",
"description": "Damn small node.js library to deal with v2 Xeno-Canto API",
"main": "lib/index.js",
"directories": {
"lib": "lib",
"test": "test"
},
"scripts": {
"test": "jest",
"build": "babel src -d lib"
},
"repository": {
"type": "git",
"url": "https://github.com/tripitakit/xeno-canto"
"url": "https://forge.chapril.org/UncleSamulus/xeno-canto.js"
},
"bugs": {
"url": "https://github.com/tripitakit/xeno-canto/issues"
},
"licenses": [
{
"type": "GNU GPL",
"url": "http://www.gnu.org/licenses/"
}
"keywords": [
"Xeno-Canto",
"birdsongs",
"API"
],
"main": "lib/xeno-canto",
"scripts": {
"test": "grunt nodeunit"
"author": "Samuel ORTION <samuel@ortion.fr>",
"license": "GPL-3.0-or-later",
"devDependencies": {
"@babel/cli": "^7.18.10",
"jest": "^29.0.1"
},
"dependencies": {
"request": "^2.88.2"
},
"devDependencies": {
"grunt": "^1.5.3",
"grunt-contrib-jshint": "^3.2.0",
"grunt-contrib-nodeunit": "^4.0.0",
"grunt-contrib-watch": "^1.1.0"
},
"directories": [
"lib",
"test"
],
"keywords": [
"bird",
"sound",
"database",
"webservice",
"api",
"science",
"biology",
"zoology",
"bioinformatics"
]
"axios": "^0.27.2"
}
}

57
src/index.js Normal file
View File

@ -0,0 +1,57 @@
const axios = require('axios');
const BASE_URL = "https://www.xeno-canto.org/api/2/recordings?query=";
function getCoords({ coords }) {
if (!!coords) {
if (!!coords.lat && !!coords.lon) {
return `lat:${coords.lat} lon:${coords.lon}`;
} else if (!!coords.box) {
/** TODO box coordinates*/
}
}
}
function search(query) {
let url;
if (typeof (query) === 'string') {
url = BASE_URL + query;
} else if (typeof (query) === 'object') {
const xeno_params = [
{ name: "" },
{ genus: 'gen:' },
{ recordist: 'rec:' },
{ country: 'cnt:' },
{ location: 'loc:' },
{ remarks: 'rmk:' },
{ coords: getCoords(query) },
{ also: 'also:' },
{ type: 'type:' },
{ nr: 'nr:' },
{ quality: 'q:' },
{ qualitylt: 'q<:' },
{ area: 'area:' }
];
let url_params = xeno_params.reduce(function (acc, param) {
const key = Object.keys(param)[0];
if (!!query[key]) {
if (key === 'coords') {
acc += param[key] + " ";
} else {
acc += param[key] + query[key] + " ";
}
};
return acc;
}, "");
url = BASE_URL + url_params;
} else {
throw new Error("Null query error");
}
return axios.get(url).then(response => {
return response.data;
}).catch(error => {
throw error;
});
}
module.exports = { search };

18
test/xeno-canto.test.js Normal file
View File

@ -0,0 +1,18 @@
const XenoCanto = require('../lib/index');
test('search red robin', () => {
return XenoCanto.search('Erithacus rubecula')
.then(data => {
expect(parseInt(data.numRecordings)).toBeGreaterThan(0);
}).catch(error => {
console.error(error);
});
});
test('advanced search for london coordinates', () => {
return XenoCanto.search({ coords: { lat: 51.5073509, lon: -0.1277583 } }).then(data => {
expect(parseInt(data.numRecordings)).toBeGreaterThan(0);
}).catch(error => {
console.error(error);
});
}, 10000)

View File

@ -1,40 +0,0 @@
'use strict';
var XenoCanto = require('../lib/xeno-canto.js');
console.debug("WARNING: The hardcoded values used in this test may change, from xeno-canto database, please double check the current recording count for each query");
exports['xeno-canto-api'] = {
setUp: function(done) {
done();
},
'search bearded bellbird returns 74 recordings': function(test) {
test.expect(4);
var xeno_canto = new XenoCanto();
xeno_canto.search("bearded bellbird", function(self){
test.ok(!!self);
test.equal(typeof(self), 'object');
test.equal(typeof(self.entity), 'object');
test.equal(self.entity.numRecordings, 74);
test.done();
});
},
'advanced search bearded bellbird cnt:Venezuela (27)': function(test) {
test.expect(4);
var xeno_canto = new XenoCanto();
var query = {
name:'bearded bellbird',
country: 'Venezuela',
};
xeno_canto.search(query, function(self){
test.ok(!!self);
test.equal(typeof(self), 'object');
test.equal(typeof(self.entity), 'object');
test.equal(self.entity.numRecordings, 27);
test.done();
});
},
};