优化download_and_extract功能:1) 相同URL跳过下载 2) 下载信息保存到解压目录

This commit is contained in:
yhydev
2025-12-13 00:35:24 +08:00
parent 77a8f51e5d
commit d3b2f26f74

View File

@@ -123,6 +123,16 @@ def download_and_extract(download_url, extract_dir='.', password=None):
# 确保解压目录存在 # 确保解压目录存在
os.makedirs(extract_dir, exist_ok=True) os.makedirs(extract_dir, exist_ok=True)
# 检查是否与最后一次下载的URL相同
last_upload_file = LAST_UPLOAD_FILE
if os.path.exists(last_upload_file):
with open(last_upload_file, 'r') as f:
last_data = json.load(f)
if last_data.get('download_url') == download_url:
print(f"URL {download_url} is the same as last download. Skipping download.")
return True
# 下载ZIP文件到临时目录 # 下载ZIP文件到临时目录
with tempfile.NamedTemporaryFile(suffix='.zip', delete=False) as tmp: with tempfile.NamedTemporaryFile(suffix='.zip', delete=False) as tmp:
zip_path = tmp.name zip_path = tmp.name
@@ -167,6 +177,21 @@ def download_and_extract(download_url, extract_dir='.', password=None):
raise e raise e
print(f"Download and extraction completed successfully.") print(f"Download and extraction completed successfully.")
# 将下载信息保存到解压目录
download_info = {
'download_url': download_url,
'extract_dir': extract_dir,
'timestamp': os.path.getmtime(zip_path),
'encrypted': bool(password),
'extracted_files': len(os.listdir(extract_dir)) if os.path.exists(extract_dir) else 0
}
download_info_file = os.path.join(extract_dir, '.download_info.json')
with open(download_info_file, 'w') as f:
json.dump(download_info, f, indent=2)
print(f"Download information saved to {download_info_file}")
return True return True
except (requests.exceptions.RequestException, zipfile.BadZipFile, IOError, Exception) as e: except (requests.exceptions.RequestException, zipfile.BadZipFile, IOError, Exception) as e:
raise Exception(f"Failed to download and extract: {str(e)}") raise Exception(f"Failed to download and extract: {str(e)}")