背景

hexo-addrlink插件采用对文件名使用crc32的方式

这种方式不允许不同文件夹内有同名文件

因此需要进行改进

脚本

对绝对路径使用crc校验,这样便可允许不同文件名

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
import zlib
import sys
import os
import time
# 用于生成新博客,带永久链接,crc32算法
filePath = sys.argv[1] + ".md"
title = filePath[filePath.rfind('/') + 1:len(filePath) - 3]
if not os.path.exists(filePath):
os.system(r"touch {}".format(filePath))
print(filePath, "has been touched")
else:
raise FileExistsError("file "+filePath+" aready exists!")
file_mtime = time.localtime(os.stat(filePath).st_mtime)
date_str = time.strftime("%Y-%m-%d %H:%M:%S", file_mtime)

rel_path = os.path.relpath(filePath, start="/Users/raccoon/blogs/source/_posts")
rel_path = rel_path[:rel_path.rfind('/')].replace("/", ", ")
catstr = "categories: [" + rel_path + "]\n"

with open(filePath, "w") as w_file:
w_file.write("---\n")
w_file.write("addrlink: " + addrlink + "\n")
w_file.write("title: " + title + "\n")
w_file.write("date: " + date_str + "\n")
w_file.write("updated: " + date_str + "\n")
w_file.write("tags: \n")
w_file.write(catstr)
w_file.write("hidden: false\n")
w_file.write("password: \n")
w_file.write("cover: \n")
w_file.write("description: \n")
w_file.write("---\n\n")

对已写的博客新增addrlink字段

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
import zlib
import sys
import os
import time
# 用于强制将未生成addrlink的都打上标记
# 遍历_posts目录及其子目录下的所有文件
posts_dir = "source/_posts"
for root, dirs, files in os.walk(posts_dir):
dirs.sort()
for file in sorted(files):
filePath = os.path.join(root, file)
filePath = os.path.abspath(filePath)
if not filePath.endswith(".md"):
continue
addrlink = hex(zlib.crc32(str.encode(filePath)))[2:]
title = filePath[filePath.rfind('/') + 1:len(filePath) - 3]
file_mtime = time.localtime(os.stat(filePath).st_mtime)
date_str = time.strftime("%Y-%m-%d %H:%M:%S", file_mtime)

rel_path = os.path.relpath(filePath, start="/Users/raccoon/blogs/source/_posts")
rel_path = rel_path[:rel_path.rfind('/')].replace("/", ", ")
catstr = "categories: [" + rel_path + "]\n"
with open(filePath, "r") as r_file:
lines = r_file.readlines()
with open(filePath, "w") as w_file:
for line in lines:
if line.startswith("title"):
w_file.write("addrlink: " + addrlink + "\n")
print(addrlink, "has been written to", filePath)
w_file.write(line)
else:
w_file.write(line)

消除上面的脚本对各个文件写入,改变了更新时间的副作用

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# 用于将head写入mtime强制(通常用于在脚本执行后, 确认此次脚本涉及的文件均为进行实质性内容更改,可进行mtime的修正)

import os
import time

if __name__ == "__main__":

posts_dir = "source/_posts"

# 遍历_posts目录及其子目录下的所有文件
for root, dirs, files in os.walk(posts_dir):
dirs.sort()
for file in sorted(files):
file_path = os.path.join(root, file)
file_path = os.path.abspath(file_path)
if not file_path.endswith(".md"):
continue
with open(file_path, 'r') as r_file:
lines = r_file.readlines()
for line in lines:
if line.startswith("updated"):
timestr = line.replace("updated: ", "").strip()
date_time = time.strptime(timestr, "%Y-%m-%d %H:%M:%S")
touch_str = time.strftime("%Y%m%d%H%M", date_time)
touch_cmd = "touch -mt " + touch_str + " " + file_path
ls_cmd = "ls -l " + file_path
os.system(touch_cmd)
print(touch_str, "has been forced added to", file_path, "Result:")
os.system(ls_cmd)

检查crc是否有重复

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
import os
from collections import Counter
# 找出crc32重复元素
posts_dir = "source/_posts"
for root, dirs, files in os.walk(posts_dir):
dirs.sort()
for file in sorted(files):
filePath = os.path.join(root, file)
filePath = os.path.abspath(filePath)
if not filePath.endswith(".md"):
continue
with open(filePath, "r") as file:
lines = file.readlines()
for line in lines:
if line.startswith("addrlink: "):
addrlinklist.append(line.replace("addrlink: ", "").strip())
break
b = dict(Counter(addrlinklist))
print({key:value for key,value in b.items() if value > 1})