product-service.js 1.41 KB
const request = require('request-promise');

const PRODUCT_XML_INDEX_URL = 'http://xml.yohobuy.com/baidu-site-map.xml';
const indexUrl = p => `http://xml.yohobuy.com/product_baidu_${p}.xml`;

const tplUrl = (type, p) => `/sitemap_${type}/product_${p}.xml`;

const jp = require('jsonpath');
const parseString = require('xml2js').parseString;

const LOC_REX = /<loc>.*<\/loc>/ig;

const url = require('url');
const _ = require('lodash');

module.exports = class extends global.yoho.BaseModel {
    constructor(ctx) {
        super(ctx);
    }

    async getUrls(page) {
        let body = await request.get(indexUrl(page));
        let xmlJson = await this._parseXml2js(body);
        let productUrls = jp.query(xmlJson, '$..loc');

        productUrls = _.flatten(productUrls);

        productUrls = productUrls.map(u => {
            return url.parse(u).path;
        });

        return productUrls;
    }

    async getIndex(type) {
        let body = await request.get(PRODUCT_XML_INDEX_URL);
        let result = body.match(LOC_REX);

        result = result.map((url, i) => {
            return tplUrl(type, i + 1);
        });

        return result;
    }

    _parseXml2js(xml) {
        return new Promise((resolve, reject) => {
            parseString(xml, (err, result) => {
                if (err) {
                    return reject(err);
                }

                resolve(result);
            });
        })
    }
};