#!/usr/bin/env python3# coding=utf-8#main.py__author__='https://blog.mzh.ren/zh/'__version__=(0,1,0)importreimportos,sysimportrequestsdefget_all_markdown_files(path):"""Get all the markdown files with all depth"""markdown_files=[]forroot,dirs,filesinos.walk(path):forfileinfiles:iffile.endswith('.md'):markdown_files.append(os.path.join(root,file))returnmarkdown_filesdefdownload_files_from_markdown(markdown_file):"""Download all the files from the markdown file"""markdown_file_path=os.path.dirname(markdown_file)items=get_date_links_from_markdown(markdown_file)foriteminitems:filename=markdown_file_path+'/'+get_filename_from_url(item)download_file(item[1],filename)defget_date_links_from_markdown(markdown_file):"""Get all the links from the markdown file"""content=read_markdown_file(markdown_file)date_and_links=re.findall(r'(\d{4}-\d{2}-\d{2}) \[download 4k\]\((https[^\)]+)\)',content)returndate_and_linksdefread_markdown_file(markdown_file):"""Read the markdown file and return the content"""withopen(markdown_file,'r')asf:content=f.read()returncontentdefdownload_file(url,filename):"""Download the file from the url and save it as filename"""ifnotcheck_file_exist(filename):print('Downloading {} to {}'.format(url,filename))r=requests.get(url)withopen(filename,'wb')asf:f.write(r.content)defget_filename_from_url(date_and_link):"""Get the filename from the date_and_link"""# ('2022-10-09', 'https://cn.bing.com/th?id=OHR.GlassOctopus_EN-US6394802515_UHD.jpg')returndate_and_link[0]+'_'+get_url_pram_value(date_and_link[1],'id')defget_url_pram_value(url,param):"""Get the value of the param from the url"""regResult=re.search(r'{}=([^&]+)'.format(param),url)ifregResult:returnregResult.group(1)defcheck_file_exist(filename):"""Check if the file exists"""returnos.path.exists(filename)if__name__=='__main__':iflen(sys.argv)>1:path=sys.argv[1]else:path='./picture'markdown_files=get_all_markdown_files(path)formarkdown_fileinmarkdown_files:download_files_from_markdown(markdown_file)