baidu-link-submit.js 9.76 KB
'use strict';

const ROOT_PATH = global.ROOT_PATH;
const _ = require('lodash');
const redis = require(`${ROOT_PATH}/libs/redis`);
const YohoApiModel = require('./../interface/yoho-api');
const co = Promise.coroutine;
const rp = require('request-promise');
const senUrl = 'http://data.zz.baidu.com/urls';

/**
 * 连接提交
 * http://ziyuan.baidu.com/linksubmit/index?site=https://www.yohobuy.com/
 */
class BaiduLinkSubmit extends global.yoho.BaseModel {
    constructor(ctx) {
        super(ctx);

        this.yohoApiModel = new YohoApiModel(ctx);
    }

    // 向百度发送数据
    sendUrl(urls, site, type) {
        let siteType = '';

        if (type === 'mip') {
            siteType = 'MIP: ';
        } else if (site.indexOf('www.yohobuy') > -1) {
            siteType = 'PC: ';
        } else if (site.indexOf('m.yohobuy') > -1) {
            siteType = 'WAP: ';
        }

        if (!site) {
            return Promise.resolve({code: 400, data: {}, message: 'site is empty'});
        }

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

        return rp({
            method: 'POST',
            uri: senUrl,
            qs: {
                token: '0lSAO4ZxEKsYopMG',
                site: site,
                type: type
            },
            qsStringifyOptions: {
                encode: false
            },
            body: urls.join('\n'),
            timeout: 8 * 1000,
            gzip: true
        }).then(result => {
            result = JSON.parse(result || '{}');

            return Object.assign({
                code: 200,
                message: `${siteType}当天剩余${result.remain || result.remain_mip}条可推送到百度资源平台,本次成功推送${result.success || result.success_mip}条。` // eslint-disable-line
            }, result);
        }).catch(e => {
            console.log(e.message);
            return {code: e.statusCode || 400, data: {}, message: e.message};
        });
    }

    // 提交商品详情URL
    goodsUrl() {
        let that = this;

        return co(function* () {

            let products = {
                pc: [],
                wap: [],
                pcUrl: 'https://www.yohobuy.com',
                wapUrl: 'https://m.yohobuy.com'
            };
            let tids = [];
            let rdata = _.get(yield that.yohoApiModel.bdPromotion(), 'data', []);
            let ids = _.map(rdata, item => {
                return `${item.erpProductId}`;
            });

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

            ids = _.difference(ids, yield redis.hmgetAsync('global:yoho:seo:linksubmit:goods', ids));// 去除已经推送的
            artice.notAlready = ids.length;


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

                return [
                    {code: 201, data: {}, message: `PC: ${message}`},
                    {code: 201, data: {}, message: `WAP: ${message}`}
                ];
            }

            _.each(ids, val => {
                tids.push(val, val);// mset做准备

                products.pc.push(`${products.pcUrl}/product/${val}.html`);
                products.wap.push(`${products.wapUrl}/product/${val}.html`);
            });

            rdata = yield Promise.all([
                that.sendUrl(products.pc, products.pcUrl),
                that.sendUrl(products.wap, products.wapUrl)
            ]);

            if (rdata[0].code !== 200 || rdata[1].code !== 200) {
                return rdata;
            }

            yield redis.hmsetAsync('global:yoho:seo:linksubmit:goods', tids);

            ids = [];
            tids = [];

            return rdata;

        })();
    }

    // 提交逛详情URL
    guangUrl() {
        let that = this;

        return co(function* () {
            let guangs = {
                pc: [],
                wap: [],
                mip: [],
                pcUrl: 'https://www.yohobuy.com',
                wapUrl: 'https://m.yohobuy.com',
                mipUrl: 'https://m.yohobuy.com'
            };

            let tids = [];
            let rdata = _.get(yield that.yohoApiModel.getLastArticleList(), 'data.artList', []);
            let ids = _.map(rdata, item => {
                return `${item.articleId}`;
            });
            let artice = {
                total: ids.length,
                notAlready: 0
            };

            ids = _.difference(ids, yield redis.hmgetAsync('global:yoho:seo:linksubmit:guang', ids));// 去除已经推送的
            artice.notAlready = ids.length;

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

                return [
                    {code: 201, data: {}, message: `PC: ${message}`},
                    {code: 201, data: {}, message: `WAP: ${message}`},
                    {code: 201, data: {}, message: `MIP: ${message}`}
                ];
            }

            _.each(ids, val => {
                tids.push(val, val);// mset做准备

                guangs.pc.push(`${guangs.pcUrl}/guang/${val}.html`);
                guangs.wap.push(`${guangs.wapUrl}/guang/${val}.html`);
                guangs.mip.push(`${guangs.mipUrl}/mip/guang/${val}.html`);
            });

            rdata = yield Promise.all([
                that.sendUrl(guangs.pc, guangs.pcUrl),
                that.sendUrl(guangs.wap, guangs.wapUrl),
                that.sendUrl(guangs.mip, guangs.mipUrl, 'mip')
            ]);

            if (rdata[0].code !== 200 || rdata[1].code !== 200) {
                return rdata;
            }


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

            ids = [];
            tids = [];

            return rdata;
        })();
    }

    _getYohoNewsLink(url) {
        return rp({
            method: 'GET',
            uri: url,
            qs: {},
            qsStringifyOptions: {
                encode: false
            },
            timeout: 8 * 1000,
            json: true,
            gzip: true
        }).then(result => {
            return {
                code: 200,
                data: result,
                message: 'success'
            };
        }).catch(e => {
            console.log(`_getYohoNewsLink: err: ${e.message}`);
            return {code: e.statusCode || 400, data: [], message: e.message};
        });
    }
    // 潮流boys
    yohoBoys() {
        let that = this;

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

            let rdata = _.get(yield that._getYohoNewsLink(
                `${initData.pcUrl}/channel/ajax/tab?channel=100&page=1`
            ), '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:linksubmit:newsBoys', links));// 去除已经推送的
            artice.notAlready = links.length;

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

                return {code: 201, data: [], message: message};
            }

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

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

                initData.pc.push(`http:${val}`);
            });

            rdata = yield that.sendUrl(initData.pc, initData.pcUrl);

            if (_.get(rdata, 'code', 400) !== 200) {
                return rdata;
            }

            yield redis.hmsetAsync('global:yoho:seo:linksubmit:newsBoys', tlinks);

            links = [];
            tlinks = [];

            return rdata;
        })();
    }
    // 潮流girls
    yohoGirls() {
        let that = this;

        return co(function* () {
            let initData = {
                pc: [],
                pcUrl: 'http://www.yohogirls.com',
            };

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

            links = _.difference(links, yield redis.hmgetAsync('global:yoho:seo:linksubmit:newsGirls', links));// 去除已经推送的
            artice.notAlready = links.length;

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

                return {code: 201, data: [], message: message};
            }

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

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

                initData.pc.push(`http:${val}`);
            });

            rdata = yield that.sendUrl(initData.pc, initData.pcUrl);

            if (_.get(rdata, 'code', 400) !== 200) {
                return rdata;
            }

            yield redis.hmsetAsync('global:yoho:seo:linksubmit:newsGirls', tlinks);

            links = [];
            tlinks = [];

            return rdata;
        })();
    }

}

module.exports = BaiduLinkSubmit;