add 百度链接推送

This commit is contained in:
xugaoyi 2019-12-25 22:11:41 +08:00
parent e2378abfdb
commit d373b662d7
6 changed files with 81 additions and 45 deletions

17
.github/workflows/baiduPush.yml vendored Normal file
View File

@ -0,0 +1,17 @@
## 利用GitHub Actions每天定时给百度推送链接提高收录率 ##
name: baiduPush
# 两种触发方式一、push代码二、每天国际标准时间23点北京时间+8即早上7点运行
on:
push:
schedule:
- cron: '0 23 * * *'
jobs:
bot:
runs-on: ubuntu-latest # 运行环境为最新版的Ubuntu
steps:
- name: 'Checkout codes' # 步骤一,获取仓库代码
uses: actions/checkout@v1
- name: 'Run baiduPush.sh' # 步骤二执行sh命令文件
run: bash ./baiduPush.sh # 注意,运行目录和文件是同一目录

11
baiduPush.sh Normal file
View File

@ -0,0 +1,11 @@
#!/usr/bin/env sh
# 确保脚本抛出遇到的错误
set -e
node utils/baiduPush.js # 生成所有链接文件
# 百度链接推送
curl -H 'Content-Type:text/plain' --data-binary @urls.txt "http://data.zz.baidu.com/urls?site=evanblogweb.com&token=T5PEAzhGaPNbjQ2X"
rm -rf urls.txt # 灭迹

View File

@ -54,10 +54,12 @@ module.exports = {
owner: 'xugaoyi', // GitHub仓库所有者 owner: 'xugaoyi', // GitHub仓库所有者
admin: ['xugaoyi'], // 对仓库有写权限的人 admin: ['xugaoyi'], // 对仓库有写权限的人
distractionFreeMode: false, distractionFreeMode: false,
id: "<%- (window.location.origin + (frontmatter.to.path || window.location.pathname)).slice(-50) %>", // 页面的唯一标识,长度不能超过50 id: "<%- frontmatter.permalink.slice(-16) %>", // 页面的唯一标识,长度不能超过50
title: "「评论」来自 <%- window.location.origin + (frontmatter.to.path || window.location.pathname) %>", // GitHub issue 的标题 title: "「评论」<%- frontmatter.title %>", // GitHub issue 的标题
// id: "<%- (window.location.origin + (frontmatter.to.path || window.location.pathname)).slice(-50) %>",
// title: "「评论」<%- window.location.origin + (frontmatter.to.path || window.location.pathname) %>",
labels: ["Gitalk", "Comment"], // GitHub issue 的标签 labels: ["Gitalk", "Comment"], // GitHub issue 的标签
body:"<%- window.location.origin + (frontmatter.to.path || window.location.pathname) %>" // GitHub issue 的内容 body:"页面:<%- window.location.origin + (frontmatter.to.path || window.location.pathname) %>" // GitHub issue 的内容
} }
} }
] ]

27
utils/baiduPush.js Normal file
View File

@ -0,0 +1,27 @@
/**
* 百度链接推送文件生成
*/
const fs = require('fs');
const path = require('path');
const MD5 = require('md5.js');
const { readFileList, docsRoot, PREFIX } = require('./frontmatter');
const urlsRoot = path.join(__dirname, '..', 'urls.txt'); // 百度链接推送文件
const DOMAIN = 'https://evanblogweb.com'
main();
/**
* 主体函数
*/
function main() {
fs.writeFileSync(urlsRoot, DOMAIN)
const files = readFileList(docsRoot); // 读取所有md文件
files.forEach( file => {
const link = `\r\n${DOMAIN}${PREFIX}${new MD5().update(file.name).digest('hex').substring(0,16)}/`;
console.log(link);
fs.appendFileSync(urlsRoot, link);
})
}

View File

@ -1,29 +0,0 @@
var fs = require('fs')
var baidu = `<script>
var _hmt = _hmt || [];
(function() {
var hm = document.createElement("script");
hm.src = "https://hm.baidu.com/hm.js?19a1f669e77c8a8c0bf92f95cca4c782";
var s = document.getElementsByTagName("script")[0];
s.parentNode.insertBefore(hm, s);
})();
</script>`
function writeJson() {
//现将json文件读出来
fs.readFile('./docs/.vuepress/dist/index.html', function(err, data) {
if (err) {
return console.error(err)
}
var person = data.toString() //将二进制的数据转换为字符串
var pos = person.indexOf('<meta charset="utf-8">')
person = person.slice(0, pos) + baidu + person.slice(pos)
fs.writeFile('./docs/.vuepress/dist/index.html', person, function(err) {
if (err) {
console.error(err)
}
console.log('----------新增成功-------------')
})
})
}
writeJson() //执行一下;

View File

@ -8,6 +8,7 @@ const MD5 = require('md5.js');
const arg = process.argv.splice(2)[0]; // 获取命令行 传入参数 const arg = process.argv.splice(2)[0]; // 获取命令行 传入参数
const docsRoot = path.join(__dirname, '..', 'docs'); // docs文件路径 const docsRoot = path.join(__dirname, '..', 'docs'); // docs文件路径
const PREFIX = '/pages/'; // 链接前缀
main(); main();
@ -38,8 +39,8 @@ function main() {
logger.warn(`此文件FrontMatter更新失败${file.filePath}因为它的FrontMatter格式并非自动生成的。`) logger.warn(`此文件FrontMatter更新失败${file.filePath}因为它的FrontMatter格式并非自动生成的。`)
} }
} }
}) })
} }
// 写入FrontMatter // 写入FrontMatter
@ -47,7 +48,8 @@ function writeFrontMatter(file, dataStr) {
const stat = fs.statSync(file.filePath); const stat = fs.statSync(file.filePath);
const date = stat.birthtime; // 创建时间 const date = stat.birthtime; // 创建时间
const dateStr = `${date.getFullYear()}-${zero(date.getMonth()+1)}-${zero(date.getDate())}`; const dateStr = `${date.getFullYear()}-${zero(date.getMonth()+1)}-${zero(date.getDate())}`;
const newData = `---\r\ntitle: ${file.name}\r\ndate: ${dateStr}\r\npermalink: /pages/${new MD5().update(file.name).digest('hex').substring(0,16)}\r\n---\r\n` + dataStr; const permalink = `${PREFIX}${new MD5().update(file.name).digest('hex').substring(0,16)}`;
const newData = `---\r\ntitle: ${file.name}\r\ndate: ${dateStr}\r\npermalink: ${permalink}\r\n---\r\n` + dataStr;
fs.writeFileSync(file.filePath, newData); // 写入 fs.writeFileSync(file.filePath, newData); // 写入
} }
@ -81,21 +83,27 @@ function readFileList(dir, filesList = []) {
return filesList; return filesList;
} }
// 抛出给baiduPush.js调用
module.exports = {
readFileList,
docsRoot,
PREFIX
};
/** /**
* 读取指定目录下的文件绝对路径 * 读取指定目录下的文件绝对路径
* @param {String} root 指定的目录 * @param {String} root 指定的目录
*/ */
function readTocs(root){ // function readTocs(root){
const result = []; // const result = [];
const files = fs.readdirSync(root); // 方法:读取目录,返回数组成员是root底下所有的目录名 (包含文件文件夹和文件) // const files = fs.readdirSync(root); // 方法:读取目录,返回数组成员是root底下所有的目录名 (包含文件文件夹和文件)
files.forEach(name => { // files.forEach(name => {
const file = path.resolve(root, name); // 方法:将路径或路径片段的序列解析为绝对路径 // const file = path.resolve(root, name); // 方法:将路径或路径片段的序列解析为绝对路径
if (fs.statSync(file).isDirectory() && name !== '.vuepress') { // 是否为文件夹目录,并排除.vuepress文件 // if (fs.statSync(file).isDirectory() && name !== '.vuepress') { // 是否为文件夹目录,并排除.vuepress文件
result.push(file); // result.push(file);
} // }
}) // })
return result; // return result;
} // }