Authored by 周少峰

sendurl to baidu

@@ -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;
@@ -19,7 +19,7 @@ @@ -19,7 +19,7 @@
19 <div class="panel-body"> 19 <div class="panel-body">
20 <label style="margin-right:20px;"><input id="allSelected" type="checkbox" style="margin-right:5px;">全选</label> 20 <label style="margin-right:20px;"><input id="allSelected" type="checkbox" style="margin-right:5px;">全选</label>
21 <a class="btn btn-default deleteAll">删除</a> 21 <a class="btn btn-default deleteAll">删除</a>
22 - 22 + <a class="btn btn-default" href="javascript:sendUrl();">推送百度</a>
23 <div class="input-append pull-right"> 23 <div class="input-append pull-right">
24 <form id="query-form" action="/keywords/expand" class="query-form" method="get"> 24 <form id="query-form" action="/keywords/expand" class="query-form" method="get">
25 <div class="btn-group"> 25 <div class="btn-group">
@@ -222,4 +222,12 @@ @@ -222,4 +222,12 @@
222 return false; 222 return false;
223 } 223 }
224 }); 224 });
  225 +
  226 + // 推送百度
  227 + function sendUrl() {
  228 + confirm('确定推送?');
  229 + $.get('/keywords/sendUrl',function(data){
  230 + alert(data.message);
  231 + })
  232 + }
225 </script> 233 </script>
@@ -48,32 +48,12 @@ const defaults = { @@ -48,32 +48,12 @@ const defaults = {
48 platformApi: 'http://192.168.102.48:8088/', 48 platformApi: 'http://192.168.102.48:8088/',
49 search: 'http://192.168.102.216:8080/yohosearch/', 49 search: 'http://192.168.102.216:8080/yohosearch/',
50 50
51 - // prod  
52 - // singleApi: 'http://single.yoho.cn/',  
53 - // api: 'http://api.yoho.cn/',  
54 - // service: 'http://service.yoho.cn/',  
55 - // serviceNotify: 'http://service.yoho.cn/',  
56 - // global: 'http://api-global.yohobuy.com/',  
57 - // platformApi: 'http://172.16.6.210:8088/',  
58 - // search: 'http://search.yohoops.org/yohosearch/',  
59 -  
60 - // gray  
61 - // singleApi: 'http://single.gray.yohops.com/',  
62 - // api: 'http://api.gray.yohops.com/',  
63 - // service: 'http://service.gray.yohops.com/',  
64 - // platformApi: 'http://172.16.6.210:8088/',  
65 -  
66 - // dev  
67 - // api: 'http://dev-api.yohops.com:9999/',  
68 - // service: 'http://dev-service.yohops.com:9999/',  
69 - // serviceNotify: 'http://dev-service.yohops.com:9999/',  
70 - // singleApi: 'http://dev-api.yohops.com:9999/',  
71 - // platformApi: 'http://192.168.102.48:8088/',,  
72 -  
73 imSocket: 'ws://socket.yohobuy.com:10240', 51 imSocket: 'ws://socket.yohobuy.com:10240',
74 imCs: 'http://im.yohobuy.com/api', 52 imCs: 'http://im.yohobuy.com/api',
75 - unionApi: 'http://172.16.6.90:8080/' 53 + unionApi: 'http://172.16.6.90:8080/',
  54 +
76 }, 55 },
  56 + baiduToken: '0lSAO4ZxEKsYopMG',
77 57
78 // redis key prefix 58 // redis key prefix
79 singleBrandKeyPre: 'golobal:yoho:single_brand:', // key: 品牌id, val: 名牌名 59 singleBrandKeyPre: 'golobal:yoho:single_brand:', // key: 品牌id, val: 名牌名