Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

update #2

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion md_image_backup.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ def download(file_path):
# img name spell
urlname = img_url.split(u"/")
img_name = filename + '_' + \
str(i) + '_' + img_quote + str(urlname[len(urlname) - 1])
str(i) + '_md_' + img_quote + str(urlname[len(urlname) - 1])
print img_name, '~~~', img_url
# write to file
f_img = open('img/' + img_name, 'wb')
Expand Down
59 changes: 43 additions & 16 deletions md_image_backup_py3.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,13 @@ def backup():
# 备份文件夹下的所有img
search('你的markdown文件路径', '.md')

# example
# def backup():
# try:
# download(str('E:\\blog\\hojunBlog19.05.21\\source\\links\\' + sys.argv[1]))
# except IndexError:
# search('E:\\blog\\hojunBlog19.05.21\\source\\links', '.md')

def search(path, word):
for filename in os.listdir(path):
fp = os.path.join(path, filename)
Expand All @@ -34,24 +41,44 @@ def download(file_path):
text = f_md.read().decode('utf-8')
# regex
# img_reg = r'\!{1}\[(.*?)\]\((.*?)\)'
# 匹配封面图
# photos:
# - imgurl
# result = re.findall('photos\:\n \- (.*?)\n', text)
# 匹配封面图 photos: imgurl
# result = re.findall('photos\: (.*?)\n', text)
# 匹配fancybox插件语法 {% fb_img imgurl desc %}
# result = re.findall('\{\% fb_img (.*?) ', text)
# 匹配html <img src="imgurl">
# result = re.findall('src\=\"(.*?)\"', text)
# 匹配markdown ![XXX](imgurl)
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()
try:
for i in range(len(result)):
# markdown 有两个匹配用这个
img_quote = result[i][0]
img_url = result[i][1]
# 其他的只有一个匹配用这个 注意之后的代码要哦删掉img_quote
# img_url = result[i]
# 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) + '_md_' + img_quote + str(urlname[len(urlname) - 1])
img_name = img_quote + str(urlname[len(urlname) - 1])
# img_name = 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()
except:
print("Unexpected error:", sys.exc_info()[0])
f_md.close()

backup()