product-service.js
1.41 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
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);
});
})
}
};