Authored by 郝肖肖

Merge branch 'feature/articleSubmit' into 'master'

'百度熊掌号'



See merge request !3
... ... @@ -16,6 +16,7 @@ const logger = global.yoho.logger;
const app = express();
const seo = require('./apps/seo');
const cseo = require('./apps/controllers/seo/index');
const baiduXzh = require('./apps/controllers/seo/xzh');
// hbs-set
app.engine('hbs', hbs({
... ... @@ -45,6 +46,10 @@ app.get('/xml-data/demo.xsd', cseo.demoXsd);
app.get('/setTask', cseo.setTask);
app.get('/delTask', cseo.delTask);
// 百度熊掌号
app.get('/xzh', baiduXzh.index);
app.get('/xzh/history', baiduXzh.history);
// 定时任务 主动推送和生成xml
seo.start();
... ...
'use strict';
/**
* [百度熊掌号]
* @type {[type]}
*/
const XzhIndexModel = require('../../models/xzh/index');
const index = (req, res) => {
req.ctx(XzhIndexModel).index().then(result => {
return res.json(result);
});
};
const history = (req, res) => {
req.ctx(XzhIndexModel).sendDataHistory(1);
return res.json({
code: 200,
data: '',
message: '任务创建成功,开始运行...'
});
};
module.exports = {
index,
history
};
... ...
'use strict';
const ROOT_PATH = global.ROOT_PATH;
const _ = require('lodash');
const rp = require('request-promise');
const util = require(`${ROOT_PATH}/libs/util`);
const senUrl = 'http://data.zz.baidu.com/urls?appid=1583402501013173&token=K0L5PUhk1XOko81r&type=';
class XzhIndexModel extends global.yoho.BaseModel {
constructor(ctx) {
super(ctx);
}
index() {
// 新增
return this.getLastArticleList({page: 1, limit: 100}).then(rdata => {
rdata = _.get(rdata, 'data.artList', []);
return this.sendData(rdata, 'realtime');
});
}
sendDataHistory(page) {
return util.sleep(3000).then(() => {
return this.getLastArticleList({page: 1});
}).then(rdata => {
rdata = _.get(rdata, 'data.artList', []);
if (rdata.length <= 0) {
return false;
}
return this.sendData(rdata).then(result => {
console.log(`sendDataHistory => result: ${JSON.stringify(result)}, page: ${page} `);
return this.sendDataHistory(++page);
});
});
}
// 向百度发送数据
sendData(data, type) {
type = type || 'batch';
data = _.map(data, item => {
return `https://m.yohobuy.com/mip/guang/${item.articleId}.html`;
});
return rp({
method: 'POST',
uri: `${senUrl}${type}`,
body: data.join('\n'),
timeout: 8 * 1000
}).then(result => {
return JSON.parse(result || '{}');
});
}
getLastArticleList(params) {
params = _.assign({
limit: 100,
udid: 'seo_format_data'
}, params);
return this.get({
url: '/guang/api/v2/article/getLastArticleList',
data: params,
param: {
cache: 86400
},
api: global.yoho.ServiceAPI
}).catch(e => {
console.log(e.message);
return {code: 400, data: {}, message: e.message};
});
}
}
module.exports = XzhIndexModel;
... ...