欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

nodejs實(shí)現(xiàn)百度輿情接口應(yīng)用示例

 更新時(shí)間:2020年02月07日 11:17:20   作者:李瓊濤  
這篇文章主要介紹了nodejs實(shí)現(xiàn)百度輿情接口應(yīng)用,結(jié)合實(shí)例形式分析了node.js調(diào)用百度輿情接口的具體使用技巧,需要的朋友可以參考下

本文實(shí)例講述了nodejs實(shí)現(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;
}

希望本文所述對(duì)大家node.js程序設(shè)計(jì)有所幫助。

相關(guān)文章

  • node.js中使用node-schedule實(shí)現(xiàn)定時(shí)任務(wù)實(shí)例

    node.js中使用node-schedule實(shí)現(xiàn)定時(shí)任務(wù)實(shí)例

    這篇文章主要介紹了node.js中使用node-schedule實(shí)現(xiàn)定時(shí)任務(wù)實(shí)例,包括安裝方法和4種使用例子,需要的朋友可以參考下
    2014-06-06
  • node中使用log4js4.x版本記錄日志的方法

    node中使用log4js4.x版本記錄日志的方法

    這篇文章主要介紹了node中使用log4js4.x版本記錄日志的方法,本文給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2019-08-08
  • Node.js中path.join()優(yōu)勢(shì)例舉分析

    Node.js中path.join()優(yōu)勢(shì)例舉分析

    在本篇文章里小編給大家整理的是一篇關(guān)于Node.js中path.join()優(yōu)勢(shì)例舉分析,有興趣的朋友們可以學(xué)習(xí)下。
    2021-08-08
  • npx的使用及原理分析

    npx的使用及原理分析

    這篇文章主要介紹了npx的使用及原理,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2024-02-02
  • Node.js Addons翻譯(C/C++擴(kuò)展)

    Node.js Addons翻譯(C/C++擴(kuò)展)

    這篇文章主要介紹了Node.js Addons翻譯(C/C++擴(kuò)展) 的相關(guān)資料,非常不錯(cuò)具有參考借鑒價(jià)值,需要的朋友可以參考下
    2016-06-06
  • 淺談手寫node可讀流之流動(dòng)模式

    淺談手寫node可讀流之流動(dòng)模式

    這篇文章主要介紹了淺談手寫node可讀流之流動(dòng)模式,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2018-06-06
  • 詳解如何給Node.js版本降級(jí)

    詳解如何給Node.js版本降級(jí)

    Node.js是一個(gè)基于chrome?v8引擎的JavaScript運(yùn)行時(shí)環(huán)境,用于構(gòu)建快速、可擴(kuò)展的網(wǎng)絡(luò)應(yīng)用程序,在某些情況下,降級(jí)Node.js版本可能額是必要的,本篇文章將向您介紹如今降級(jí)Node.js版本并提供相應(yīng)的源代碼示例,需要的朋友可以參考下
    2023-11-11
  • 關(guān)于Node.js的events.EventEmitter用法介紹

    關(guān)于Node.js的events.EventEmitter用法介紹

    本篇文章主要介紹了關(guān)于Node.js的events.EventEmitter用法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下。
    2017-04-04
  • node.js版本管理工具n無效的原理和解決方法

    node.js版本管理工具n無效的原理和解決方法

    大家都知道在Centos中一般需要根據(jù)項(xiàng)目的環(huán)境安裝指定版本的Node, 而現(xiàn)有的yum源版本一般不夠全面也不一定找的到所需要的指定版本, 此時(shí)就必須自行下載Node源碼進(jìn)行編譯安裝了,如果你在使用node.js版本管理工具n的時(shí)候發(fā)現(xiàn)工具無效,下面就來看看這篇文章的解決方法吧。
    2016-11-11
  • CentOS 8.2服務(wù)器上安裝最新版Node.js的方法

    CentOS 8.2服務(wù)器上安裝最新版Node.js的方法

    這篇文章主要介紹了CentOS 8.2服務(wù)器上安裝最新版Node.js的方法,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2020-12-12

最新評(píng)論