index.js 8.37 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 SENURL = 'http://data.zz.baidu.com/urls';
const NEWSKEY = 'global:yoho:seo:baidu';
const XZHKEY = {
    yohobuy: {
        appid: '1583402501013173',
        token: 'K0L5PUhk1XOko81r'
    },
    yohonow: {
        appid: '1590360901815465',
        token: 'qcODcW0QEjYMApou'
    },
    mars: {
        appid: '1575043410474268',
        token: 'Fr9lp0npUOPCuU04'
    }
};

// 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);
    }

    index() {
        let that = this;

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

            // 新增
            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;

            if (artice.notAlready <= 0) {
                return {code: 201, data: {}, message: `获取${artice.total}条记录,未推送0条,成功推送0条。`};
            }

            rdata = yield that._sendData(_.map(ids, id => {
                return `https://m.yohobuy.com/mip/guang/${id}.html`;
            }), XZHKEY.yohobuy, '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 rdata;
        })();
    }

    yohoGirlsAll() {
        let that = this;

        return co(function* () {
            let urls = yield redis.lrangeAsync(`${NEWSKEY}:mip:www_yohogirls_com`, 0, 999);
            let rdata = yield that._sendData(urls, XZHKEY.yohonow, 'batch');

            yield redis.ltrimAsync(`${NEWSKEY}:www_yohogirls_com`, 1000, -1);
            yield redis.ltrimAsync(`${NEWSKEY}:mip:www_yohogirls_com`, 1000, -1);
            return rdata;
        })();
    }

    yohoBoysAll() {
        let that = this;

        return co(function* () {
            let urls = yield redis.lrangeAsync(`${NEWSKEY}:mip:www_yohoboys_com`, 0, 999);
            let rdata = yield that._sendData(urls, XZHKEY.yohonow, 'batch');

            yield redis.ltrimAsync(`${NEWSKEY}:www_yohoboys_com`, 1000, -1);
            yield redis.ltrimAsync(`${NEWSKEY}:mip:www_yohoboys_com`, 1000, -1);

            return rdata;
        })();
    }

    marsAll() {
        let that = this;

        return co(function* () {
            let urls = yield redis.lrangeAsync(`${NEWSKEY}:mip:www_yohomars_com`, 0, 999);
            let rdata = yield that._sendData(urls, XZHKEY.mars, 'batch');

            yield redis.ltrimAsync(`${NEWSKEY}:www_yohomars_com`, 1000, -1);
            yield redis.ltrimAsync(`${NEWSKEY}:mip:www_yohomars_com`, 1000, -1);

            return rdata;
        })();
    }

    // 实时推送潮流资讯
    yohoBoysReal() {
        let that = this;

        return co(function* () {
            let initData = {
                mip: [],
                mipUrl: 'http://www.yohoboys.com'
            };

            let rdata = _.get(yield that._getRp(
                `${initData.mipUrl}/channel/ajax/tab?channel=100&page=1`,
                true
            ), 'data', []);
            let tlinks = [];
            let links = _.map(rdata, item => {
                return _.get(item, 'content.url', '').replace(/http:|https:/, '');
            });
            let artice = {
                total: links.length,
                notAlready: 0
            };

            links = _.difference(
                links,
                yield redis.hmgetAsync('global:yoho:seo:xzh:yohoBoysReal', links)
            );// 去除已经推送的
            artice.notAlready = links.length;
            if (artice.notAlready <= 0) {
                let message = `获取${artice.total}条记录,未推送0条,成功推送0条。`;

                return {code: 201, data: {}, message: `MIP: ${message}`};
            }

            _.each(links, val => {
                if (_.isEmpty(val)) {
                    return true;
                }

                tlinks.push(val, val);// mset做准备

                initData.mip.push('http:' + val.replace(/\/news\/index/, '/mip/news/mobile'));
            });

            rdata = yield that._sendData(initData.mip, XZHKEY.yohonow, 'realtime');

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

            yield redis.hmsetAsync('global:yoho:seo:xzh:yohoBoysReal', tlinks);

            initData = {};
            links = [];
            tlinks = [];

            return rdata;
        })();
    }

    // 实时推送mars
    marsReal() {
        let that = this;

        return co(function* () {
            let marsIds = [];
            let lastId = 0;
            let initData = {
                mip: [],
                mipUrl: 'http://www.yohomars.com'
            };

            // init global:yoho:seo:xzh:marsReal 12222
            let marsRealId = yield redis.getAsync('global:yoho:seo:xzh:marsReal');
            let marsReal = [];

            marsRealId = parseInt(marsRealId, 10) || 12222;

            for (let i = 0; i < 8; i++) {
                marsReal.push(yield that._getRp(`http://www.yohomars.com/share/store/${marsRealId}`, false));
                marsIds.push(marsRealId);
                marsRealId = (marsRealId + 1);
            }

            _.each(marsReal, (mars, index) => {
                if (mars.code === 200) {
                    lastId = marsIds[index];
                    initData.mip.push(`http://www.yohomars.com/store/share/mip/${lastId}.html`);
                }
            });

            // 记录最后一次404或者上次推送成功连接id
            lastId = lastId ? (lastId + 1) : marsIds.shift();
            yield redis.setAsync('global:yoho:seo:xzh:marsReal', lastId);

            return that._sendData(initData.mip, XZHKEY.mars, 'realtime');
        })();
    }

    // 向百度发送数据
    _sendData(urls, appTokey, type) {

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

        return rp({
            method: 'POST',
            uri: SENURL,
            qs: {
                appid: appTokey.appid,
                token: appTokey.token,
                type: type || 'batch'
            },
            qsStringifyOptions: {
                encode: false
            },
            body: urls.join('\n'),
            timeout: 8 * 1000
        }).then(result => {
            result = JSON.parse(result || '{}');
            result = Object.assign({}, result, {
                remain: result.remain_batch || result.remain_realtime || result.remain,
                success: result.success_batch || result.success_realtime || result.success
            });

            return Object.assign({
                code: 200,
                message: `当天剩余${result.remain}条可推送到mip,本次成功推送${result.success}条。`
            }, result);
        }).catch(e => {
            console.log(e.message);
            return {code: e.statusCode || 400, data: {}, message: e.message};
        });
    }

    _getRp(url, json) {
        return rp({
            method: 'GET',
            uri: url,
            qs: {},
            qsStringifyOptions: {
                encode: false
            },
            timeout: 8 * 1000,
            json: json || true,
            gzip: true
        }).then(result => {
            return {
                code: 200,
                data: result,
                message: 'success'
            };
        }).catch(e => {
            return {code: e.statusCode || 400, data: [], message: e.message};
        });
    }
}

module.exports = XzhIndexModel;