From a7e279fa8a94f838b8c05299efc5ef8f5082bbc0 Mon Sep 17 00:00:00 2001 From: honjun Date: Wed, 22 May 2019 09:50:34 +0800 Subject: [PATCH 1/2] update catch exception --- md_image_backup.py | 2 +- md_image_backup_py3.py | 48 ++++++++++++++++++++++++++++-------------- 2 files changed, 33 insertions(+), 17 deletions(-) diff --git a/md_image_backup.py b/md_image_backup.py index e710e51..4389cac 100644 --- a/md_image_backup.py +++ b/md_image_backup.py @@ -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') diff --git a/md_image_backup_py3.py b/md_image_backup_py3.py index 576aa08..345188e 100644 --- a/md_image_backup_py3.py +++ b/md_image_backup_py3.py @@ -14,6 +14,12 @@ def backup(): # 备份文件夹下的所有img search('你的markdown文件路径', '.md') +# 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) @@ -34,24 +40,34 @@ def download(file_path): text = f_md.read().decode('utf-8') # regex # img_reg = r'\!{1}\[(.*?)\]\((.*?)\)' + # result = re.findall('photos\:\n \- (.*?)\n', text) + # result = re.findall('photos\: (.*?)\n', text) + # result = re.findall('\{\% fb_img (.*?) ', text) + # result = re.findall('src\=\"(.*?)\"', text) 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)): + 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) + '_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() \ No newline at end of file From 9c3d5c77a37b1d653b032ffcfb0f9f4e760590d0 Mon Sep 17 00:00:00 2001 From: honjun Date: Wed, 22 May 2019 10:09:56 +0800 Subject: [PATCH 2/2] =?UTF-8?q?update=20=E6=B3=A8=E9=87=8A=E5=92=8C?= =?UTF-8?q?=E6=8D=95=E8=8E=B7=E5=BC=82=E5=B8=B8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- md_image_backup_py3.py | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/md_image_backup_py3.py b/md_image_backup_py3.py index 345188e..1ca46d6 100644 --- a/md_image_backup_py3.py +++ b/md_image_backup_py3.py @@ -14,6 +14,7 @@ def backup(): # 备份文件夹下的所有img search('你的markdown文件路径', '.md') +# example # def backup(): # try: # download(str('E:\\blog\\hojunBlog19.05.21\\source\\links\\' + sys.argv[1])) @@ -40,17 +41,27 @@ 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 # result = re.findall('src\=\"(.*?)\"', text) + # 匹配markdown ![XXX](imgurl) result = re.findall('!\[(.*?)\]\((.*?)\)', text) print(result) 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)