Skip to content

Instantly share code, notes, and snippets.

@timcsy
Last active May 7, 2024 09:09
Show Gist options
  • Save timcsy/aa1c67483b848718ba98e900074a1a6a to your computer and use it in GitHub Desktop.
Save timcsy/aa1c67483b848718ba98e900074a1a6a to your computer and use it in GitHub Desktop.
備份 HackMD 上面之前上傳到 imgur 的圖片。請到 HackMD 的設定頁面把所有的筆記下載下來,放在 Notes 資料夾裡面,然後把這個 imgur.py 檔放在外面,執行 python imgur.py 之後就可以備份囉!備份後的圖將存到 imgur 資料夾
import os
from glob import glob
import re
import urllib.request
paths = glob('Notes/*.md')
results = []
for path in paths:
with open(path, 'r', encoding='UTF-8') as fin:
data = fin.read()
results += re.findall("https://i.imgur.com/[^\ \n)]*", data)
if not os.path.exists('imgur'):
os.makedirs('imgur')
i = 0
for imgURL in results:
i += 1
try:
urllib.request.urlretrieve(imgURL, 'imgur/' + os.path.basename(imgURL))
except:
print("error on "+ imgURL)
print('\r' + str(i) + '/' + str(len(results)), end='')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment