...
|
...
|
@@ -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; |
...
|
...
|
|