时间:2021-05-26
本文实例讲述了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程序设计有所帮助。
声明:本页内容来源网络,仅供用户参考;我单位不保证亦不表示资料全面及准确无误,也不保证亦不表示这些资料为最新信息,如因任何原因,本网内容或者用户因倚赖本网内容造成任何损失或损害,我单位将不会负任何法律责任。如涉及版权问题,请提交至online#300.cn邮箱联系删除。
百度指数是以百度海量网民行为数据为基础的数据分享平台。在这里,你可以研究关键词搜索趋势、洞察网民兴趣和需求、监测舆情动向、定位受众特征。 百度指数(Baidu
本文实例讲述了C#实现解析百度天气数据,Rss解析百度新闻以及根据IP获取所在城市的方法,分享给大家供大家参考。具体实现方法如下:一、百度天气接口地址:http
8月27消息,中文搜索引擎百度旗下产品——百度地图(ditu.baidu.com)今年以来动作频频,继宣布正式对外宣布开放API(应用程序接口)后,百度地图昨晚
用Vue调用百度的搜索接口,实现简单的搜索功能。搜索框的样式是基于Bootstrap,当然对样式做了简单的调整,使之类似于百度搜索。代码如下百度搜索.gray{
百度圣卡是什么? 百度圣卡是百度与联通公司联合推出的流量卡产品,用户通过办理三种不同资费的圣卡可以实现在手机百度、百度地图、百度贴吧以及爱奇艺四大应用内流量