nodejs实现百度舆情接口应用示例
程序员文章站
2022-05-14 15:13:51
本文实例讲述了nodejs实现百度舆情接口。分享给大家供大家参考,具体如下:const url = require('url');const http = require('http');const...
本文实例讲述了nodejs实现百度舆情接口。分享给大家供大家参考,具体如下:
const url = require('url'); const http = require('http'); const https = require('https'); const qs = require('querystring'); let trends = exports; trends.getinstance = function () { return new trends; } function trends() { this.expiretime = 1800; this.accesskey = 'xxxxxxxx'; this.secretkey = 'xxxxxxxx'; this.userkey = 'xxxxxxxx'; this.usersecret = 'xxxxxxxx'; this.host = 'trends.baidubce.com'; this.timestamp = _.time(); this.utctimestamp = _.utctime(); } trends.prototype.request = async function (method, uri, params) { method = method.touppercase(); let token = this.gettoken(); let headers = this.getheaders(method, uri); params = object.assign({}, params || {}, { 'user_key': this.userkey, 'token': token, 'timestamp': this.timestamp }); let url = `http://${this.host}${uri}`; return await this.httprequest(method, url, params, headers); } trends.prototype.getheaders = function (method, uri) { let authorization = this.getauthorization(method, uri); return { 'content-type': 'application/x-www-form-urlencoded', 'host': this.host, 'x-bce-date': this.utctimestamp, 'authorization': authorization, }; } trends.prototype.getauthorization = function (method, uri) { let authstring = `bce-auth-v1/${this.accesskey}/${this.utctimestamp}/${this.expiretime}`; let signinkey = _.hmac(authstring, this.secretkey, 'sha256'); let header = { 'host': this.host, 'x-bce-date': _.urlencode(this.utctimestamp) }; let headerarr = []; for (let name in header) { let val = header[name]; headerarr.push(`${name}:${val}`); } let headerkeystr = object.keys(header).sort().join(';'); let requeststr = `${method}\n${uri}\n\n${headerarr.join('\n')}`; let signautre = _.hmac(requeststr, signinkey, 'sha256'); return `${authstring}/${headerkeystr}/${signautre}`; } trends.prototype.gettoken = function () { return _.hmac(this.userkey + this.timestamp, this.usersecret); } trends.prototype.httprequest = async function (method, url, params, headers) { let urlobj = url.parse(url); let protocol = urlobj.protocol; let options = { hostname: urlobj.hostname, port: urlobj.port, path: urlobj.path, method: method, headers: headers, timeout: 10000, }; let postdata = qs.stringify(params || {}); return new promise((resolve, reject) => { let req = (protocol == 'http:' ? http : https).request(options, (res) => { let chunks = []; res.on('data', (data) => { chunks.push(data); }); res.on('end', () => { let buffer = buffer.concat(chunks); let encoding = res.headers['content-encoding']; if (encoding == 'gzip') { zlib.unzip(buffer, function (err, decoded) { resolve(decoded.tostring()); }); } else if (encoding == 'deflate') { zlib.inflate(buffer, function (err, decoded) { resolve(decoded.tostring()); }); } else { resolve(buffer.tostring()); } }); }); req.on('error', (e) => { _.error('request error', method, url, params, e); resolve(''); }); req.on("timeout", (e) => { _.error('request timeout', method, url, params, e); resolve(''); }) if (method.touppercase() == 'post') { req.write(postdata); } req.end(); }); }
module.exports = function () { return new script; } function script() {} script.prototype.run = async function () { let rst = this.gettasklist(); console.log(rst); } script.prototype.gettasklist = async function () { let params = {}; let method = 'post'; let uri = '/openapi/gettasklist'; let rst = await _.trends.getinstance().request(method, uri, params); return rst; }
希望本文所述对大家node.js程序设计有所帮助。