-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcdn-img.py
82 lines (67 loc) · 2.21 KB
/
cdn-img.py
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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# -*- coding: utf-8 -*-
"""
-------------------------------------------------
File Name: hexo-img-transfer
Description :
Author : xmz
date: 2020/4/11
-------------------------------------------------
Change Activity:
2020/4/11:
-------------------------------------------------
"""
import os
import re
import shutil
__author__ = 'xmz'
folder = './public/'
img_folder = '../../../img/'
img_files = []
def all_posts():
md_files = []
for fpathe, dirs, fs in os.walk(folder):
for f in fs:
filename = os.path.join(fpathe, f)
if filename.endswith("index.html") and "20" in filename and "archive" not in filename:
# print(filename)
md_files.append(filename)
return md_files
def all_images():
for fpathe, dirs, fs in os.walk(img_folder):
for f in fs:
filename = os.path.join(fpathe, f)
if filename.endswith("png") or filename.endswith("jpg"):
img_files.append(filename)
def copy_img(md_name, imgs):
for i in imgs:
for ai in img_files:
if i in ai:
if not os.path.exists(md_name):
os.makedirs(md_name)
shutil.copy(ai, md_name)
def rw_md(fn, content):
with open(fn, "w", encoding='utf8') as f:
f.writelines(content)
def replace_url():
js_deliver = "https://cdn.jsdelivr.net/gh/xmzzyo/Blog@master/source/_posts/"
md_files = all_posts()
for md in md_files:
print(md)
# md_n = md.split("/")[-1]
# md_n = md_n.replace(".md", "")
# print(md_n)
with open(md, 'r', encoding='utf8') as f:
lines = f.readlines()
for i, l in enumerate(lines):
if re.match(".*<img\\ssrc=\"(.+?)\".*", l):
urls = re.findall("<img\\ssrc=\"(.+?)\"", l)
for url in urls:
if "touxiang" not in url:
l = l.replace(url, js_deliver+url)
lines[i] = l
rw_md(md, lines)
# img_path.append(img[0])
# print(img_path)
# copy_img(md_n, img_path)
if __name__ == "__main__":
replace_url()