Skip to content

Commit

Permalink
use native http instead of deprecated request lib
Browse files Browse the repository at this point in the history
  • Loading branch information
huiwang committed Nov 22, 2020
1 parent db1dfeb commit 5d4ba94
Show file tree
Hide file tree
Showing 3 changed files with 388 additions and 26 deletions.
49 changes: 26 additions & 23 deletions lib/recommend_console.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
var rp = require('request-promise');
var Promise = require('bluebird');
var fs = require('hexo-fs');
var pathFn = require('path');
var join = pathFn.join;
var extractConfig = require('./recommend_config');

const https = require('https');
const url = require('url');
const fs = require('hexo-fs');
const pathFn = require('path');
const join = pathFn.join;
const extractConfig = require('./recommend_config');

module.exports = function generateConsole(args) {
var log = this.log;
Expand Down Expand Up @@ -32,34 +31,38 @@ module.exports = function generateConsole(args) {
};
});

const request_body = {
const request_body = JSON.stringify({
internal: rp_config.internalLinks,
external: rp_config.externalLinks,
site: {
domain: config.url,
posts: request_posts
}
}
});

const options = {
hostname: url.parse(rp_config.server).hostname,
method: 'POST',
baseUrl: rp_config.server,
uri: 'recommendation',
body: request_body,
json: true
path: '/recommendation',
headers: {
'Content-Type': 'application/json',
'Content-Length': Buffer.byteLength(request_body)
}
};


rp(options)
.then(function (result) {
return fs.writeFile(dest, JSON.stringify(result));
var result = "";
const req = https.request(options, response => {
response.on("data", d => {
result += d;
})
.then(function () {
response.on("end", () => {
fs.writeFile(dest, result);
const end = new Date().getTime();
log.info("All recommended posts downloaded succesfully to " + dest_file + " in " + (end - start) / 1000 + "s")
const duration = (end - start) / 1000;
log.info("All recommended posts downloaded succesfully to " + dest_file + " in " + duration + "s")
})
.catch(function (error) {
log.info("Unable to download recommendatoin from server", error);
});
});
req.on("error", error => log.info("Unable to download recommendatoin from server", error));
req.write(request_body);
req.end();
})
}
Loading

0 comments on commit 5d4ba94

Please sign in to comment.