seoModel.js
9.65 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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
'use strict';
const api = global.yoho.API;
const redis = global.yoho.redis;
const rp = require('request-promise');
const serviceApi = global.yoho.ServiceAPI;
const Promise = require('bluebird');
const co = Promise.coroutine;
const _ = require('lodash');
const logger = global.yoho.logger;
const helper = global.yoho.helpers;
const config = require('../config/config');
const qs = require('querystring');
const fs = require('fs');
const util = require('../libs/util');
const ret = {
code: 200,
message: '',
data: {}
};
/**
* redis multi command
*/
const multiAsync = (multi)=>{
return multi.execAsync().then(function(res) {
return res;
});
};
const baiduUrls = {
urls: 'http://data.zz.baidu.com/urls',
update: 'http://data.zz.baidu.com/update',
del: 'http://data.zz.baidu.com/del'
};
const siteUrls = {
pcProduct: {
site: 'https://www.yohobuy.com',
url: []
},
pcGuang: {
site: 'https://www.yohobuy.com/',
url: []
},
mProduct: {
site: 'https://m.yohobuy.com',
url: []
},
mGuang: {
site: 'https://m.yohobuy.com/',
url: []
}
};
// 配置
const redisKey = {
keywordsList: 'keywords_mana_list' // 关键词列表
};
/**
* 将链接推送到百度站长
* @param params object {site: 'https://www.yohobuy.com', type: 'mip'} 默认不需要type
* @param urls
*/
const sendUrlsToBaidu = (params, urls) => {
let paramsDef = {
token: config.baiduToken
};
// 过滤无效的参数
_.forEach(params, (val, key) => {
if (!val) {
delete params[key];
}
});
qs.escape = (str) => {
return str;
};
let options = {
url: `${baiduUrls.urls}?${qs.stringify(Object.assign(paramsDef, params), null, null, {})}`,
headers: {
'Content-Type': 'text/plain'
},
method: 'post',
form: urls.join('\n'),
json: true,
timeout: 10000,
gzip: true
};
return rp(options).then(result => {
logger.info(Object.assign(params, result, {length: urls.length}));
});
};
/**
* 获取最新1000条商品详情链接和逛详情链接
*/
const getUrls = () => {
let apiArr = [api.get('', {method: 'web.product.bdPromotion'}),
serviceApi.get('/guang/api/v2/article/getLastArticleList', {limit: 100})];
return api.all(apiArr).spread((productData, articleData) => {
_.forEach(_.get(productData, 'data', {}), value => {
siteUrls.pcProduct.url.push('https:' + helper.urlFormat(`/product/${value.erpProductId}.html`));
siteUrls.mProduct.url.push('https:' + helper.urlFormat(`/product/${value.erpProductId}.html`, null, 'm'));
});
_.forEach(_.get(articleData, 'data.artList', {}), value => {
siteUrls.pcGuang.url.push('https:' + helper.urlFormat(`/guang/${value.articleId}.html`));
siteUrls.mGuang.url.push('https:' + helper.urlFormat(`/guang/${value.articleId}.html`, null, 'm'));
});
return siteUrls;
});
};
/**
* 发送最新商品详情1000条和逛详情100条推送到相应的站点域名(pc和wap)
*/
const sendUrls = () => {
co(function*() {
// 获取pc/wap的商品详情和逛的链接
let sendArr = [],
urls = yield getUrls();
_.forEach(urls, value => {
sendArr.push(sendUrlsToBaidu({site: value.site, type: value.type}, value.url));
});
// 推送url
api.all(sendArr);
})();
};
/**
* 调用接口建议词
*/
const getKeywordsApi = (page, limit, count) => {
let params = {
page: page || 1,
limit: limit || 1000,
count: count || 6,
method: 'web.search.suggestList'
};
return api.get('', params);
};
/**
* 关键词同步到redis
*/
const synchronousKeywords = () => {
return getKeywordsApi(1, 1).then(res => {
let start = 0,
page = 1,
intervalTime = 1000, // 循环调用的时间间隔
limit = 1000, // 每次请求接口关键词数量
total = _.get(res, 'data.total', 0);
// 接口调用失败
if (total <= 0) {
console.log('no data');
return;
}
// 循环遍历接口关键词写入redis
let interval = setInterval(() => {
if (start > total) {
clearInterval(interval);
}
getKeywordsApi(page, limit).then(result => {
let multi = redis.multi();
start += limit;
page++;
_.forEach(_.get(result, 'data.suggest_list', []), value => {
value.keyword = value.keyword.split(' ').join('');
if (value.keyword.length <= 2) {
return;
}
let key = `keywords_mana:${value.keyword}`;
multi.set(key, value.keyword);
multi.lrem('keywords_mana_list', 1, key).lpush('keywords_mana_list', key);
});
multiAsync(multi);
}).catch(()=>{
clearInterval(interval);
});
}, intervalTime);
});
};
// 纪录关键词
const writeFile = (file, str) => {
fs.appendFile(file, `${str}\n`, function(err) {
if (err) {
logger.info(err);
}
});
};
/**
* 查询 redis中 关键词
* @type {{getKeyWordsUrl}}
*/
const getRedisKeywords2 = (start, end) => {
return redis.lrangeAsync(redisKey.keywordsList, start, end).then(res => {
return _.map(res, (elem) => {
return elem.replace('keywords_mana:', '');
});
});
};
/**
* 查询 redis中 关键词
* @type {{getKeyWordsUrl}}
*/
const getRedisKeywords = (start, end) => {
return redis.lrangeAsync(redisKey.keywordsList, start, end).then(res => {
let urls = {pc: [], wap: []};
_.forEach(res, keyword => {
keyword = keyword.replace('keywords_mana:', '');
let buff = new Buffer(keyword).toString('hex').toUpperCase();
writeFile(`./logs/keywords_${helper.dateFormat('YYYY-MM-DD H', new Date())}.log`, `${keyword} https://www.yohobuy.com/so/${buff}.html`);
// TODO 纪录已经推送的关键词
urls.pc.push(`https://www.yohobuy.com/so/${buff}.html`);
urls.wap.push(`https://m.yohobuy.com/so/${buff}.html`);
});
return urls;
});
};
/**
* 推送url
*/
const sendKeywordsUrls = () => {
return redis.llenAsync(redisKey.keywordsList).then(total => {
console.log(total);
if (total <= 0) {
return;
}
let start = 0,
intervalTime = 1000, // 循环调用的时间间隔
count = 1000;
let interval = setInterval(() => {
if (start >= total) {
clearInterval(interval);
}
getRedisKeywords(start, start + count).then(urls => {
// 发送到百度
sendUrlsToBaidu({site: 'https://www.yohobuy.com'}, urls.pc);
sendUrlsToBaidu({site: 'https://m.yohobuy.com'}, urls.wap);
}).catch(() => {
clearInterval(interval);
});
start += count;
}, intervalTime);
return [];
});
};
const rpKeyWordsUrl = (url) => {
if (!url) {
return Promise.resolve(Object.assign({}, ret, {
code: 400,
message: 'url is empty'
}));
}
return rp({
uri: url,
method: 'GET',
headers: {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 Chrome/55.0.2883.75 Safari/537.36'
},
resolveWithFullResponse: true, // header、statusCode
timeout: 10 * 1000,
gzip: true
}).then(body => {
return Object.assign({}, ret, {
data: {
url: url,
cache: body.headers['x-cache-status']
}
});
}).catch(err => {
return Object.assign({}, ret, {
code: err.statusCode,
data: {
url: url
}
});
});
};
const visitKeyWordsUrl = () => {
let size = 5;
let currentPage = 1;
return redis.hgetAsync('yohoSeo', 'rpPage').then(page => {
page = page || 1;
let intval = setInterval(() => {
currentPage = page;
return redis.hsetAsync('yohoSeo', 'rpPage', page++).then(() => {
return util.sleep(100);
}).then(() => {
return getRedisKeywords2((currentPage - 1) * size, currentPage * size - 1);
}).then(d => {
let ddata = [];
let buff;
if (d.length <= 0) {
clearInterval(intval);
return redis.hsetAsync('yohoSeo', 'rpPage', 1).then(() => {
return [];
});
}
_.forEach(d, keyword => {
buff = new Buffer(keyword).toString('hex').toUpperCase();
// TODO 纪录已经推送的关键词
ddata.push(
rpKeyWordsUrl(`https://www.yohobuy.com/so/${buff}.html`),
rpKeyWordsUrl(`https://m.yohobuy.com/so/${buff}.html`)
);
});
return Promise.all(ddata);
}).then(d => {
console.log(`rpKeyWordsUrl => page: ${page}, result: ${JSON.stringify(d)}`);
return d;
});
}, 300);
return page;
});
};
module.exports = {
sendUrls,
synchronousKeywords,
sendKeywordsUrls,
visitKeyWordsUrl
};