diff --git a/.eslintrc.js b/.eslintrc.js new file mode 100644 index 0000000..7bb0b7e --- /dev/null +++ b/.eslintrc.js @@ -0,0 +1,6 @@ +module.exports = { + extends: 'guo', + rules: { + 'valid-jsdoc': 'off', + } +}; \ No newline at end of file diff --git a/package.json b/package.json index 23911ec..9487037 100644 --- a/package.json +++ b/package.json @@ -1,29 +1,32 @@ { - "name": "tencentyoutuyun", - "version": "1.0.0", - "description": "node sdk for qcloud youtu", + "name": "youtusdk", + "version": "2.0.0", + "description": "腾讯优图开放平台第三方SDK", "main": "index.js", "scripts": { - "test": "echo \"Error: no test specified\" && exit 1" + "sample": "export DEBUG='YouTu:*' && node sample/sample.js", + "test": "echo \"Error: no test specified\" && exit 1", + "lint": "eslint tencentyoutuyun sample --fix" }, "repository": { "type": "git", - "url": "git+https://github.com/TencentYouTu/nodejs_sdk.git" + "url": "https://github.com/yourtion/node-youtu" }, "keywords": [ "qcloud", "youtu" ], - "author": "tencentyoutuyun", + "author": "YourtionGuo ", "license": "MIT", "bugs": { - "url": "https://github.com/TencentYouTu/nodejs_sdk/issues" + "url": "https://github.com/yourtion/node-youtu/issues" }, - "homepage": "https://github.com/TencentYouTu/nodejs_sdk#readme", - "engines": [ - "node >= 0.4.7" - ], + "homepage": "https://github.com/yourtion/node-youtu#readme", "dependencies": { - "iconv-lite": "^0.4.13" + "debug": "^2.2.0" + }, + "devDependencies": { + "eslint": "^4.12.1", + "eslint-config-guo": "^0.6.0" } } diff --git a/run b/run new file mode 100755 index 0000000..a53eaa2 --- /dev/null +++ b/run @@ -0,0 +1,2 @@ +export DEBUG=* +node sample/sample.js diff --git a/sample/sample.js b/sample/sample.js index 9ac0a21..bc617bc 100644 --- a/sample/sample.js +++ b/sample/sample.js @@ -1,26 +1,59 @@ -var tencentyoutuyun = require('..'); -var conf = tencentyoutuyun.conf; -var youtu = tencentyoutuyun.youtu; +const tencentyoutuyun = require('..'); +const conf = tencentyoutuyun.conf; +const youtu = tencentyoutuyun.youtu; // 设置开发者和应用信息, 请填写你在开放平台 -var appid = '****'; -var secretId = '****'; -var secretKey = '****'; -var userid = '****'; +const appid = '****'; +const secretId = '****'; +const secretKey = '****'; +const userid = '****'; -conf.setAppInfo(appid, secretId, secretKey, userid, 0) +conf.setAppInfo(appid, secretId, secretKey, userid, 0); -//youtu.imageporn('a1.jpg', function(data){ +// 人脸检测 测试 +// youtu.detectface('a.jpg', 0, function (data){ +// console.log('detectface:' + JSON.stringify(data)); +// }); + +// 人脸比对 测试 +// youtu.facecompare('a.jpg', 'a.jpg', function(data){ +// console.log("facecompare:" + JSON.stringify(data)); +// }); + +// 人脸比对 测试 +// youtu.fuzzydetect('a.jpg', function(data){ +// console.log("fuzzydetect:" + JSON.stringify(data)); +// }) + +// youtu.fooddetect('a.jpg', function(data){ +// console.log("fooddetect:" + JSON.stringify(data)); +// }); + +// youtu.imagetag('a.jpg', function(data){ +// console.log("imagetag:" + JSON.stringify(data)); +// }); + + +// youtu.imageporn('a1.jpg', function(data){ // console.log("imagetag:" + JSON.stringify(data)); -//}); +// }); -//youtu.idcardocr('a.jpg', 0, function(data){ +// youtu.idcardocr('a.jpg', 0, function(data){ // console.log("idcardocr:" + JSON.stringify(data)); -//}); +// }); -//youtu.namecardocr('a.jpg', false, function(data){ +// youtu.namecardocr('a.jpg', false, function(data){ // console.log("namecardocr:" + JSON.stringify(data)); -//}); +// }); // 其他接口 类似 +// 身份证OCR识别 +// youtu.idcardocr('sfz3.jpg',0,function(data){ +// console.log("fuzzydetect:" + JSON.stringify(data)); +// }) + +// 身份证OCR识别 +// youtu.namecardocr('mp2.jpg',true,function(data){ +// console.log("fuzzydetect:" + JSON.stringify(data)); +// }) diff --git a/tencentyoutuyun/auth.js b/tencentyoutuyun/auth.js index b5cd13f..44e31b0 100644 --- a/tencentyoutuyun/auth.js +++ b/tencentyoutuyun/auth.js @@ -1,42 +1,41 @@ -var crypto = require('crypto'); -var urlM = require('url'); -var conf = require('./conf'); +const crypto = require('crypto'); +const conf = require('./conf'); exports.AUTH_PARAMS_ERROR = -1; exports.AUTH_SECRET_ID_KEY_ERROR = -2; -exports.appSign = function(expired, userid) { +exports.appSign = function (expired, userid) { - var secretId = conf.SECRET_ID || ''; - var secretKey = conf.SECRET_KEY || ''; - var appid = conf.APPID || ''; + const secretId = conf.SECRET_ID || ''; + const secretKey = conf.SECRET_KEY || ''; + const appid = conf.APPID || ''; - var pexpired = expired || ''; - var puserid = userid || conf.USERID; + const pexpired = expired || ''; + const puserid = userid || conf.USERID; - if (!pexpired || !puserid ) { - return module.exports.AUTH_PARAMS_ERROR; - } + if (!pexpired || !puserid) { + return module.exports.AUTH_PARAMS_ERROR; + } - if ( !secretId || !secretKey ) { - return module.exports.AUTH_SECRET_ID_KEY_ERROR; - } + if (!secretId || !secretKey) { + return module.exports.AUTH_SECRET_ID_KEY_ERROR; + } - var now = parseInt(Date.now() / 1000); - var rdm = parseInt(Math.random() * Math.pow(2, 32)); + const now = parseInt(Date.now() / 1000, 10); + const rdm = parseInt(Math.random() * Math.pow(2, 32), 10); - // the order of every key is not matter verify - var plainText = 'a=' + appid + '&k=' + secretId + '&e=' + pexpired + '&t=' + now + '&r=' + rdm + '&u=' + puserid; + // the order of every key is not matter verify + const plainText = 'a=' + appid + '&k=' + secretId + '&e=' + pexpired + '&t=' + now + '&r=' + rdm + '&u=' + puserid; - var data = new Buffer(plainText,'utf8'); + const data = new Buffer(plainText, 'utf8'); - var res = crypto.createHmac('sha1',secretKey).update(data).digest(); + const res = crypto.createHmac('sha1', secretKey).update(data).digest(); - var bin = Buffer.concat([res,data]); + const bin = Buffer.concat([ res, data ]); - var sign = bin.toString('base64'); + const sign = bin.toString('base64'); - return sign; -} + return sign; +}; diff --git a/tencentyoutuyun/conf.js b/tencentyoutuyun/conf.js index 267f5aa..468252e 100644 --- a/tencentyoutuyun/conf.js +++ b/tencentyoutuyun/conf.js @@ -1,40 +1,36 @@ -var fs = require('fs'); -var path = require('path'); -var os = require('os'); +const fs = require('fs'); +const path = require('path'); +const os = require('os'); // 请到 open.youtu.qq.com查看您对应的appid相关信息并填充 -// 请统一 通过 setAppInfo 设置 +// 请统一 通过 setAppInfo 设置 exports.APPID = ''; exports.SECRET_ID = ''; exports.SECRET_KEY = ''; exports.USERID = ''; -var pkg = JSON.parse(fs.readFileSync(path.join(__dirname, '../', 'package.json'))); -var ua = function() { - return 'QcloudNodejs/' + pkg.version + ' (' + os.type() + '; ' + os.platform() + '; ' + os.arch() + '; ) '; -} +const pkg = JSON.parse(fs.readFileSync(path.join(__dirname, '../', 'package.json'))); +const ua = function () { + return 'YouTuSDK/' + pkg.version + ' (' + os.type() + '; ' + os.platform() + '; ' + os.arch() + '; ) '; +}; -exports.USER_AGENT = ua; -exports.API_YOUTU_SERVER= 'api.youtu.qq.com'; -exports.API_YOUTU_PORT= 80; +exports.getAgent = ua; +exports.API_YOUTU_SERVER = 'api.youtu.qq.com'; +exports.API_YOUTU_PORT = 80; exports.API_DOMAIN = 0; -// 初始化 应用信息 -exports.setAppInfo = function(appid, secretId, secretKey, userid, domain) { - module.exports.APPID = appid; - module.exports.SECRET_ID = secretId; - module.exports.SECRET_KEY = secretKey; - module.exports.USERID = userid; - if(domain == 0) - { - exports.API_YOUTU_SERVER= 'api.youtu.qq.com'; - } - else - { - exports.API_YOUTU_SERVER= 'youtu.api.qcloud.com'; - } - exports.API_DOMAIN = domain; -} - +// 初始化 应用信息 +exports.setAppInfo = function (appid, secretId, secretKey, userid, domain) { + module.exports.APPID = appid; + module.exports.SECRET_ID = secretId; + module.exports.SECRET_KEY = secretKey; + module.exports.USERID = userid; + if(domain === 0) { + exports.API_YOUTU_SERVER = 'api.youtu.qq.com'; + } else { + exports.API_YOUTU_SERVER = 'youtu.api.qcloud.com'; + } + exports.API_DOMAIN = domain; +}; diff --git a/tencentyoutuyun/youtu.js b/tencentyoutuyun/youtu.js index 2772069..0a7e350 100644 --- a/tencentyoutuyun/youtu.js +++ b/tencentyoutuyun/youtu.js @@ -1,143 +1,120 @@ -var http = require('http'); -var https = require('https'); -var fs = require('fs'); +const http = require('http'); +const https = require('https'); +const fs = require('fs'); +const debug = require('debug')('YouTu:'); - - -var auth = require('./auth'); -var conf = require('./conf'); +const auth = require('./auth'); +const conf = require('./conf'); // 30 days -var EXPIRED_SECONDS = 2592000; +const EXPIRED_SECONDS = 2592000; +const cb = function (ret) { + console.log(ret); +}; /** - * return the status message + * return the status message */ function statusText(status) { + switch (status) { + case 200: + return 'HTTP OK'; + case 400: + return 'Bad Request'; + case 401: + return 'Unauthorized'; + case 403: + return 'Forbidden'; + case 500: + return 'Internal Server Error'; + default: + return 'unkown'; + } +} - var statusText = 'unkown'; - - switch (status) { - case 200: - statusText = 'HTTP OK'; - break; - case 400: - statusText = 'Bad Request'; - break; - case 401: - statusText = 'Unauthorized'; - break; - case 403: - statusText = 'Forbidden'; - break; - case 500: - statusText = 'Internal Server Error'; - break; - } - return statusText; -}; +function getrequest(protocol, params, callback) { -function getrequest(protocol, params, callback) { + return protocol.request(params, function (response) { + + debug('STATUS: ' + response.statusCode); + debug('HEADERS: ' + JSON.stringify(response.headers)); - return protocol.request(params, function(response) { + if(response.statusCode !== 200) { + callback({ 'httpcode': response.statusCode, 'code': response.statusCode, 'message': statusText(response.statusCode), 'data': {}}); + return; + } + + let body = ''; + response.setEncoding('utf8'); - // console.log('STATUS: ' + response.statusCode); - // console.log('HEADERS: ' + JSON.stringify(response.headers)); - - if( response.statusCode != 200 ){ - callback({'httpcode':response.statusCode, 'code':response.statusCode , 'message':statusText(response.statusCode) , 'data':{}}); - return; - } - - var body = ''; - response.setEncoding('utf8'); - - response.on('data', function (chunk) { - body += chunk; - }); - response.on('end', function(){ - callback({'httpcode':response.statusCode, 'code':response.statusCode , 'message':statusText(response.statusCode) , 'data':JSON.parse(body)}); - }); - - response.on('error', function(e){ - callback({'httpcode':response.statusCode, 'code':response.statusCode , 'message': '' + e , 'data':{} }); - }); - }); // -}; + response.on('data', function (chunk) { + body += chunk; + }); + response.on('end', function (){ + callback({ 'httpcode': response.statusCode, 'code': response.statusCode, 'message': statusText(response.statusCode), 'data': JSON.parse(body) }); + }); + + response.on('error', function (e){ + callback({ 'httpcode': response.statusCode, 'code': response.statusCode, 'message': '' + e, 'data': {}}); + }); + }); +} + +function getFile(imagePath, callback) { + if(imagePath.substring(0, 4) === 'http') { + return callback(null, imagePath, true); + } + fs.readFile(imagePath, function (err, file) { + if(err) return callback(err); + callback(null, file.toString('base64'), false); + }); +} /** * @brief detectface * @param imagePath 待检测的路径(本地路径或url) - * @param isbigface 是否大脸模式 0表示检测所有人脸, 1表示只检测照片最大人脸 适合单人照模式 + * @param isbigface 是否大脸模式 0表示检测所有人脸, 1表示只检测照片最大人脸 适合单人照模式 * @param callback 回调函数, 参见Readme 文档 */ -exports.detectface = function(imagePath, isbigface, callback) { - - callback = callback || function(ret){console.log(ret)}; - - var expired = parseInt(Date.now() / 1000) + EXPIRED_SECONDS; - var sign = auth.appSign(expired); - var tag = imagePath.substring(0,4); - var request_body = ''; - if (tag == 'http') - { - request_body = JSON.stringify({ - app_id: conf.APPID, - url : imagePath, - mode : isbigface, - }); - } - else - { - try { - var data = fs.readFileSync(imagePath).toString('base64'); - } catch (e) { - callback({'httpcode':0, 'code':-1, 'message':'file ' + imagePath + ' not exists', 'data':{}}); - return; - } - - if(data == null) { - callback({'httpcode':0, 'code':-1, 'message':'file ' + imagePath + ' not exists or params error', 'data':{}}); - return; - }; - - request_body = JSON.stringify({ - app_id: conf.APPID, - image : data.toString('base64'), - mode : isbigface, - }); - } - var params = { - hostname: conf.API_YOUTU_SERVER, - path: '/youtu/api/detectface', - method: 'POST', - headers: { - 'Authorization': sign, - 'User-Agent' : conf.USER_AGENT(), - 'Content-Length': request_body.length, - 'Content-Type': 'text/json' - } - }; +exports.detectface = function (imagePath, isbigface, callback) { + + callback = callback || cb; + + const expired = parseInt(Date.now() / 1000, 10) + EXPIRED_SECONDS; + const sign = auth.appSign(expired); + getFile(imagePath, function (err, data, url) { + if(err) return callback({ 'httpcode': 0, 'code': -1, 'message': 'file ' + imagePath + ' not exists', 'data': {}}); - //console.log(request_body); - //console.log(params); - var request = null; - if (conf.API_DOMAIN == 0) - { - request = getrequest(http, params, callback); - } - else { - request = getrequest(https, params, callback); - } + const request_body = JSON.stringify({ + app_id: conf.APPID, + url: url ? data : undefined, + image: !url ? data : undefined, + mode: isbigface, + }); + const params = { + hostname: conf.API_YOUTU_SERVER, + path: '/youtu/api/detectface', + method: 'POST', + headers: { + 'Authorization': sign, + 'User-Agent': conf.getAgent(), + 'Content-Length': request_body.length, + 'Content-Type': 'text/json', + }, + }; - request.on('error', function(e) { - callback({'httpcode': 0, 'code': 0, 'message':e.message, 'data': {}}); + // debug(request_body); + debug(params); + const request = conf.API_DOMAIN === 0 ? getrequest(http, params, callback) : getrequest(https, params, callback); + + request.on('error', function (e) { + callback({ 'httpcode': 0, 'code': 0, 'message': e.message, 'data': {}}); }); - // send the request body - request.end(request_body); - //console.log(request_body); -} + request.end(request_body); + // debug(request_body); + }); +}; /** @@ -146,69 +123,43 @@ exports.detectface = function(imagePath, isbigface, callback) { * @param isbigface 是否大脸模式 * @param callback 回调函数, 参见Readme 文档 */ -exports.faceshape = function(imagePath, isbigface, callback) { - - callback = callback || function(ret){console.log(ret)}; - - var expired = parseInt(Date.now() / 1000) + EXPIRED_SECONDS; - var sign = auth.appSign(expired); - var tag = imagePath.substring(0,4); - var request_body = ''; - if (tag == 'http') - { - request_body = JSON.stringify({ - app_id: conf.APPID, - url : imagePath, - mode : isbigface, - }); - } - else - { - try { - var data = fs.readFileSync(imagePath).toString('base64'); - } catch (e) { - callback({'httpcode':0, 'code':-1, 'message':'file ' + imagePath + ' not exists', 'data':{}}); - return; - } - if(data == null) { - callback({'httpcode':0, 'code':-1, 'message':'file ' + imagePath + ' not exists or params error', 'data':{}}); - return; - }; - - request_body = JSON.stringify({ - app_id: conf.APPID, - image : data.toString('base64'), - mode : isbigface, - }); - } - var params = { - hostname: conf.API_YOUTU_SERVER, - path: '/youtu/api/faceshape', - method: 'POST', - headers: { - 'Authorization': sign, - 'User-Agent' : conf.USER_AGENT(), - 'Content-Length': request_body.length, - 'Content-Type': 'text/json' - } +exports.faceshape = function (imagePath, isbigface, callback) { + + callback = callback || cb; + + const expired = parseInt(Date.now() / 1000, 10) + EXPIRED_SECONDS; + const sign = auth.appSign(expired); + getFile(imagePath, function (err, data, url) { + if(err) return callback({ 'httpcode': 0, 'code': -1, 'message': 'file ' + imagePath + ' not exists', 'data': {}}); + + const request_body = JSON.stringify({ + app_id: conf.APPID, + url: url ? data : undefined, + image: !url ? data : undefined, + mode: isbigface, + }); + const params = { + hostname: conf.API_YOUTU_SERVER, + path: '/youtu/api/faceshape', + method: 'POST', + headers: { + 'Authorization': sign, + 'User-Agent': conf.getAgent(), + 'Content-Length': request_body.length, + 'Content-Type': 'text/json', + }, }; - - var request = null; - if (conf.API_DOMAIN == 0) - { - request = getrequest(http, params, callback); - } - else { - request = getrequest(https, params, callback); - } - request.on('error', function(e) { - callback({'httpcode': 0, 'code': 0, 'message':e.message, 'data': {}}); + const request = conf.API_DOMAIN === 0 ? getrequest(http, params, callback) : getrequest(https, params, callback); + + request.on('error', function (e) { + callback({ 'httpcode': 0, 'code': 0, 'message': e.message, 'data': {}}); }); // send the request body request.end(request_body); -} + }); +}; /** @@ -217,75 +168,52 @@ exports.faceshape = function(imagePath, isbigface, callback) { * @param image_b 待比对的B图片路径(本地路径或url) * @param callback 回调函数, 参见Readme 文档 */ -exports.facecompare = function(image_a, image_b, callback) { - - callback = callback || function(ret){console.log(ret)}; - - var expired = parseInt(Date.now() / 1000) + EXPIRED_SECONDS; - var sign = auth.appSign(expired); - var tag = image_a.substring(0,4); - var request_body = ''; - if (tag == 'http') - { - var request_body = JSON.stringify({ - app_id: conf.APPID, - urlA : image_a, - urlB : image_b, - }); - } - else - { - try { - var data_a = fs.readFileSync(image_a).toString('base64'); - var data_b = fs.readFileSync(image_b).toString('base64'); - } catch (e) { - callback({'httpcode':0, 'code':-1, 'message':'file ' + image_a + ' or ' + image_b + ' not exists', 'data':{}}); - return; - } - - - - if(data_a == null || data_b == null ) { - callback({'httpcode':0, 'code':-1, 'message':'file ' + imagePath + ' not exists or params error', 'data':{}}); - return; - }; - - var request_body = JSON.stringify({ - app_id: conf.APPID, - imageA : data_a.toString('base64'), - imageB : data_b.toString('base64'), - }); +exports.facecompare = function (image_a, image_b, callback) { + + callback = callback || cb; + + const expired = parseInt(Date.now() / 1000, 10) + EXPIRED_SECONDS; + const sign = auth.appSign(expired); + getFile(image_a, function (err, data_a, url1) { + if(err) { + return callback({ 'httpcode': 0, 'code': -1, 'message': 'file ' + image_a + ' not exists', 'data': {}}); } - - var params = { + getFile(image_b, function (err, data_b, url2) { + if(err) { + return callback({ 'httpcode': 0, 'code': -1, 'message': 'file ' + image_b + ' not exists', 'data': {}}); + } + + const request_body = JSON.stringify({ + app_id: conf.APPID, + urlA: url1 ? data_a : undefined, + imageA: !url1 ? data_a : undefined, + urlB: url1 ? data_b : undefined, + imageB: !url2 ? data_b : undefined, + }); + + const params = { hostname: conf.API_YOUTU_SERVER, path: '/youtu/api/facecompare', method: 'POST', headers: { - 'Authorization': sign, - 'User-Agent' : conf.USER_AGENT(), - 'Content-Length': request_body.length, - 'Content-Type': 'text/json' - } - }; - - //console.log(request_body); - var request = null; - if (conf.API_DOMAIN == 0) - { - request = getrequest(http, params, callback); - } - else { - request = getrequest(https, params, callback); - } - - request.on('error', function(e) { - callback({'httpcode': 0, 'code': 0, 'message':e.message, 'data': {}}); + 'Authorization': sign, + 'User-Agent': conf.getAgent(), + 'Content-Length': request_body.length, + 'Content-Type': 'text/json', + }, + }; + + const request = conf.API_DOMAIN === 0 ? getrequest(http, params, callback) : getrequest(https, params, callback); + + request.on('error', function (e) { + callback({ 'httpcode': 0, 'code': 0, 'message': e.message, 'data': {}}); + }); + + // send the request body + request.end(request_body); }); - - // send the request body - request.end(request_body); -} + }); +}; /** @@ -294,61 +222,47 @@ exports.facecompare = function(image_a, image_b, callback) { * @param image_url 待比对的B图片路径(url) * @param callback 回调函数, 参见Readme 文档 */ -exports.facecompare_file_url = function(image_file, image_url, callback) { +exports.facecompare_file_url = function (image_file, image_url, callback) { - callback = callback || function(ret){console.log(ret)}; + callback = callback || cb; - var expired = parseInt(Date.now() / 1000) + EXPIRED_SECONDS; - var sign = auth.appSign(expired); + const expired = parseInt(Date.now() / 1000, 10) + EXPIRED_SECONDS; + const sign = auth.appSign(expired); - try { - var data_a = fs.readFileSync(image_file).toString('base64'); - } catch (e) { - callback({'httpcode':0, 'code':-1, 'message':'file ' + image_file + ' not exists', 'data':{}}); - return; + getFile(image_file, function (err, data) { + if(err) { + return callback({ 'httpcode': 0, 'code': -1, 'message': 'file ' + image_file + ' not exists', 'data': {}}); } - - if(data_a == null ) { - callback({'httpcode':0, 'code':-1, 'message':'file ' + imagePath + ' not exists or params error', 'data':{}}); - return; - }; - var request_body = JSON.stringify({ - app_id: conf.APPID, - imageA : data_a.toString('base64'), - urlB : image_url, + const request_body = JSON.stringify({ + app_id: conf.APPID, + imageA: data, + urlB: image_url, }); - - var params = { - hostname: conf.API_YOUTU_SERVER, - path: '/youtu/api/facecompare', - method: 'POST', - headers: { - 'Authorization': sign, - 'User-Agent' : conf.USER_AGENT(), - 'Content-Length': request_body.length, - 'Content-Type': 'text/json' - } + const params = { + hostname: conf.API_YOUTU_SERVER, + path: '/youtu/api/facecompare', + method: 'POST', + headers: { + 'Authorization': sign, + 'User-Agent': conf.getAgent(), + 'Content-Length': request_body.length, + 'Content-Type': 'text/json', + }, }; - - //console.log(request_body); - var request = null; - if (conf.API_DOMAIN == 0) - { - request = getrequest(http, params, callback); - } - else { - request = getrequest(https, params, callback); - } - - request.on('error', function(e) { - callback({'httpcode': 0, 'code': 0, 'message':e.message, 'data': {}}); + + // console.log(request_body); + const request = conf.API_DOMAIN === 0 ? getrequest(http, params, callback) : getrequest(https, params, callback); + + request.on('error', function (e) { + callback({ 'httpcode': 0, 'code': 0, 'message': e.message, 'data': {}}); }); // send the request body request.end(request_body); -} + }); +}; /** @@ -357,75 +271,45 @@ exports.facecompare_file_url = function(image_file, image_url, callback) { * @param imagePath 待验证的图片路径(本地路径或url) * @param callback 回调函数, 参见Readme 文档 */ -exports.faceverify = function(imagePath, person_id, callback) { +exports.faceverify = function (imagePath, person_id, callback) { - callback = callback || function(ret){console.log(ret)}; + callback = callback || cb; - var expired = parseInt(Date.now() / 1000) + EXPIRED_SECONDS; - var sign = auth.appSign(expired); - var tag = imagePath.substring(0,4); - var request_body = ''; + const expired = parseInt(Date.now() / 1000, 10) + EXPIRED_SECONDS; + const sign = auth.appSign(expired); + getFile(imagePath, function (err, data, url) { + if(err) return callback({ 'httpcode': 0, 'code': -1, 'message': 'file ' + imagePath + ' not exists', 'data': {}}); - if (tag == 'http') - { - request_body = JSON.stringify({ - app_id: conf.APPID, - url : imagePath, - person_id : person_id, - }); - } - else - { - try { - var data = fs.readFileSync(imagePath).toString('base64'); - } catch (e) { - callback({'httpcode':0, 'code':-1, 'message':'file ' + imagePath + ' not exists', 'data':{}}); - return; - } - - if(data == null) { - callback({'httpcode':0, 'code':-1, 'message':'file ' + imagePath + ' not exists or params error', 'data':{}}); - return; - }; - - request_body = JSON.stringify({ - app_id: conf.APPID, - image : data.toString('base64'), - person_id : person_id, - }); - } - - var params = { - hostname: conf.API_YOUTU_SERVER, - path: '/youtu/api/faceverify', - method: 'POST', - headers: { - 'Authorization': sign, - 'User-Agent' : conf.USER_AGENT(), - 'Content-Length': request_body.length, - 'Content-Type': 'text/json' - } + const request_body = JSON.stringify({ + app_id: conf.APPID, + url: url ? data : undefined, + image: !url ? data : undefined, + person_id, + }); + + const params = { + hostname: conf.API_YOUTU_SERVER, + path: '/youtu/api/faceverify', + method: 'POST', + headers: { + 'Authorization': sign, + 'User-Agent': conf.getAgent(), + 'Content-Length': request_body.length, + 'Content-Type': 'text/json', + }, }; - - //console.log(request_body); - var request = null; - if (conf.API_DOMAIN == 0) - { - request = getrequest(http, params, callback); - } - else { - request = getrequest(https, params, callback); - } - - - request.on('error', function(e) { - callback({'httpcode': 0, 'code': 0, 'message':e.message, 'data': {}}); - }); + + // debug(request_body); + const request = conf.API_DOMAIN === 0 ? getrequest(http, params, callback) : getrequest(https, params, callback); + + request.on('error', function (e) { + callback({ 'httpcode': 0, 'code': 0, 'message': e.message, 'data': {}}); + }); // send the request body request.end(request_body); - -} + }); +}; /** @@ -434,73 +318,46 @@ exports.faceverify = function(imagePath, person_id, callback) { * @param imagePath 待识别的图片路径(本地路径或url) * @param callback 回调函数, 参见Readme 文档 */ -exports.faceidentify= function(imagePath, group_id, callback) { +exports.faceidentify = function (imagePath, group_id, callback) { - callback = callback || function(ret){console.log(ret)}; + callback = callback || cb; - var expired = parseInt(Date.now() / 1000) + EXPIRED_SECONDS; - var sign = auth.appSign(expired); - var tag = imagePath.substring(0,4); - var request_body = ''; - - if (tag == 'http') - { - request_body = JSON.stringify({ - app_id: conf.APPID, - url : imagePath, - group_id : group_id, - }); - } - else - { - try { - var data = fs.readFileSync(imagePath).toString('base64'); - } catch (e) { - callback({'httpcode':0, 'code':-1, 'message':'file ' + imagePath + ' not exists', 'data':{}}); - return; - } - - - if(data == null) { - callback({'httpcode':0, 'code':-1, 'message':'file ' + imagePath + ' not exists or params error', 'data':{}}); - return; - }; - - request_body = JSON.stringify({ - app_id: conf.APPID, - image : data.toString('base64'), - group_id : group_id, - }); - } + const expired = parseInt(Date.now() / 1000, 10) + EXPIRED_SECONDS; + const sign = auth.appSign(expired); + getFile(imagePath, function (err, data, url) { + if(err) return callback({ 'httpcode': 0, 'code': -1, 'message': 'file ' + imagePath + ' not exists', 'data': {}}); - var params = { - hostname: conf.API_YOUTU_SERVER, - path: '/youtu/api/faceidentify', - method: 'POST', - headers: { - 'Authorization': sign, - 'User-Agent' : conf.USER_AGENT(), - 'Content-Length': request_body.length, - 'Content-Type': 'text/json' - } + + const request_body = JSON.stringify({ + app_id: conf.APPID, + url: url ? data : undefined, + image: !url ? data : undefined, + group_id, + }); + + const params = { + hostname: conf.API_YOUTU_SERVER, + path: '/youtu/api/faceidentify', + method: 'POST', + headers: { + 'Authorization': sign, + 'User-Agent': conf.getAgent(), + 'Content-Length': request_body.length, + 'Content-Type': 'text/json', + }, }; - - var request = null; - if (conf.API_DOMAIN == 0) - { - request = getrequest(http, params, callback); - } - else { - request = getrequest(https, params, callback); - } - - request.on('error', function(e) { - callback({'httpcode': 0, 'code': 0, 'message':e.message, 'data': {}}); + + const request = conf.API_DOMAIN === 0 ? getrequest(http, params, callback) : getrequest(https, params, callback); + + + request.on('error', function (e) { + callback({ 'httpcode': 0, 'code': 0, 'message': e.message, 'data': {}}); }); // send the request body request.end(request_body); -} + }); +}; /** * @brief newperson @@ -511,83 +368,49 @@ exports.faceidentify= function(imagePath, group_id, callback) { * @param persontag 人备注信息,用户自解释字段 * @param callback 回调函数, 参见Readme 文档 */ -exports.newperson= function(imagePath, person_id, person_name, group_ids, persontag, callback) { +exports.newperson = function (imagePath, person_id, person_name, group_ids, persontag, callback) { - callback = callback || function(ret){console.log(ret)}; + callback = callback || cb; - var expired = parseInt(Date.now() / 1000) + EXPIRED_SECONDS; - var sign = auth.appSign(expired); - - var tag = imagePath.substring(0,4); - var request_body = ''; - - if (tag == 'http') - { - request_body = JSON.stringify({ - app_id: conf.APPID, - url : imagePath, - person_id : person_id, - person_name: person_name, - group_ids : group_ids, - tag: persontag, - }); - } - else - { - try { - var data = fs.readFileSync(imagePath).toString('base64'); - } catch (e) { - callback({'httpcode':0, 'code':-1, 'message':'file ' + imagePath + ' not exists', 'data':{}}); - return; - } - - - if(data == null) { - callback({'httpcode':0, 'code':-1, 'message':'file ' + imagePath + ' not exists or params error', 'data':{}}); - return; - }; - - request_body = JSON.stringify({ - app_id: conf.APPID, - image : data.toString('base64'), - person_id : person_id, - person_name: person_name, - group_ids : group_ids, - tag: persontag, - }); - } + const expired = parseInt(Date.now() / 1000, 10) + EXPIRED_SECONDS; + const sign = auth.appSign(expired); + + getFile(imagePath, function (err, data, url) { + if(err) return callback({ 'httpcode': 0, 'code': -1, 'message': 'file ' + imagePath + ' not exists', 'data': {}}); - var buffer = new Buffer(request_body, "UTF-8"); + const request_body = JSON.stringify({ + app_id: conf.APPID, + url: url ? data : undefined, + image: !url ? data : undefined, + person_id, + person_name, + group_ids, + tag: persontag, + }); - var params = { - hostname: conf.API_YOUTU_SERVER, - path: '/youtu/api/newperson', - method: 'POST', - headers: { - 'Authorization': sign, - 'User-Agent' : conf.USER_AGENT(), - 'Content-Length': buffer.length, - 'Content-Type': 'text/json' - } + const params = { + hostname: conf.API_YOUTU_SERVER, + path: '/youtu/api/newperson', + method: 'POST', + headers: { + 'Authorization': sign, + 'User-Agent': conf.getAgent(), + 'Content-Length': request_body.length, + 'Content-Type': 'text/json', + }, }; - var request = null; - if (conf.API_DOMAIN == 0) - { - request = getrequest(http, params, callback); - } - else { - request = getrequest(https, params, callback); - } - - request.on('error', function(e) { - callback({'httpcode': 0, 'code': 0, 'message':e.message, 'data': {}}); + const request = conf.API_DOMAIN === 0 ? getrequest(http, params, callback) : getrequest(https, params, callback); + + request.on('error', function (e) { + callback({ 'httpcode': 0, 'code': 0, 'message': e.message, 'data': {}}); }); - + // send the request body request.end(request_body); -} + }); +}; /** @@ -595,48 +418,40 @@ exports.newperson= function(imagePath, person_id, person_name, group_ids, person * @param person_id 待删除的个体id * @param callback 回调函数, 参见Readme 文档 */ -exports.delperson= function(person_id, callback) { - - callback = callback || function(ret){console.log(ret)}; - - var expired = parseInt(Date.now() / 1000) + EXPIRED_SECONDS; - var sign = auth.appSign(expired); - - var request_body = JSON.stringify({ - app_id: conf.APPID, - person_id : person_id, - }); - - var params = { - hostname: conf.API_YOUTU_SERVER, - port: conf.API_YOUTU_PORT, - path: '/youtu/api/delperson', - method: 'POST', - headers: { - 'Authorization': sign, - 'User-Agent' : conf.USER_AGENT(), - 'Content-Length': request_body.length, - 'Content-Type': 'text/json' - } - }; - - var request = null; - if (conf.API_DOMAIN == 0) - { - request = getrequest(http, params, callback); - } - else { - request = getrequest(https, params, callback); - } - - request.on('error', function(e) { - callback({'httpcode': 0, 'code': 0, 'message':e.message, 'data': {}}); - }); - - // send the request body - request.end(request_body); -} - +exports.delperson = function (person_id, callback) { + + callback = callback || cb; + + const expired = parseInt(Date.now() / 1000, 10) + EXPIRED_SECONDS; + const sign = auth.appSign(expired); + + const request_body = JSON.stringify({ + app_id: conf.APPID, + person_id, + }); + + const params = { + hostname: conf.API_YOUTU_SERVER, + port: conf.API_YOUTU_PORT, + path: '/youtu/api/delperson', + method: 'POST', + headers: { + 'Authorization': sign, + 'User-Agent': conf.getAgent(), + 'Content-Length': request_body.length, + 'Content-Type': 'text/json', + }, + }; + + const request = conf.API_DOMAIN === 0 ? getrequest(http, params, callback) : getrequest(https, params, callback); + + request.on('error', function (e) { + callback({ 'httpcode': 0, 'code': 0, 'message': e.message, 'data': {}}); + }); + + // send the request body + request.end(request_body); +}; /** @@ -646,86 +461,74 @@ exports.delperson= function(person_id, callback) { * @param facetag 人脸备注信息,用户自解释字段 * @param callback 回调函数, 参见Readme 文档 */ - exports.addface = function(person_id, images, facetag, callback) { - - callback = callback || function(ret){console.log(ret)}; +exports.addface = function (person_id, images, facetag, callback) { + + callback = callback || cb; + + const expired = parseInt(Date.now() / 1000, 10) + EXPIRED_SECONDS; + const sign = auth.appSign(expired); + + const tag = images[0].substring(0, 4); + let request_body = ''; + let data; + if (tag === 'http') { + request_body = JSON.stringify({ + app_id: conf.APPID, + urls: images, + person_id, + tag: facetag, + }); + } else { + const image_bufs = []; + for(const idx in images) { + try { + data = fs.readFileSync(images[idx]); + } catch (e) { + callback({ 'httpcode': 0, 'code': -1, 'message': 'file ' + images[idx] + ' not exists', 'data': {}}); + return; + } + + if(data == null) { + callback({ 'httpcode': 0, 'code': -1, 'message': images[idx] + ': read failed!', 'data': {}}); + return; + } - var expired = parseInt(Date.now() / 1000) + EXPIRED_SECONDS; - var sign = auth.appSign(expired); - - var tag = images[0].substring(0,4); - var request_body = ''; - - if (tag == 'http') - { - request_body = JSON.stringify({ - app_id: conf.APPID, - urls : images, - person_id : person_id, - tag : facetag - }); - } - else - { - var image_bufs = new Array(); - - for( var idx in images) - { - try { - var data =fs.readFileSync(images[idx]); - } catch (e) { - callback({'httpcode':0, 'code':-1, 'message':'file ' + images[idx] + ' not exists', 'data':{}}); - return; - } - - if(data == null) { - callback({'httpcode':0, 'code':-1, 'message': images[idx] + ": read failed!", 'data':{}}); - return; - } - - image_bufs[idx] = data.toString('base64'); - } - - request_body = JSON.stringify({ - app_id: conf.APPID, - images : image_bufs, - person_id : person_id, - tag : facetag - }); + image_bufs[idx] = data.toString('base64'); } - - //console.log(request_body); - var buffer = new Buffer(request_body, "UTF-8"); - var params = { - hostname: conf.API_YOUTU_SERVER, - path: '/youtu/api/addface', - method: 'POST', - headers: { - 'Authorization': sign, - 'User-Agent' : conf.USER_AGENT(), - 'Content-Length': buffer.length, - 'Content-Type': 'text/json' - } - }; - - //console.log(params); - var request = null; - if (conf.API_DOMAIN == 0) - { - request = getrequest(http, params, callback); - } - else { - request = getrequest(https, params, callback); - } - - request.on('error', function(e) { - callback({'httpcode': 0, 'code': 0, 'message':e.message, 'data': {}}); + + request_body = JSON.stringify({ + app_id: conf.APPID, + images: image_bufs, + person_id, + tag: facetag, }); + } + + // debug(request_body); + const buffer = new Buffer(request_body, 'UTF-8'); + const params = { + hostname: conf.API_YOUTU_SERVER, + path: '/youtu/api/addface', + method: 'POST', + headers: { + 'Authorization': sign, + 'User-Agent': conf.getAgent(), + 'Content-Length': buffer.length, + 'Content-Type': 'text/json', + }, + }; + + debug(params); + const request = conf.API_DOMAIN === 0 ? getrequest(http, params, callback) : getrequest(https, params, callback); + + request.on('error', function (e) { + callback({ 'httpcode': 0, 'code': 0, 'message': e.message, 'data': {}}); + }); + + // send the request body + request.end(request_body); - // send the request body - request.end(request_body); - -} +}; /** @@ -734,47 +537,40 @@ exports.delperson= function(person_id, callback) { * @param face_ids 待删除的人脸id 数组 * @param callback 回调函数, 参见Readme 文档 */ -exports.delface = function(person_id, face_ids, callback) { - - callback = callback || function(ret){console.log(ret)}; - - var expired = parseInt(Date.now() / 1000) + EXPIRED_SECONDS; - var sign = auth.appSign(expired); - - var request_body = JSON.stringify({ - app_id: conf.APPID, - face_ids: face_ids, - person_id : person_id, - }); - - var params = { - hostname: conf.API_YOUTU_SERVER, - path: '/youtu/api/delface', - method: 'POST', - headers: { - 'Authorization': sign, - 'User-Agent' : conf.USER_AGENT(), - 'Content-Length': request_body.length, - 'Content-Type': 'text/json' - } - }; - - var request = null; - if (conf.API_DOMAIN == 0) - { - request = getrequest(http, params, callback); - } - else { - request = getrequest(https, params, callback); - } - - request.on('error', function(e) { - callback({'httpcode': 0, 'code': 0, 'message':e.message, 'data': {}}); - }); - - // send the request body - request.end(request_body); -} +exports.delface = function (person_id, face_ids, callback) { + + callback = callback || cb; + + const expired = parseInt(Date.now() / 1000, 10) + EXPIRED_SECONDS; + const sign = auth.appSign(expired); + + const request_body = JSON.stringify({ + app_id: conf.APPID, + face_ids, + person_id, + }); + + const params = { + hostname: conf.API_YOUTU_SERVER, + path: '/youtu/api/delface', + method: 'POST', + headers: { + 'Authorization': sign, + 'User-Agent': conf.getAgent(), + 'Content-Length': request_body.length, + 'Content-Type': 'text/json', + }, + }; + + const request = conf.API_DOMAIN === 0 ? getrequest(http, params, callback) : getrequest(https, params, callback); + + request.on('error', function (e) { + callback({ 'httpcode': 0, 'code': 0, 'message': e.message, 'data': {}}); + }); + + // send the request body + request.end(request_body); +}; /** @@ -784,570 +580,465 @@ exports.delface = function(person_id, face_ids, callback) { * @param tag 新设置的人脸备注信息 * @param callback 回调函数, 参见Readme 文档 */ -exports.setinfo = function(person_name, person_id, tag, callback) { - - callback = callback || function(ret){console.log(ret)}; +exports.setinfo = function (person_name, person_id, tag, callback) { + + callback = callback || cb; + + const expired = parseInt(Date.now() / 1000, 10) + EXPIRED_SECONDS; + const sign = auth.appSign(expired); + + const request_body = JSON.stringify({ + app_id: conf.APPID, + person_name, + person_id, + tag, + }); + + const params = { + hostname: conf.API_YOUTU_SERVER, + path: '/youtu/api/setinfo', + method: 'POST', + headers: { + 'Authorization': sign, + 'User-Agent': conf.getAgent(), + 'Content-Length': request_body.length, + 'Content-Type': 'text/json', + }, + }; + + const request = conf.API_DOMAIN === 0 ? getrequest(http, params, callback) : getrequest(https, params, callback); + + request.on('error', function (e) { + callback({ 'httpcode': 0, 'code': 0, 'message': e.message, 'data': {}}); + }); + + // send the request body + request.end(request_body); +}; - var expired = parseInt(Date.now() / 1000) + EXPIRED_SECONDS; - var sign = auth.appSign(expired); - - var request_body = JSON.stringify({ - app_id: conf.APPID, - person_name: person_name, - person_id : person_id, - tag: tag, - }); - var buffer = new Buffer(request_body, "UTF-8"); - var params = { - hostname: conf.API_YOUTU_SERVER, - path: '/youtu/api/setinfo', - method: 'POST', - headers: { - 'Authorization': sign, - 'User-Agent' : conf.USER_AGENT(), - 'Content-Length': buffer.length, - 'Content-Type': 'text/json' - } - }; +/** + * @brief getinfo + * @param person_id 待查询的个体身份id + * @param callback 回调函数, 参见Readme 文档 + */ +exports.getinfo = function (person_id, callback) { + + callback = callback || cb; + + const expired = parseInt(Date.now() / 1000, 10) + EXPIRED_SECONDS; + const sign = auth.appSign(expired); + + const request_body = JSON.stringify({ + app_id: conf.APPID, + person_id, + }); + + const params = { + hostname: conf.API_YOUTU_SERVER, + path: '/youtu/api/getinfo', + method: 'POST', + headers: { + 'Authorization': sign, + 'User-Agent': conf.getAgent(), + 'Content-Length': request_body.length, + 'Content-Type': 'text/json', + }, + }; + + const request = conf.API_DOMAIN === 0 ? getrequest(http, params, callback) : getrequest(https, params, callback); + + request.on('error', function (e) { + callback({ 'httpcode': 0, 'code': 0, 'message': e.message, 'data': {}}); + }); + + // send the request body + request.end(request_body); +}; - var request = null; - if (conf.API_DOMAIN == 0) - { - request = getrequest(http, params, callback); - } - else { - request = getrequest(https, params, callback); - } - - request.on('error', function(e) { - callback({'httpcode': 0, 'code': 0, 'message':e.message, 'data': {}}); - }); - // send the request body - request.end(request_body); -} +/** + * @brief getgroupids + * @param callback 回调函数, 参见Readme 文档 + */ +exports.getgroupids = function (callback) { + + callback = callback || cb; + + const expired = parseInt(Date.now() / 1000, 10) + EXPIRED_SECONDS; + const sign = auth.appSign(expired); + + const request_body = JSON.stringify({ + app_id: conf.APPID, + }); + + const params = { + hostname: conf.API_YOUTU_SERVER, + path: '/youtu/api/getgroupids', + method: 'POST', + headers: { + 'Authorization': sign, + 'User-Agent': conf.getAgent(), + 'Content-Length': request_body.length, + 'Content-Type': 'text/json', + }, + }; + + const request = conf.API_DOMAIN === 0 ? getrequest(http, params, callback) : getrequest(https, params, callback); + + request.on('error', function (e) { + callback({ 'httpcode': 0, 'code': 0, 'message': e.message, 'data': {}}); + }); + + // send the request body + request.end(request_body); +}; +/** + * @brief getpersonids + * @param group_id 待查询的组id + * @param callback 回调函数, 参见Readme 文档 + */ +exports.getpersonids = function (group_id, callback) { + + callback = callback || cb; + + const expired = parseInt(Date.now() / 1000, 10) + EXPIRED_SECONDS; + const sign = auth.appSign(expired); + + + const request_body = JSON.stringify({ + app_id: conf.APPID, + group_id, + }); + + const params = { + hostname: conf.API_YOUTU_SERVER, + port: conf.API_YOUTU_PORT, + path: '/youtu/api/getpersonids', + method: 'POST', + headers: { + 'Authorization': sign, + 'User-Agent': conf.getAgent(), + 'Content-Length': request_body.length, + 'Content-Type': 'text/json', + }, + }; + + const request = conf.API_DOMAIN === 0 ? getrequest(http, params, callback) : getrequest(https, params, callback); + + request.on('error', function (e) { + callback({ 'httpcode': 0, 'code': 0, 'message': e.message, 'data': {}}); + }); + + // send the request body + request.end(request_body); +}; /** - * @brief getinfo - * @param person_id 待查询的个体身份id + * @brief getfaceids + * @param person_id 待查询的个体id * @param callback 回调函数, 参见Readme 文档 */ -exports.getinfo = function(person_id, callback) { - - callback = callback || function(ret){console.log(ret)}; +exports.getfaceids = function (person_id, callback) { + + callback = callback || cb; + + const expired = parseInt(Date.now() / 1000, 10) + EXPIRED_SECONDS; + const sign = auth.appSign(expired); + + + const request_body = JSON.stringify({ + app_id: conf.APPID, + person_id, + }); + + const params = { + hostname: conf.API_YOUTU_SERVER, + path: '/youtu/api/getfaceids', + method: 'POST', + headers: { + 'Authorization': sign, + 'User-Agent': conf.getAgent(), + 'Content-Length': request_body.length, + 'Content-Type': 'text/json', + }, + }; + + const request = conf.API_DOMAIN === 0 ? getrequest(http, params, callback) : getrequest(https, params, callback); + + request.on('error', function (e) { + callback({ 'httpcode': 0, 'code': 0, 'message': e.message, 'data': {}}); + }); - var expired = parseInt(Date.now() / 1000) + EXPIRED_SECONDS; - var sign = auth.appSign(expired); - - - var request_body = JSON.stringify({ - app_id: conf.APPID, - person_id : person_id, - }); + // send the request body + request.end(request_body); +}; - var params = { - hostname: conf.API_YOUTU_SERVER, - path: '/youtu/api/getinfo', - method: 'POST', - headers: { - 'Authorization': sign, - 'User-Agent' : conf.USER_AGENT(), - 'Content-Length': request_body.length, - 'Content-Type': 'text/json' - } - }; - var request = null; - if (conf.API_DOMAIN == 0) - { - request = getrequest(http, params, callback); - } - else { - request = getrequest(https, params, callback); - } +/** + * @brief getfaceinfo + * @param face_id 待查询的人脸id + * @param callback 回调函数, 参见Readme 文档 + */ +exports.getfaceinfo = function (face_id, callback) { + + callback = callback || cb; + + const expired = parseInt(Date.now() / 1000, 10) + EXPIRED_SECONDS; + const sign = auth.appSign(expired); + + const request_body = JSON.stringify({ + app_id: conf.APPID, + face_id, + }); + + const params = { + hostname: conf.API_YOUTU_SERVER, + path: '/youtu/api/getfaceinfo', + method: 'POST', + headers: { + 'Authorization': sign, + 'User-Agent': conf.getAgent(), + 'Content-Length': request_body.length, + 'Content-Type': 'text/json', + }, + }; + + const request = conf.API_DOMAIN === 0 ? getrequest(http, params, callback) : getrequest(https, params, callback); - request.on('error', function(e) { - callback({'httpcode': 0, 'code': 0, 'message':e.message, 'data': {}}); - }); - - // send the request body - request.end(request_body); -} + request.on('error', function (e) { + callback({ 'httpcode': 0, 'code': 0, 'message': e.message, 'data': {}}); + }); + // send the request body + request.end(request_body); +}; /** - * @brief getgroupids + * @brief fuzzydetect + * @param imagePath 待检测的路径 * @param callback 回调函数, 参见Readme 文档 */ -exports.getgroupids = function(callback) { +exports.fuzzydetect = function (imagePath, callback) { - callback = callback || function(ret){console.log(ret)}; + callback = callback || cb; - var expired = parseInt(Date.now() / 1000) + EXPIRED_SECONDS; - var sign = auth.appSign(expired); + const expired = parseInt(Date.now() / 1000, 10) + EXPIRED_SECONDS; + const sign = auth.appSign(expired); + getFile(imagePath, function (err, data, url) { + if(err) return callback({ 'httpcode': 0, 'code': -1, 'message': 'file ' + imagePath + ' not exists', 'data': {}}); - var request_body = JSON.stringify({ - app_id: conf.APPID, - }); - var params = { - hostname: conf.API_YOUTU_SERVER, - path: '/youtu/api/getgroupids', - method: 'POST', - headers: { - 'Authorization': sign, - 'User-Agent' : conf.USER_AGENT(), - 'Content-Length': request_body.length, - 'Content-Type': 'text/json' - } + const request_body = JSON.stringify({ + app_id: conf.APPID, + url: url ? data : undefined, + image: !url ? data : undefined, + }); + + const params = { + hostname: conf.API_YOUTU_SERVER, + path: '/youtu/imageapi/fuzzydetect', + method: 'POST', + headers: { + 'Authorization': sign, + 'User-Agent': conf.getAgent(), + 'Content-Length': request_body.length, + 'Content-Type': 'text/json', + }, }; - - - var request = null; - if (conf.API_DOMAIN == 0) - { - request = getrequest(http, params, callback); - } - else { - request = getrequest(https, params, callback); - } - - request.on('error', function(e) { - callback({'httpcode': 0, 'code': 0, 'message':e.message, 'data': {}}); + // debug(request_body); + debug(params); + const request = conf.API_DOMAIN === 0 ? getrequest(http, params, callback) : getrequest(https, params, callback); + + request.on('error', function (e) { + callback({ 'httpcode': 0, 'code': 0, 'message': e.message, 'data': {}}); }); - + // send the request body request.end(request_body); -} - + }); +}; /** - * @brief getpersonids - * @param group_id 待查询的组id + * @brief fooddetect + * @param imagePath 待检测的路径 * @param callback 回调函数, 参见Readme 文档 */ -exports.getpersonids = function(group_id, callback) { +exports.fooddetect = function (imagePath, callback) { - callback = callback || function(ret){console.log(ret)}; + callback = callback || cb; - var expired = parseInt(Date.now() / 1000) + EXPIRED_SECONDS; - var sign = auth.appSign(expired); - - - var request_body = JSON.stringify({ - app_id: conf.APPID, - group_id:group_id - }); + const expired = parseInt(Date.now() / 1000, 10) + EXPIRED_SECONDS; + const sign = auth.appSign(expired); + getFile(imagePath, function (err, data, url) { + if(err) return callback({ 'httpcode': 0, 'code': -1, 'message': 'file ' + imagePath + ' not exists', 'data': {}}); - var params = { - hostname: conf.API_YOUTU_SERVER, - port: conf.API_YOUTU_PORT, - path: '/youtu/api/getpersonids', - method: 'POST', - headers: { - 'Authorization': sign, - 'User-Agent' : conf.USER_AGENT(), - 'Content-Length': request_body.length, - 'Content-Type': 'text/json' - } + const request_body = JSON.stringify({ + app_id: conf.APPID, + url: url ? data : undefined, + image: !url ? data : undefined, + }); + + const params = { + hostname: conf.API_YOUTU_SERVER, + path: '/youtu/imageapi/fooddetect', + method: 'POST', + headers: { + 'Authorization': sign, + 'User-Agent': conf.getAgent(), + 'Content-Length': request_body.length, + 'Content-Type': 'text/json', + }, }; + // debug(request_body); + debug(params); + const request = conf.API_DOMAIN === 0 ? getrequest(http, params, callback) : getrequest(https, params, callback); - var request = null; - if (conf.API_DOMAIN == 0) - { - request = getrequest(http, params, callback); - } - else { - request = getrequest(https, params, callback); - } - - request.on('error', function(e) { - callback({'httpcode': 0, 'code': 0, 'message':e.message, 'data': {}}); + request.on('error', function (e) { + callback({ 'httpcode': 0, 'code': 0, 'message': e.message, 'data': {}}); }); - + // send the request body request.end(request_body); -} - - - + }); +}; /** - * @brief getfaceids - * @param person_id 待查询的个体id + * @brief imagetag + * @param imagePath 待检测的路径 * @param callback 回调函数, 参见Readme 文档 */ -exports.getfaceids = function(person_id, callback) { +exports.imagetag = function (imagePath, callback) { - callback = callback || function(ret){console.log(ret)}; + callback = callback || cb; - var expired = parseInt(Date.now() / 1000) + EXPIRED_SECONDS; - var sign = auth.appSign(expired); - + const expired = parseInt(Date.now() / 1000, 10) + EXPIRED_SECONDS; + const sign = auth.appSign(expired); + getFile(imagePath, function (err, data, url) { + if(err) return callback({ 'httpcode': 0, 'code': -1, 'message': 'file ' + imagePath + ' not exists', 'data': {}}); - var request_body = JSON.stringify({ - app_id: conf.APPID, - person_id:person_id + const request_body = JSON.stringify({ + app_id: conf.APPID, + url: url ? data : undefined, + image: !url ? data : undefined, }); - - var params = { - hostname: conf.API_YOUTU_SERVER, - path: '/youtu/api/getfaceids', - method: 'POST', - headers: { - 'Authorization': sign, - 'User-Agent' : conf.USER_AGENT(), - 'Content-Length': request_body.length, - 'Content-Type': 'text/json' - } + + const params = { + hostname: conf.API_YOUTU_SERVER, + path: '/youtu/imageapi/imagetag', + method: 'POST', + headers: { + 'Authorization': sign, + 'User-Agent': conf.getAgent(), + 'Content-Length': request_body.length, + 'Content-Type': 'text/json', + }, }; + // debug(request_body); + debug(params); + + const request = conf.API_DOMAIN === 0 ? getrequest(http, params, callback) : getrequest(https, params, callback); - var request = null; - if (conf.API_DOMAIN == 0) - { - request = getrequest(http, params, callback); - } - else { - request = getrequest(https, params, callback); - } - - request.on('error', function(e) { - callback({'httpcode': 0, 'code': 0, 'message':e.message, 'data': {}}); + request.on('error', function (e) { + callback({ 'httpcode': 0, 'code': 0, 'message': e.message, 'data': {}}); }); - + // send the request body request.end(request_body); -} - - + }); +}; /** - * @brief getfaceinfo - * @param face_id 待查询的人脸id + * @brief idcardocr + * @param imagePath 待检测的路径(本地路径或url) + * @param card_type Int 身份证图片类型,0-正面,1-反面 * @param callback 回调函数, 参见Readme 文档 */ -exports.getfaceinfo = function(face_id, callback) { +exports.idcardocr = function (imagePath, card_type, callback) { - callback = callback || function(ret){console.log(ret)}; + callback = callback || cb; - var expired = parseInt(Date.now() / 1000) + EXPIRED_SECONDS; - var sign = auth.appSign(expired); - - var request_body = JSON.stringify({ - app_id: conf.APPID, - face_id:face_id - }); + const expired = parseInt(Date.now() / 1000, 10) + EXPIRED_SECONDS; + const sign = auth.appSign(expired); + getFile(imagePath, function (err, data, url) { + if(err) return callback({ 'httpcode': 0, 'code': -1, 'message': 'file ' + imagePath + ' not exists', 'data': {}}); - var params = { - hostname: conf.API_YOUTU_SERVER, - path: '/youtu/api/getfaceinfo', - method: 'POST', - headers: { - 'Authorization': sign, - 'User-Agent' : conf.USER_AGENT(), - 'Content-Length': request_body.length, - 'Content-Type': 'text/json' - } + const request_body = JSON.stringify({ + app_id: conf.APPID, + url: url ? data : undefined, + image: !url ? data : undefined, + card_type, + }); + + const params = { + hostname: conf.API_YOUTU_SERVER, + path: '/youtu/ocrapi/idcardocr', + method: 'POST', + headers: { + 'Authorization': sign, + 'User-Agent': conf.getAgent(), + 'Content-Length': request_body.length, + 'Content-Type': 'text/json', + }, }; + + const request = conf.API_DOMAIN === 0 ? getrequest(http, params, callback) : getrequest(https, params, callback); - var request = null; - if (conf.API_DOMAIN == 0) - { - request = getrequest(http, params, callback); - } - else { - request = getrequest(https, params, callback); - } - - request.on('error', function(e) { - callback({'httpcode': 0, 'code': 0, 'message':e.message, 'data': {}}); + request.on('error', function (e) { + callback({ 'httpcode': 0, 'code': 0, 'message': e.message, 'data': {}}); }); // send the request body request.end(request_body); -} + }); +}; -/** - * @brief fuzzydetect +/* + * @brief imageporn * @param imagePath 待检测的路径 * @param callback 回调函数, 参见Readme 文档 */ -exports.fuzzydetect = function(imagePath, callback) { - - callback = callback || function(ret){console.log(ret)}; - - var expired = parseInt(Date.now() / 1000) + EXPIRED_SECONDS; - var sign = auth.appSign(expired); - var tag = imagePath.substring(0,4); - var request_body = ''; - if (tag == 'http') - { - request_body = JSON.stringify({ - app_id: conf.APPID, - url : imagePath, - }); - } - else - { - try { - var data = fs.readFileSync(imagePath).toString('base64'); - } catch (e) { - callback({'httpcode':0, 'code':-1, 'message':'file ' + imagePath + ' not exists', 'data':{}}); - return; - } - - - if(data == null) { - callback({'httpcode':0, 'code':-1, 'message':'file ' + imagePath + ' not exists or params error', 'data':{}}); - return; - }; - - request_body = JSON.stringify({ - app_id: conf.APPID, - image : data.toString('base64'), - }); - } - var params = { - hostname: conf.API_YOUTU_SERVER, - path: '/youtu/imageapi/fuzzydetect', - method: 'POST', - headers: { - 'Authorization': sign, - 'User-Agent' : conf.USER_AGENT(), - 'Content-Length': request_body.length, - 'Content-Type': 'text/json' - } - }; - //console.log(request_body); - //console.log(params); - var request = null; - if (conf.API_DOMAIN == 0) - { - request = getrequest(http, params, callback); - } - else { - request = getrequest(https, params, callback); - } - - request.on('error', function(e) { - callback({'httpcode': 0, 'code': 0, 'message':e.message, 'data': {}}); - }); - - // send the request body - request.end(request_body); -} +exports.imageporn = function (imagePath, callback) { -/** - * @brief fooddetect - * @param imagePath 待检测的路径 - * @param callback 回调函数, 参见Readme 文档 - */ -exports.fooddetect = function(imagePath, callback) { - - callback = callback || function(ret){console.log(ret)}; - - var expired = parseInt(Date.now() / 1000) + EXPIRED_SECONDS; - var sign = auth.appSign(expired); - var tag = imagePath.substring(0,4); - var request_body = ''; - if (tag == 'http') - { - request_body = JSON.stringify({ - app_id: conf.APPID, - url : imagePath, - }); - } - else - { - - try { - var data = fs.readFileSync(imagePath).toString('base64'); - } catch (e) { - callback({'httpcode':0, 'code':-1, 'message':'file ' + imagePath + ' not exists', 'data':{}}); - return; - } - if(data == null) { - callback({'httpcode':0, 'code':-1, 'message':'file ' + imagePath + ' not exists or params error', 'data':{}}); - return; - }; - - request_body = JSON.stringify({ - app_id: conf.APPID, - image : data.toString('base64'), - }); - } - var params = { - hostname: conf.API_YOUTU_SERVER, - path: '/youtu/imageapi/fooddetect', - method: 'POST', - headers: { - 'Authorization': sign, - 'User-Agent' : conf.USER_AGENT(), - 'Content-Length': request_body.length, - 'Content-Type': 'text/json' - } - }; - //console.log(request_body); - //console.log(params); - var request = null; - if (conf.API_DOMAIN == 0) - { - request = getrequest(http, params, callback); - } - else { - request = getrequest(https, params, callback); - } - - request.on('error', function(e) { - callback({'httpcode': 0, 'code': 0, 'message':e.message, 'data': {}}); - }); - - // send the request body - request.end(request_body); -} + callback = callback || cb; -/** - * @brief imagetag - * @param imagePath 待检测的路径 - * @param callback 回调函数, 参见Readme 文档 - */ -exports.imagetag = function(imagePath, callback) { - - callback = callback || function(ret){console.log(ret)}; - - var expired = parseInt(Date.now() / 1000) + EXPIRED_SECONDS; - var sign = auth.appSign(expired); - var tag = imagePath.substring(0,4); - var request_body = ''; - if (tag == 'http') - { - request_body = JSON.stringify({ - app_id: conf.APPID, - url : imagePath, - }); - } - else - { - try { - var data = fs.readFileSync(imagePath).toString('base64'); - } catch (e) { - callback({'httpcode':0, 'code':-1, 'message':'file ' + imagePath + ' not exists', 'data':{}}); - return; - } - - if(data == null) { - callback({'httpcode':0, 'code':-1, 'message':'file ' + imagePath + ' not exists or params error', 'data':{}}); - return; - }; - - request_body = JSON.stringify({ - app_id: conf.APPID, - image : data.toString('base64'), - }); - } - var params = { - hostname: conf.API_YOUTU_SERVER, - path: '/youtu/imageapi/imagetag', - method: 'POST', - headers: { - 'Authorization': sign, - 'User-Agent' : conf.USER_AGENT(), - 'Content-Length': request_body.length, - 'Content-Type': 'text/json' - } - }; - //console.log(request_body); - //console.log(params); + const expired = parseInt(Date.now() / 1000, 10) + EXPIRED_SECONDS; + const sign = auth.appSign(expired); + getFile(imagePath, function (err, data, url) { + if(err) return callback({ 'httpcode': 0, 'code': -1, 'message': 'file ' + imagePath + ' not exists', 'data': {}}); - var request = null; - if (conf.API_DOMAIN == 0) - { - request = getrequest(http, params, callback); - } - else { - request = getrequest(https, params, callback); - } - - request.on('error', function(e) { - callback({'httpcode': 0, 'code': 0, 'message':e.message, 'data': {}}); - }); - - // send the request body - request.end(request_body); -} -/** - * @brief imageporn - * @param imagePath 待检测的路径 - * @param callback 回调函数, 参见Readme 文档 - */ -exports.imageporn = function(imagePath, callback) { - - callback = callback || function(ret){console.log(ret)}; - - var expired = parseInt(Date.now() / 1000) + EXPIRED_SECONDS; - var sign = auth.appSign(expired); - var tag = imagePath.substring(0,4); - var request_body = ''; - if (tag == 'http') - { - request_body = JSON.stringify({ - app_id: conf.APPID, - url : imagePath, - }); - } - else - { - try { - var data = fs.readFileSync(imagePath).toString('base64'); - } catch (e) { - callback({'httpcode':0, 'code':-1, 'message':'file ' + imagePath + ' not exists', 'data':{}}); - return; - } - if(data == null) { - callback({'httpcode':0, 'code':-1, 'message':'file ' + imagePath + ' not exists or params error', 'data':{}}); - return; - }; - - request_body = JSON.stringify({ - app_id: conf.APPID, - image : data.toString('base64'), - }); - } - var params = { - hostname: conf.API_YOUTU_SERVER, - path: '/youtu/imageapi/imageporn', - method: 'POST', - headers: { - 'Authorization': sign, - 'User-Agent' : conf.USER_AGENT(), - 'Content-Length': request_body.length, - 'Content-Type': 'text/json' - } + const request_body = JSON.stringify({ + app_id: conf.APPID, + url: url ? data : undefined, + image: !url ? data : undefined, + }); + + const params = { + hostname: conf.API_YOUTU_SERVER, + path: '/youtu/imageapi/imageporn', + method: 'POST', + headers: { + 'Authorization': sign, + 'User-Agent': conf.getAgent(), + 'Content-Length': request_body.length, + 'Content-Type': 'text/json', + }, }; - //console.log(request_body); - //console.log(params); - - var request = null; - if (conf.API_DOMAIN == 0) - { - request = getrequest(http, params, callback); - } - else { - request = getrequest(https, params, callback); - } - - request.on('error', function(e) { - callback({'httpcode': 0, 'code': 0, 'message':e.message, 'data': {}}); + + const request = conf.API_DOMAIN === 0 ? getrequest(http, params, callback) : getrequest(https, params, callback); + + request.on('error', function (e) { + callback({ 'httpcode': 0, 'code': 0, 'message': e.message, 'data': {}}); }); - + // send the request body - request.end(request_body); -} + request.end(request_body); + }); +}; /** @@ -1356,145 +1047,87 @@ exports.imageporn = function(imagePath, callback) { * @param cardType 0 代表输入图像是身份证正面, 1代表输入是身份证反面 * @param callback 回调函数, 参见Readme 文档 */ -exports.idcardocr = function(imagePath, cardType, callback) { - - callback = callback || function(ret){console.log(ret)}; - - var expired = parseInt(Date.now() / 1000) + EXPIRED_SECONDS; - var sign = auth.appSign(expired); - var tag = imagePath.substring(0,4); - var request_body = ''; - if (tag == 'http') - { - request_body = JSON.stringify({ - app_id: conf.APPID, - card_type: cardType, - url : imagePath, - }); - } - else - { - try { - var data = fs.readFileSync(imagePath).toString('base64'); - } catch (e) { - callback({'httpcode':0, 'code':-1, 'message':'file ' + imagePath + ' not exists', 'data':{}}); - return; - } - if(data == null) { - callback({'httpcode':0, 'code':-1, 'message':'file ' + imagePath + ' not exists or params error', 'data':{}}); - return; - }; - - request_body = JSON.stringify({ - app_id: conf.APPID, - card_type: cardType, - image : data.toString('base64'), - }); - } - var params = { - hostname: conf.API_YOUTU_SERVER, - path: '/youtu/ocrapi/idcardocr', - method: 'POST', - headers: { - 'Authorization': sign, - 'User-Agent' : conf.USER_AGENT(), - 'Content-Length': request_body.length, - 'Content-Type': 'text/json' - } +exports.idcardocr = function (imagePath, cardType, callback) { + + callback = callback || cb; + + const expired = parseInt(Date.now() / 1000, 10) + EXPIRED_SECONDS; + const sign = auth.appSign(expired); + getFile(imagePath, function (err, data, url) { + if(err) return callback({ 'httpcode': 0, 'code': -1, 'message': 'file ' + imagePath + ' not exists', 'data': {}}); + + const request_body = JSON.stringify({ + app_id: conf.APPID, + card_type: cardType, + url: url ? data : undefined, + image: !url ? data : undefined, + }); + + const params = { + hostname: conf.API_YOUTU_SERVER, + path: '/youtu/ocrapi/idcardocr', + method: 'POST', + headers: { + 'Authorization': sign, + 'User-Agent': conf.getAgent(), + 'Content-Length': request_body.length, + 'Content-Type': 'text/json', + }, }; - //console.log(request_body); - //console.log(params); - - var request = null; - if (conf.API_DOMAIN == 0) - { - request = getrequest(http, params, callback); - } - else { - request = getrequest(https, params, callback); - } - - request.on('error', function(e) { - callback({'httpcode': 0, 'code': 0, 'message':e.message, 'data': {}}); + + const request = conf.API_DOMAIN === 0 ? getrequest(http, params, callback) : getrequest(https, params, callback); + + request.on('error', function (e) { + callback({ 'httpcode': 0, 'code': 0, 'message': e.message, 'data': {}}); }); - + // send the request body - request.end(request_body); -} + request.end(request_body); + }); +}; /** * @brief namecardocr - * @param imagePath 待检测的路径 - * @param retImage false 代表不需要返回识别后图像, true 代表需要返回识别后图像 + * @param imagePath 待检测的路径(本地路径或url) + * @param retimage 是否需要返回处理结果图,true返回,false不返回 * @param callback 回调函数, 参见Readme 文档 */ -exports.namecardocr = function(imagePath, retImage, callback) { - - callback = callback || function(ret){console.log(ret)}; - - var expired = parseInt(Date.now() / 1000) + EXPIRED_SECONDS; - var sign = auth.appSign(expired); - var tag = imagePath.substring(0,4); - var request_body = ''; - if (tag == 'http') - { - request_body = JSON.stringify({ - app_id: conf.APPID, - retimage: retImage, - url : imagePath, - }); - } - else - { - try { - var data = fs.readFileSync(imagePath).toString('base64'); - } catch (e) { - callback({'httpcode':0, 'code':-1, 'message':'file ' + imagePath + ' not exists', 'data':{}}); - return; - } - - if(data == null) { - callback({'httpcode':0, 'code':-1, 'message':'file ' + imagePath + ' not exists or params error', 'data':{}}); - return; - }; - - request_body = JSON.stringify({ - app_id: conf.APPID, - retimage: retImage, - image : data.toString('base64'), - }); - } - var params = { - //hostname: conf.API_YOUTU_SERVER, - host: '101.226.76.164', - port: 18082, - path:'/youtu/ocrapi/namecardocr', - method: 'POST', - headers: { - 'Authorization': sign, - 'User-Agent' : conf.USER_AGENT(), - 'Content-Length': request_body.length, - 'Content-Type': 'text/json' - } - }; - //console.log(request_body); - //console.log(params); +exports.namecardocr = function (imagePath, retimage, callback) { + + callback = callback || cb; + + const expired = parseInt(Date.now() / 1000, 10) + EXPIRED_SECONDS; + const sign = auth.appSign(expired); + getFile(imagePath, function (err, data, url) { + if(err) return callback({ 'httpcode': 0, 'code': -1, 'message': 'file ' + imagePath + ' not exists', 'data': {}}); - var request = null; - if (conf.API_DOMAIN == 0) - { - request = getrequest(http, params, callback); - } - else { - request = getrequest(https, params, callback); - } - - request.on('error', function(e) { - callback({'httpcode': 0, 'code': 0, 'message':e.message, 'data': {}}); + + const request_body = JSON.stringify({ + app_id: conf.APPID, + url: url ? data : undefined, + image: !url ? data : undefined, + retimage, }); - - // send the request body - request.end(request_body); -} + + const params = { + hostname: conf.API_YOUTU_SERVER, + path: '/youtu/ocrapi/namecardocr', + method: 'POST', + headers: { + 'Authorization': sign, + 'User-Agent': conf.getAgent(), + 'Content-Length': request_body.length, + 'Content-Type': 'text/json', + }, + }; + + const request = conf.API_DOMAIN === 0 ? getrequest(http, params, callback) : getrequest(https, params, callback); + request.on('error', function (e) { + callback({ 'httpcode': 0, 'code': 0, 'message': e.message, 'data': {}}); + }); + // send the request body + request.end(request_body); + }); +};