From 6d22c166c6065541d0d38c3372818a101edcc92e Mon Sep 17 00:00:00 2001 From: YourtionGuo Date: Sun, 29 Nov 2015 23:13:37 +0800 Subject: [PATCH 01/10] add `debug` --- package.json | 3 ++- tencentyoutuyun/youtu.js | 32 ++++++++++++++++---------------- 2 files changed, 18 insertions(+), 17 deletions(-) diff --git a/package.json b/package.json index 23911ec..16b9280 100644 --- a/package.json +++ b/package.json @@ -24,6 +24,7 @@ "node >= 0.4.7" ], "dependencies": { - "iconv-lite": "^0.4.13" + "iconv-lite": "^0.4.13", + "debug": "^2.2.0" } } diff --git a/tencentyoutuyun/youtu.js b/tencentyoutuyun/youtu.js index b86bac7..bf614f1 100644 --- a/tencentyoutuyun/youtu.js +++ b/tencentyoutuyun/youtu.js @@ -2,7 +2,7 @@ var http = require('http'); var https = require('https'); var fs = require('fs'); var conv = require('iconv-lite') - +var debug = require('debug')('YouTu:'); var auth = require('./auth'); var conf = require('./conf'); @@ -41,8 +41,8 @@ function getrequest(protocol, params, callback) { return protocol.request(params, function(response) { - // console.log('STATUS: ' + response.statusCode); - // console.log('HEADERS: ' + JSON.stringify(response.headers)); + debug('STATUS: ' + response.statusCode); + debug('HEADERS: ' + JSON.stringify(response.headers)); if( response.statusCode != 200 ){ callback({'httpcode':response.statusCode, 'code':response.statusCode , 'message':statusText(response.statusCode) , 'data':{}}); @@ -114,8 +114,8 @@ exports.detectface = function(imagePath, isbigface, callback) { } }; - //console.log(request_body); - //console.log(params); + // debug(request_body); + debug(params); var request = null; if (conf.API_DOMAIN == 0) { @@ -131,7 +131,7 @@ exports.detectface = function(imagePath, isbigface, callback) { // send the request body request.end(request_body); - //console.log(request_body); + // debug(request_body); } @@ -253,7 +253,7 @@ exports.facecompare = function(image_a, image_b, callback) { } }; - //console.log(request_body); + // debug(request_body); var request = null; if (conf.API_DOMAIN == 0) { @@ -323,7 +323,7 @@ exports.faceverify = function(imagePath, person_id, callback) { } }; - //console.log(request_body); + // debug(request_body); var request = null; if (conf.API_DOMAIN == 0) { @@ -592,7 +592,7 @@ exports.delperson= function(person_id, callback) { }); } - //console.log(request_body); + // debug(request_body); var buffer = new Buffer(request_body, "UTF-8"); var params = { hostname: conf.API_YOUTU_SERVER, @@ -606,7 +606,7 @@ exports.delperson= function(person_id, callback) { } }; - //console.log(params); + debug(params); var request = null; if (conf.API_DOMAIN == 0) { @@ -1014,8 +1014,8 @@ exports.fuzzydetect = function(imagePath, callback) { 'Content-Type': 'text/json' } }; - //console.log(request_body); - //console.log(params); + // debug(request_body); + debug(params); var request = null; if (conf.API_DOMAIN == 0) { @@ -1078,8 +1078,8 @@ exports.fooddetect = function(imagePath, callback) { 'Content-Type': 'text/json' } }; - //console.log(request_body); - //console.log(params); + // debug(request_body); + debug(params); var request = null; if (conf.API_DOMAIN == 0) { @@ -1142,8 +1142,8 @@ exports.imagetag = function(imagePath, callback) { 'Content-Type': 'text/json' } }; - //console.log(request_body); - //console.log(params); + // debug(request_body); + debug(params); var request = null; if (conf.API_DOMAIN == 0) From 98827724a40f25d9f7bf38953e1d3bcf0ef96f47 Mon Sep 17 00:00:00 2001 From: YourtionGuo Date: Sat, 19 Dec 2015 13:53:18 +0800 Subject: [PATCH 02/10] update --- package.json | 1 + 1 file changed, 1 insertion(+) diff --git a/package.json b/package.json index 16b9280..60a830f 100644 --- a/package.json +++ b/package.json @@ -4,6 +4,7 @@ "description": "node sdk for qcloud youtu", "main": "index.js", "scripts": { + "sample": "export DEBUG="YouTu:*" && node sample/sample.js", "test": "echo \"Error: no test specified\" && exit 1" }, "repository": { From e03d1faa627f2e666492951ead0661f887c388f5 Mon Sep 17 00:00:00 2001 From: YourtionGuo Date: Fri, 27 May 2016 10:51:13 +0800 Subject: [PATCH 03/10] bug fix --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 60a830f..400556e 100644 --- a/package.json +++ b/package.json @@ -4,7 +4,7 @@ "description": "node sdk for qcloud youtu", "main": "index.js", "scripts": { - "sample": "export DEBUG="YouTu:*" && node sample/sample.js", + "sample": "export DEBUG='YouTu:*' && node sample/sample.js", "test": "echo \"Error: no test specified\" && exit 1" }, "repository": { From 4940be4a80c883efa54851f1bbf75a2a4bd8d38b Mon Sep 17 00:00:00 2001 From: YourtionGuo Date: Fri, 27 May 2016 10:51:34 +0800 Subject: [PATCH 04/10] idcardocr --- tencentyoutuyun/youtu.js | 66 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 66 insertions(+) diff --git a/tencentyoutuyun/youtu.js b/tencentyoutuyun/youtu.js index bf614f1..91dcade 100644 --- a/tencentyoutuyun/youtu.js +++ b/tencentyoutuyun/youtu.js @@ -1161,3 +1161,69 @@ exports.imagetag = function(imagePath, callback) { // send the request body request.end(request_body); } + +/** + * @brief idcardocr + * @param imagePath 待检测的路径(本地路径或url) + * @param card_type Int 身份证图片类型,0-正面,1-反面 + * @param callback 回调函数, 参见Readme 文档 + */ +exports.idcardocr = function(imagePath, card_type, 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, + card_type : card_type, + }); + } + else + { + var data = fs.readFileSync(imagePath).toString('base64'); + + 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'), + card_type : card_type, + }); + } + 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' + } + }; + + 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); +} From e523c5c764d987adbb48b42f0cf8d5533424be7e Mon Sep 17 00:00:00 2001 From: YourtionGuo Date: Fri, 27 May 2016 10:51:50 +0800 Subject: [PATCH 05/10] namecardocr --- tencentyoutuyun/youtu.js | 66 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 66 insertions(+) diff --git a/tencentyoutuyun/youtu.js b/tencentyoutuyun/youtu.js index 91dcade..6d100e9 100644 --- a/tencentyoutuyun/youtu.js +++ b/tencentyoutuyun/youtu.js @@ -1227,3 +1227,69 @@ exports.idcardocr = function(imagePath, card_type, callback) { // send the request body request.end(request_body); } + +/** + * @brief namecardocr + * @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, + url : imagePath, + retimage : retimage, + }); + } + else + { + var data = fs.readFileSync(imagePath).toString('base64'); + + 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'), + retimage : retimage, + }); + } + var params = { + hostname: conf.API_YOUTU_SERVER, + path: '/youtu/ocrapi/namecardocr', + 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); +} From 1a86de74daf74e39b4eb51c66b2442fe01dc9c78 Mon Sep 17 00:00:00 2001 From: YourtionGuo Date: Fri, 27 May 2016 10:55:12 +0800 Subject: [PATCH 06/10] sample --- sample/sample.js | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/sample/sample.js b/sample/sample.js index 07eaff8..97040d9 100644 --- a/sample/sample.js +++ b/sample/sample.js @@ -34,3 +34,13 @@ youtu.imagetag('a.jpg', function(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)); +//}) + From a118f82b4d37d9d58c60641ba24525b6796bf7ed Mon Sep 17 00:00:00 2001 From: YourtionGuo Date: Sun, 21 Aug 2016 12:38:20 +0800 Subject: [PATCH 07/10] update package --- package.json | 12 ++++++------ run | 2 ++ 2 files changed, 8 insertions(+), 6 deletions(-) create mode 100755 run diff --git a/package.json b/package.json index 400556e..8c3d8f5 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { - "name": "tencentyoutuyun", + "name": "youtusdk", "version": "1.0.0", - "description": "node sdk for qcloud youtu", + "description": "腾讯优图开放平台第三方SDK", "main": "index.js", "scripts": { "sample": "export DEBUG='YouTu:*' && node sample/sample.js", @@ -9,18 +9,18 @@ }, "repository": { "type": "git", - "url": "git+https://github.com/TencentYouTu/nodejs_sdk.git" + "url": "https://github.com/yourtion/youtu_nodejs_sdk" }, "keywords": [ "qcloud", "youtu" ], - "author": "tencentyoutuyun", + "author": "YourtionGuo ", "license": "MIT", "bugs": { - "url": "https://github.com/TencentYouTu/nodejs_sdk/issues" + "url": "https://github.com/yourtion/youtu_nodejs_sdk/issues" }, - "homepage": "https://github.com/TencentYouTu/nodejs_sdk#readme", + "homepage": "https://github.com/yourtion/youtu_nodejs_sdk#readme", "engines": [ "node >= 0.4.7" ], 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 From e232828a2eccde3da87338b7d34742c6eab91f19 Mon Sep 17 00:00:00 2001 From: YourtionGuo Date: Sun, 3 Dec 2017 11:03:10 +0800 Subject: [PATCH 08/10] update 1.1 --- package.json | 12 ++++-------- sample/sample.js | 14 +++++++------- 2 files changed, 11 insertions(+), 15 deletions(-) diff --git a/package.json b/package.json index 8c3d8f5..75ba992 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "youtusdk", - "version": "1.0.0", + "version": "1.1.0", "description": "腾讯优图开放平台第三方SDK", "main": "index.js", "scripts": { @@ -9,7 +9,7 @@ }, "repository": { "type": "git", - "url": "https://github.com/yourtion/youtu_nodejs_sdk" + "url": "https://github.com/yourtion/node-youtu" }, "keywords": [ "qcloud", @@ -18,14 +18,10 @@ "author": "YourtionGuo ", "license": "MIT", "bugs": { - "url": "https://github.com/yourtion/youtu_nodejs_sdk/issues" + "url": "https://github.com/yourtion/node-youtu/issues" }, - "homepage": "https://github.com/yourtion/youtu_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" } } diff --git a/sample/sample.js b/sample/sample.js index 9f9e7c4..9c6b114 100644 --- a/sample/sample.js +++ b/sample/sample.js @@ -3,17 +3,17 @@ var conf = tencentyoutuyun.conf; var youtu = tencentyoutuyun.youtu; // 设置开发者和应用信息, 请填写你在开放平台 -var appid = '****'; -var secretId = '****'; -var secretKey = '****'; -var userid = '****'; +var appid = '1006920'; +var secretId = 'AKIDZ6O9LK4HfRgY1EAcJincpUu27iQdn99A'; +var secretKey = 'EU1TBKJfb0zRLFrpT7FuD0JMXBGIy2Yz'; +var userid = '1001'; conf.setAppInfo(appid, secretId, secretKey, userid, 0) // 人脸检测 测试 -// youtu.detectface('a.jpg', 0, function(data){ -// console.log("detectface:" + JSON.stringify(data)); -// }); +youtu.detectface('knowme.png', 0, function(data){ + console.log("detectface:" + JSON.stringify(data)); +}); // 人脸比对 测试 // youtu.facecompare('a.jpg', 'a.jpg', function(data){ From 06c018b1a128bd5fe43103498fcc8ffcb655a875 Mon Sep 17 00:00:00 2001 From: YourtionGuo Date: Sun, 3 Dec 2017 11:10:21 +0800 Subject: [PATCH 09/10] update --- sample/sample.js | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/sample/sample.js b/sample/sample.js index 9c6b114..9f9e7c4 100644 --- a/sample/sample.js +++ b/sample/sample.js @@ -3,17 +3,17 @@ var conf = tencentyoutuyun.conf; var youtu = tencentyoutuyun.youtu; // 设置开发者和应用信息, 请填写你在开放平台 -var appid = '1006920'; -var secretId = 'AKIDZ6O9LK4HfRgY1EAcJincpUu27iQdn99A'; -var secretKey = 'EU1TBKJfb0zRLFrpT7FuD0JMXBGIy2Yz'; -var userid = '1001'; +var appid = '****'; +var secretId = '****'; +var secretKey = '****'; +var userid = '****'; conf.setAppInfo(appid, secretId, secretKey, userid, 0) // 人脸检测 测试 -youtu.detectface('knowme.png', 0, function(data){ - console.log("detectface:" + JSON.stringify(data)); -}); +// youtu.detectface('a.jpg', 0, function(data){ +// console.log("detectface:" + JSON.stringify(data)); +// }); // 人脸比对 测试 // youtu.facecompare('a.jpg', 'a.jpg', function(data){ From 485b72b84f5729181ef6ec153ae5ed9cbc5d5bfb Mon Sep 17 00:00:00 2001 From: YourtionGuo Date: Mon, 4 Dec 2017 00:01:23 +0800 Subject: [PATCH 10/10] eslint and async file read --- .eslintrc.js | 6 + package.json | 9 +- sample/sample.js | 45 +- tencentyoutuyun/auth.js | 49 +- tencentyoutuyun/conf.js | 52 +- tencentyoutuyun/youtu.js | 2044 +++++++++++++++----------------------- 6 files changed, 895 insertions(+), 1310 deletions(-) create mode 100644 .eslintrc.js 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 75ba992..9487037 100644 --- a/package.json +++ b/package.json @@ -1,11 +1,12 @@ { "name": "youtusdk", - "version": "1.1.0", + "version": "2.0.0", "description": "腾讯优图开放平台第三方SDK", "main": "index.js", "scripts": { "sample": "export DEBUG='YouTu:*' && node sample/sample.js", - "test": "echo \"Error: no test specified\" && exit 1" + "test": "echo \"Error: no test specified\" && exit 1", + "lint": "eslint tencentyoutuyun sample --fix" }, "repository": { "type": "git", @@ -23,5 +24,9 @@ "homepage": "https://github.com/yourtion/node-youtu#readme", "dependencies": { "debug": "^2.2.0" + }, + "devDependencies": { + "eslint": "^4.12.1", + "eslint-config-guo": "^0.6.0" } } diff --git a/sample/sample.js b/sample/sample.js index 9f9e7c4..bc617bc 100644 --- a/sample/sample.js +++ b/sample/sample.js @@ -1,18 +1,19 @@ -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.detectface('a.jpg', 0, function(data){ -// console.log("detectface:" + JSON.stringify(data)); +// youtu.detectface('a.jpg', 0, function (data){ +// console.log('detectface:' + JSON.stringify(data)); // }); // 人脸比对 测试 @@ -34,25 +35,25 @@ conf.setAppInfo(appid, secretId, secretKey, userid, 0) // }); -//youtu.imageporn('a1.jpg', function(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){ +// 身份证OCR识别 +// youtu.idcardocr('sfz3.jpg',0,function(data){ // console.log("fuzzydetect:" + JSON.stringify(data)); -//}) +// }) -//身份证OCR识别 -//youtu.namecardocr('mp2.jpg',true,function(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 9201630..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'); -var conv = require('iconv-lite') -var debug = require('debug')('YouTu:'); +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) { + + return protocol.request(params, function (response) { + + debug('STATUS: ' + response.statusCode); + debug('HEADERS: ' + JSON.stringify(response.headers)); -function getrequest(protocol, params, callback) { + if(response.statusCode !== 200) { + callback({ 'httpcode': response.statusCode, 'code': response.statusCode, 'message': statusText(response.statusCode), 'data': {}}); + return; + } - return protocol.request(params, function(response) { + let body = ''; + response.setEncoding('utf8'); - debug('STATUS: ' + response.statusCode); - debug('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': {}}); + + 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', + }, }; // debug(request_body); debug(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); // 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' - } - }; - - // debug(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 = ''; - - 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, - }); - } + 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/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', + }, }; - + // debug(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': {}}); - }); + 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)}; +exports.delperson = function (person_id, callback) { - 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, - }); + callback = callback || cb; - 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' - } - }; + 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, + }); - 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 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', + }, + }; - // send the request body - request.end(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); +}; /** @@ -646,86 +461,74 @@ exports.delperson= function(person_id, callback) { * @param facetag 人脸备注信息,用户自解释字段 * @param callback 回调函数, 参见Readme 文档 */ - exports.addface = function(person_id, images, facetag, callback) { +exports.addface = function (person_id, images, facetag, 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 = 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 - }); - } - - // debug(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' - } - }; + 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; + } - debug(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': {}}); + image_bufs[idx] = data.toString('base64'); + } + + 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', + }, + }; - // send the request body - request.end(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); + +}; /** @@ -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) { +exports.delface = function (person_id, face_ids, 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_ids: face_ids, - person_id : person_id, - }); + 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, + }); - 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' - } - }; + 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', + }, + }; - 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); -} + // send the request body + request.end(request_body); +}; /** @@ -784,51 +580,41 @@ 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)}; - - 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, - }); +exports.setinfo = function (person_name, person_id, tag, callback) { - 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' - } - }; + callback = callback || cb; - 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 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, + }); - // send the request body - request.end(request_body); -} + 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); +}; /** @@ -836,93 +622,77 @@ exports.setinfo = function(person_name, person_id, tag, callback) { * @param person_id 待查询的个体身份id * @param callback 回调函数, 参见Readme 文档 */ -exports.getinfo = function(person_id, callback) { +exports.getinfo = function (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 request_body = JSON.stringify({ - app_id: conf.APPID, - person_id : person_id, - }); + const expired = parseInt(Date.now() / 1000, 10) + EXPIRED_SECONDS; + const sign = auth.appSign(expired); - 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' - } - }; + const request_body = JSON.stringify({ + app_id: conf.APPID, + person_id, + }); - 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 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', + }, + }; - // send the request body - request.end(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); +}; /** * @brief getgroupids * @param callback 回调函数, 参见Readme 文档 */ -exports.getgroupids = function(callback) { +exports.getgroupids = function (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, - }); + callback = callback || cb; - 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 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', + }, + }; - 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); -} + // send the request body + request.end(request_body); +}; /** @@ -930,50 +700,41 @@ exports.getgroupids = function(callback) { * @param group_id 待查询的组id * @param callback 回调函数, 参见Readme 文档 */ -exports.getpersonids = function(group_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, - group_id:group_id - }); +exports.getpersonids = function (group_id, callback) { - 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' - } - }; + callback = callback || cb; - 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 expired = parseInt(Date.now() / 1000, 10) + EXPIRED_SECONDS; + const sign = auth.appSign(expired); + + + const request_body = JSON.stringify({ + app_id: conf.APPID, + group_id, + }); - // send the request body - request.end(request_body); -} + 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); +}; /** @@ -981,48 +742,40 @@ exports.getpersonids = function(group_id, callback) { * @param person_id 待查询的个体id * @param callback 回调函数, 参见Readme 文档 */ -exports.getfaceids = function(person_id, callback) { +exports.getfaceids = function (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 request_body = JSON.stringify({ - app_id: conf.APPID, - person_id:person_id - }); + 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, + }); - 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/api/getfaceids', + 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); - } + 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); +}; /** @@ -1030,255 +783,173 @@ exports.getfaceids = function(person_id, callback) { * @param face_id 待查询的人脸id * @param callback 回调函数, 参见Readme 文档 */ -exports.getfaceinfo = function(face_id, callback) { +exports.getfaceinfo = function (face_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 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); + + const request_body = JSON.stringify({ + app_id: conf.APPID, + face_id, + }); - 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 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', + }, + }; - var request = null; - if (conf.API_DOMAIN == 0) - { - request = getrequest(http, params, callback); - } - else { - request = getrequest(https, params, callback); - } + 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': {}}); - }); + 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); +}; /** * @brief fuzzydetect * @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' - } +exports.fuzzydetect = function (imagePath, 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, + }); + + 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', + }, }; // debug(request_body); debug(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 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' - } +exports.fooddetect = function (imagePath, 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, + }); + + 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); - 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 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' - } +exports.imagetag = function (imagePath, 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, + }); + + 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); - - 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 idcardocr @@ -1286,134 +957,88 @@ exports.imagetag = function(imagePath, callback) { * @param card_type Int 身份证图片类型,0-正面,1-反面 * @param callback 回调函数, 参见Readme 文档 */ -exports.idcardocr = function(imagePath, card_type, 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 tag = imagePath.substring(0,4); - var request_body = ''; - if (tag == 'http') - { - request_body = JSON.stringify({ + 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 : imagePath, - card_type : card_type, + url: url ? data : undefined, + image: !url ? data : undefined, + card_type, }); - } - else - { - var data = fs.readFileSync(imagePath).toString('base64'); - - if(data == null) { - callback({'httpcode':0, 'code':-1, 'message':'file ' + imagePath + ' not exists or params error', 'data':{}}); - return; + + 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); - request_body = JSON.stringify({ - app_id: conf.APPID, - image : data.toString('base64'), - card_type : card_type, + request.on('error', function (e) { + callback({ 'httpcode': 0, 'code': 0, 'message': e.message, 'data': {}}); }); - } - 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' - } - }; - - 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); -} + // 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' - } - }; - //console.log(request_body); - //console.log(params); +exports.imageporn = function (imagePath, 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, }); - + + 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', + }, + }; + + 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); + }); +}; /** @@ -1422,71 +1047,44 @@ 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 @@ -1494,62 +1092,42 @@ exports.idcardocr = function(imagePath, cardType, callback) { * @param retimage 是否需要返回处理结果图,true返回,false不返回 * @param callback 回调函数, 参见Readme 文档 */ -exports.namecardocr = function(imagePath, retimage, callback) { +exports.namecardocr = function (imagePath, retimage, 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, - retimage : retimage, - }); - } - else - { - var data = fs.readFileSync(imagePath).toString('base64'); + 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(data == null) { - callback({'httpcode':0, 'code':-1, 'message':'file ' + imagePath + ' not exists or params error', 'data':{}}); - return; - }; - request_body = JSON.stringify({ + const request_body = JSON.stringify({ app_id: conf.APPID, - image : data.toString('base64'), - retimage : retimage, + url: url ? data : undefined, + image: !url ? data : undefined, + retimage, }); - } - var params = { - hostname: conf.API_YOUTU_SERVER, - path: '/youtu/ocrapi/namecardocr', - 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': {}}); - }); + 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); - // 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); + }); +};