-
Notifications
You must be signed in to change notification settings - Fork 15
/
recommend_console.js
68 lines (59 loc) · 1.93 KB
/
recommend_console.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
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;
var config = this.config;
var rp_config = extractConfig(config);
var source_dir = this.source_dir;
var dest_file = '_data/recommended_posts.json';
var dest = join(source_dir, dest_file);
var Post = this.model('Post');
this.load().then(function () {
const start = new Date().getTime();
log.info("Start fetching recommendation with config " + JSON.stringify(rp_config))
const request_posts = Post.filter(post => post.published).map(post => {
return {
title: post.title,
permalink: post.permalink,
tags: post.tags.data.map(tag => tag.name),
updated: post.date.toISOString()
};
});
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',
path: '/recommendation',
headers: {
'Content-Type': 'application/json',
'Content-Length': Buffer.byteLength(request_body)
}
};
var result = "";
const req = https.request(options, response => {
response.on("data", d => {
result += d;
})
response.on("end", () => {
fs.writeFile(dest, result);
const end = new Date().getTime();
const duration = (end - start) / 1000;
log.info("All recommended posts downloaded succesfully to " + dest_file + " in " + duration + "s")
})
});
req.on("error", error => log.info("Unable to download recommendatoin from server", error));
req.write(request_body);
req.end();
})
}