nodejs實現(xiàn)百度輿情接口應(yīng)用示例
更新時間:2020年02月07日 11:17:20 作者:李瓊濤
這篇文章主要介紹了nodejs實現(xiàn)百度輿情接口應(yīng)用,結(jié)合實例形式分析了node.js調(diào)用百度輿情接口的具體使用技巧,需要的朋友可以參考下
本文實例講述了nodejs實現(xiàn)百度輿情接口。分享給大家供大家參考,具體如下:
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程序設(shè)計有所幫助。
您可能感興趣的文章:
- node.js集成百度UE編輯器
- Node.js編寫爬蟲的基本思路及抓取百度圖片的實例分享
- Node Puppeteer圖像識別實現(xiàn)百度指數(shù)爬蟲的示例
- nodejs根據(jù)ip數(shù)組在百度地圖中進(jìn)行定位
- 微信小程序訪問node.js接口服務(wù)器搭建教程
- 詳解基于Node.js的微信JS-SDK后端接口實現(xiàn)代碼
- Node.js 實現(xiàn)簡單的接口服務(wù)器的實例代碼
- 用NodeJS實現(xiàn)批量查詢地理位置的經(jīng)緯度接口
- 詳解nodejs微信jssdk后端接口
- 用Node編寫RESTful API接口的示例代碼
- node 文件上傳接口的轉(zhuǎn)發(fā)的實現(xiàn)
- 配置node服務(wù)器并且鏈接微信公眾號接口配置步驟詳解
相關(guān)文章
node.js中 mysql 增刪改查操作及async,await處理實例分析
這篇文章主要介紹了node.js中 mysql 增刪改查操作及async,await處理,結(jié)合實例形式分析了node.js中 mysql庫安裝、增刪改查操作及async,await處理相關(guān)實現(xiàn)技巧,需要的朋友可以參考下2020-02-02
package-lock.json解決依賴的版本管理使用詳解
這篇文章主要為大家介紹了package-lock.json解決依賴的版本管理使用詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-08-08
node.js通過axios實現(xiàn)網(wǎng)絡(luò)請求的方法
下面小編就為大家分享一篇node.js通過axios實現(xiàn)網(wǎng)絡(luò)請求的方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-03-03

