Authored by 周少峰

sendurl to baidu

... ... @@ -16,6 +16,7 @@ const rp = require('request-promise');
const config = require('../../../config/config');
const singleBrandKeyPre = config.singleBrandKeyPre;
const singleSortKeyPre = config.singleSortKeyPre;
const qs = require('querystring');
const multiAsync=(multi)=>{
return multi.execAsync().then(function(res) {
... ... @@ -361,5 +362,135 @@ r.post('/expand/del', async(ctx) => {
};
});
});
const baiduUrls = {
urls: 'http://data.zz.baidu.com/urls',
update: 'http://data.zz.baidu.com/update',
del: 'http://data.zz.baidu.com/del'
};
/**
* 将链接推送到百度站长
* @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 true;
//return rp(options).then(result => {
// logger.info(Object.assign(params, result, {length: urls.length}));
// if (result) {
// return true;
// } else {
// return false;
// }
//});
};
/**
* 推送urls
* @params limit url条数
* @return boolean
*/
const sendUrls = (limit) => {
// 调用limit条未推送关键词
let mysql = new Mysql();
console.log('sendUrl exe');
return mysql.query(`select id, keyword from seo_keywords where is_push=0 order by id asc limit ${limit}`).then(d => {
let pcPages = [],
wapPages = [];
console.log(d);
_.forEach(d, (val) => {
pcPages.push(`https://www.yohobuy.com/chanpin/${val.id}.html`);
wapPages.push(`https://m.yohobuy.com/chanpin/${val.id}.html`);
})
// 根据 site 推送相应站点
return Promise.all([sendUrlsToBaidu({site: 'https://www.yohobuy.com'}, pcPages),sendUrlsToBaidu({site: 'https://m.yohobuy.com'}, wapPages)]).then(result => {
let updateSql = `UPDATE seo_keywords SET is_push = 1 WHERE id >= ${_.get(_.first(d), 'id')} and id <= ${_.get(_.last(d), 'id')}`;
console.log(updateSql);
// 更新关键词推送状态
return mysql.query(updateSql).then(updateRes => {
console.log(updateRes);
return updateRes;
});
})
});
}
const getPushTotal = () => {
let mysql = new Mysql();
return mysql.query(`select count(id) from seo_keywords where is_push=0`).then(d=>{
return _.get(d, '0.count(id)', 0);
});
}
/**
* 推送url到百度(获取2000条未推送关键词,监测是否还有未推关键词,如果有再推2000,直到全部推送)
*/
r.get('/sendUrl', async(ctx, next) => {
let result = {
code: 200,
message: ''
};
let total = await getPushTotal(); // 待推送总数
let limit = 20; // 每次推送的条数
if (total === 0) {
result.message = '没有需要推送的词!';
} else {
let intervalTime = Math.floor(total/limit) + 1;
console.log(total);
console.log(intervalTime);
// 推送2000条
let interval = setInterval(() => {
sendUrls(limit);
if (--intervalTime < 1) {
clearInterval(interval);
}
}, 1000);
result.message = `${intervalTime*10} 秒后查看推送结果`;
}
ctx.response.body = result;
});
module.exports = r;
... ...
... ... @@ -19,7 +19,7 @@
<div class="panel-body">
<label style="margin-right:20px;"><input id="allSelected" type="checkbox" style="margin-right:5px;">全选</label>
<a class="btn btn-default deleteAll">删除</a>
<a class="btn btn-default" href="javascript:sendUrl();">推送百度</a>
<div class="input-append pull-right">
<form id="query-form" action="/keywords/expand" class="query-form" method="get">
<div class="btn-group">
... ... @@ -222,4 +222,12 @@
return false;
}
});
// 推送百度
function sendUrl() {
confirm('确定推送?');
$.get('/keywords/sendUrl',function(data){
alert(data.message);
})
}
</script>
\ No newline at end of file
... ...
... ... @@ -48,32 +48,12 @@ const defaults = {
platformApi: 'http://192.168.102.48:8088/',
search: 'http://192.168.102.216:8080/yohosearch/',
// prod
// singleApi: 'http://single.yoho.cn/',
// api: 'http://api.yoho.cn/',
// service: 'http://service.yoho.cn/',
// serviceNotify: 'http://service.yoho.cn/',
// global: 'http://api-global.yohobuy.com/',
// platformApi: 'http://172.16.6.210:8088/',
// search: 'http://search.yohoops.org/yohosearch/',
// gray
// singleApi: 'http://single.gray.yohops.com/',
// api: 'http://api.gray.yohops.com/',
// service: 'http://service.gray.yohops.com/',
// platformApi: 'http://172.16.6.210:8088/',
// dev
// api: 'http://dev-api.yohops.com:9999/',
// service: 'http://dev-service.yohops.com:9999/',
// serviceNotify: 'http://dev-service.yohops.com:9999/',
// singleApi: 'http://dev-api.yohops.com:9999/',
// platformApi: 'http://192.168.102.48:8088/',,
imSocket: 'ws://socket.yohobuy.com:10240',
imCs: 'http://im.yohobuy.com/api',
unionApi: 'http://172.16.6.90:8080/'
unionApi: 'http://172.16.6.90:8080/',
},
baiduToken: '0lSAO4ZxEKsYopMG',
// redis key prefix
singleBrandKeyPre: 'golobal:yoho:single_brand:', // key: 品牌id, val: 名牌名
... ...