-
Notifications
You must be signed in to change notification settings - Fork 3
/
md_image_backup_py3.py
57 lines (50 loc) · 1.52 KB
/
md_image_backup_py3.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
# coding=utf-8
import sys
import os
import re
import requests
import urllib.request
def backup():
try:
# 备份指定文件的img
download(str('你的markdown文件路径' + sys.argv[1]))
except IndexError:
# 备份文件夹下的所有img
search('你的markdown文件路径', '.md')
def search(path, word):
for filename in os.listdir(path):
fp = os.path.join(path, filename)
if os.path.isfile(fp) and word in filename:
print(fp)
download(str(fp))
elif os.path.isdir(fp):
search(fp, word)
def download(file_path):
# filename = "test"
name = file_path.split(u"\\")
filename = name[-1]
f_md = open(file_path, 'rb')
# all text of md file
text = f_md.read().decode('utf-8')
# regex
# img_reg = r'\!{1}\[(.*?)\]\((.*?)\)'
result = re.findall('!\[(.*?)\]\((.*?)\)', text)
print(result)
for i in range(len(result)):
img_quote = result[i][0]
img_url = result[i][1]
# download img
request = urllib.request.Request(img_url)
response = urllib.request.urlopen(request)
img_contents = response.read()
# img name spell
urlname = img_url.split(u"/")
img_name = filename + '_' + \
str(i) + '_' + img_quote + str(urlname[len(urlname) - 1])
print (img_name + '~~~' + img_url)
# write to file
f_img = open('img/' + img_name, 'wb')
f_img.write(img_contents)
f_img.close()
f_md.close()
backup()