import re,os,time
import subprocess

save_path='D:\\Download'#下载文件储存路径
name = 'cn_windows_10_multiple_editions_x64_dvd_6848463.iso'
ed2k = "ed2k://|file|cn_windows_10_multiple_editions_x64_dvd_6848463.iso|4303300608|94FD861E82458005A9CA8E617379856A|/"

def delete_download_directory():
    if os.path.exists(save_path):
        shutil.rmtree(save_path)
        print(f"{save_path} 已被删除")
        
def check_start(file_name):#判断文件是否开始下载
    tmp=file_name+'.xltd.cfg'
    return os.path.exists(os.path.join(save_path,tmp))

def check_end(fiename):#检查文件是否下载成功
    return os.path.exists(os.path.join(save_path,fiename))

def download(name,url):#下载文件
    os.system(r'""C:\Program Files (x86)\Thunder Network\Thunder\Program\ThunderStart.exe"" {url}'.format(url=url))#迅雷ThunderStart.exe的路径
    time.sleep(10)
    if check_start(name):
    print("正在下载 {}".format(name))
    time.sleep(2)
    while True:
        if check_end(name):
            return True

def run():
    print("=======自动下载程序启动=========")
    if check_end(name):
        delete_download_directory()
        continue
    if download(name, url)
        print("======下载完成======",end='\n')
        
while True:
    run()

