index.js 3.04 KB
'use strict';

const ROOT_PATH = global.ROOT_PATH;
const _ = require('lodash');
const co = Promise.coroutine;
const rp = require('request-promise');
const redis = require(`${ROOT_PATH}/libs/redis`);
const YohoApiModel = require('./../interface/yoho-api');
const TaskLogModel = require('./../interface/task-log');
const senUrl = 'http://data.zz.baidu.com/urls?appid=1583402501013173&token=K0L5PUhk1XOko81r&type=';

// http://ziyuan.baidu.com/xzh/commit/push?appid=1583402501013173&qq-pf-to=pcqq.c2c

class XzhIndexModel extends global.yoho.BaseModel {
    constructor(ctx) {
        super(ctx);

        this.yohoApiModel = new YohoApiModel(ctx);
        this.taskLogModel = new TaskLogModel(ctx);
    }

    index() {
        let that = this;

        return co(function* () {
            let ids = [];
            let message = '';

            // 新增
            let rdata = yield that.yohoApiModel.getLastArticleList({page: 1, limit: 100});

            rdata = _.get(rdata, 'data.artList', []);
            ids = _.map(rdata, item => {
                return `${item.articleId}`;
            });

            let artice = {
                total: ids.length,
                notAlready: 0
            };

            ids = _.difference(ids, yield redis.hmgetAsync('global:yoho:seo:xzh:guang', ids));// 去除已经推送的
            artice.notAlready = ids.length;
            message = `获取${artice.total}条记录,未推送的有${artice.notAlready}条记录`;

            if (artice.notAlready <= 0) {
                return {code: 201, data: {}, message: message};
            }

            rdata = yield that.sendData(ids, 'realtime');

            if (rdata.code !== 200) {
                return rdata;
            }

            let tids = [];

            _.each(ids, id => {
                tids.push(id, id);
            });

            yield redis.hmsetAsync('global:yoho:seo:xzh:guang', tids);

            ids = [];
            tids = [];

            return Object.assign(rdata, {message: message});
        })().then(rdata => {
            let key = this.taskLogModel.getKey('http://127.0.0.1:6005/sendXzh');

            return this.taskLogModel.ltrim(key).then(() => {
                return this.taskLogModel.add(key, rdata);
            }).then(() => {
                return rdata;
            });
        });
    }

    // 向百度发送数据
    sendData(data, type) {

        if (data.length <= 0) {
            return Promise.resolve({code: 400, data: {}, message: 'data is empty'});
        }

        type = type || 'batch';

        data = _.map(data, id => {
            return `https://m.yohobuy.com/mip/guang/${id}.html`;
        });

        return rp({
            method: 'POST',
            uri: `${senUrl}${type}`,
            body: data.join('\n'),
            timeout: 8 * 1000
        }).then(result => {
            return Object.assign({code: 200}, JSON.parse(result || '{}'));
        }).catch(e => {
            console.log(e.message);
            return {code: 400, data: {}, message: e.message};
        });
    }
}

module.exports = XzhIndexModel;