|
@@ -16,6 +16,7 @@ const rp = require('request-promise'); |
|
@@ -16,6 +16,7 @@ const rp = require('request-promise'); |
16
|
const config = require('../../../config/config');
|
16
|
const config = require('../../../config/config');
|
17
|
const singleBrandKeyPre = config.singleBrandKeyPre;
|
17
|
const singleBrandKeyPre = config.singleBrandKeyPre;
|
18
|
const singleSortKeyPre = config.singleSortKeyPre;
|
18
|
const singleSortKeyPre = config.singleSortKeyPre;
|
|
|
19
|
+const qs = require('querystring');
|
19
|
|
20
|
|
20
|
const multiAsync=(multi)=>{
|
21
|
const multiAsync=(multi)=>{
|
21
|
return multi.execAsync().then(function(res) {
|
22
|
return multi.execAsync().then(function(res) {
|
|
@@ -361,5 +362,135 @@ r.post('/expand/del', async(ctx) => { |
|
@@ -361,5 +362,135 @@ r.post('/expand/del', async(ctx) => { |
361
|
};
|
362
|
};
|
362
|
});
|
363
|
});
|
363
|
});
|
364
|
});
|
|
|
365
|
+const baiduUrls = {
|
|
|
366
|
+ urls: 'http://data.zz.baidu.com/urls',
|
|
|
367
|
+ update: 'http://data.zz.baidu.com/update',
|
|
|
368
|
+ del: 'http://data.zz.baidu.com/del'
|
|
|
369
|
+};
|
|
|
370
|
+/**
|
|
|
371
|
+ * 将链接推送到百度站长
|
|
|
372
|
+ * @param params object {site: 'https://www.yohobuy.com', type: 'mip'} 默认不需要type
|
|
|
373
|
+ * @param urls
|
|
|
374
|
+ */
|
|
|
375
|
+const sendUrlsToBaidu = (params, urls) => {
|
|
|
376
|
+ let paramsDef = {
|
|
|
377
|
+ token: config.baiduToken
|
|
|
378
|
+ };
|
|
|
379
|
+
|
|
|
380
|
+ // 过滤无效的参数
|
|
|
381
|
+ _.forEach(params, (val, key) => {
|
|
|
382
|
+ if (!val) {
|
|
|
383
|
+ delete params[key];
|
|
|
384
|
+ }
|
|
|
385
|
+ });
|
|
|
386
|
+
|
|
|
387
|
+ qs.escape = (str) => {
|
|
|
388
|
+ return str;
|
|
|
389
|
+ };
|
|
|
390
|
+
|
|
|
391
|
+ let options = {
|
|
|
392
|
+ url: `${baiduUrls.urls}?${qs.stringify(Object.assign(paramsDef, params), null, null, {})}`,
|
|
|
393
|
+ headers: {
|
|
|
394
|
+ 'Content-Type': 'text/plain'
|
|
|
395
|
+ },
|
|
|
396
|
+ method: 'post',
|
|
|
397
|
+ form: urls.join('\n'),
|
|
|
398
|
+ json: true,
|
|
|
399
|
+ timeout: 10000,
|
|
|
400
|
+ gzip: true
|
|
|
401
|
+ };
|
|
|
402
|
+
|
|
|
403
|
+ return true;
|
|
|
404
|
+ //return rp(options).then(result => {
|
|
|
405
|
+ // logger.info(Object.assign(params, result, {length: urls.length}));
|
|
|
406
|
+ // if (result) {
|
|
|
407
|
+ // return true;
|
|
|
408
|
+ // } else {
|
|
|
409
|
+ // return false;
|
|
|
410
|
+ // }
|
|
|
411
|
+ //});
|
|
|
412
|
+};
|
|
|
413
|
+/**
|
|
|
414
|
+ * 推送urls
|
|
|
415
|
+ * @params limit url条数
|
|
|
416
|
+ * @return boolean
|
|
|
417
|
+ */
|
|
|
418
|
+const sendUrls = (limit) => {
|
|
|
419
|
+
|
|
|
420
|
+ // 调用limit条未推送关键词
|
|
|
421
|
+ let mysql = new Mysql();
|
|
|
422
|
+
|
|
|
423
|
+ console.log('sendUrl exe');
|
|
|
424
|
+ return mysql.query(`select id, keyword from seo_keywords where is_push=0 order by id asc limit ${limit}`).then(d => {
|
|
|
425
|
+ let pcPages = [],
|
|
|
426
|
+ wapPages = [];
|
|
|
427
|
+
|
|
|
428
|
+ console.log(d);
|
|
|
429
|
+ _.forEach(d, (val) => {
|
|
|
430
|
+ pcPages.push(`https://www.yohobuy.com/chanpin/${val.id}.html`);
|
|
|
431
|
+ wapPages.push(`https://m.yohobuy.com/chanpin/${val.id}.html`);
|
|
|
432
|
+ })
|
|
|
433
|
+
|
|
|
434
|
+ // 根据 site 推送相应站点
|
|
|
435
|
+ return Promise.all([sendUrlsToBaidu({site: 'https://www.yohobuy.com'}, pcPages),sendUrlsToBaidu({site: 'https://m.yohobuy.com'}, wapPages)]).then(result => {
|
|
|
436
|
+ let updateSql = `UPDATE seo_keywords SET is_push = 1 WHERE id >= ${_.get(_.first(d), 'id')} and id <= ${_.get(_.last(d), 'id')}`;
|
|
|
437
|
+
|
|
|
438
|
+ console.log(updateSql);
|
|
|
439
|
+
|
|
|
440
|
+ // 更新关键词推送状态
|
|
|
441
|
+ return mysql.query(updateSql).then(updateRes => {
|
|
|
442
|
+ console.log(updateRes);
|
|
|
443
|
+ return updateRes;
|
|
|
444
|
+ });
|
|
|
445
|
+ })
|
|
|
446
|
+ });
|
|
|
447
|
+}
|
|
|
448
|
+
|
|
|
449
|
+const getPushTotal = () => {
|
|
|
450
|
+ let mysql = new Mysql();
|
|
|
451
|
+
|
|
|
452
|
+ return mysql.query(`select count(id) from seo_keywords where is_push=0`).then(d=>{
|
|
|
453
|
+ return _.get(d, '0.count(id)', 0);
|
|
|
454
|
+ });
|
|
|
455
|
+}
|
|
|
456
|
+
|
|
|
457
|
+/**
|
|
|
458
|
+ * 推送url到百度(获取2000条未推送关键词,监测是否还有未推关键词,如果有再推2000,直到全部推送)
|
|
|
459
|
+ */
|
|
|
460
|
+
|
|
|
461
|
+r.get('/sendUrl', async(ctx, next) => {
|
|
|
462
|
+ let result = {
|
|
|
463
|
+ code: 200,
|
|
|
464
|
+ message: ''
|
|
|
465
|
+ };
|
|
|
466
|
+
|
|
|
467
|
+ let total = await getPushTotal(); // 待推送总数
|
|
|
468
|
+
|
|
|
469
|
+ let limit = 20; // 每次推送的条数
|
|
|
470
|
+
|
|
|
471
|
+ if (total === 0) {
|
|
|
472
|
+ result.message = '没有需要推送的词!';
|
|
|
473
|
+ } else {
|
|
|
474
|
+ let intervalTime = Math.floor(total/limit) + 1;
|
364
|
|
475
|
|
|
|
476
|
+ console.log(total);
|
|
|
477
|
+ console.log(intervalTime);
|
|
|
478
|
+
|
|
|
479
|
+
|
|
|
480
|
+ // 推送2000条
|
|
|
481
|
+ let interval = setInterval(() => {
|
|
|
482
|
+ sendUrls(limit);
|
|
|
483
|
+
|
|
|
484
|
+ if (--intervalTime < 1) {
|
|
|
485
|
+ clearInterval(interval);
|
|
|
486
|
+ }
|
|
|
487
|
+
|
|
|
488
|
+ }, 1000);
|
|
|
489
|
+ result.message = `${intervalTime*10} 秒后查看推送结果`;
|
|
|
490
|
+ }
|
|
|
491
|
+
|
|
|
492
|
+
|
|
|
493
|
+
|
|
|
494
|
+ ctx.response.body = result;
|
|
|
495
|
+});
|
365
|
module.exports = r; |
496
|
module.exports = r; |