本帖最后由 Jirehlov1234 于 2023-8-27 00:45 编辑
- import os
- import json
- import hashlib
- import time
- import subprocess
- curl_path = "curl.exe"
- base_url = "https://note.youdao.com/yws/public/notebook/d04da3da6f8f011be18e9fe893ed2cfb/subdir/WEB4c64cfb83b69382247b8bc37425e47ac"
- backup_folder = "C:\\Users\\contact\\Desktop\\folder"
- hash_algorithm = hashlib.sha256
- def download_file(url, save_path):
- subprocess.run([curl_path, "-A", "MyApp", "-L", "-o", save_path, url], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
- def get_hash(file_path):
- hash_obj = hash_algorithm()
- with open(file_path, "rb") as f:
- for chunk in iter(lambda: f.read(4096), b""):
- hash_obj.update(chunk)
- return hash_obj.hexdigest()
- def get_original_filename(file_path):
- base_name = os.path.basename(file_path)
- name, _ = os.path.splitext(base_name)
- return name
- while True:
- response = subprocess.run([curl_path, "-A", "MyApp", "-s", base_url], stdout=subprocess.PIPE)
- data = json.loads(response.stdout)[2]
-
- for item in data:
- p = item["p"].split("/")[-1]
- tl = item["tl"]
- dl_url = f"https://note.youdao.com/yws/api/personal/file/{p}?method=download&shareKey=d04da3da6f8f011be18e9fe893ed2cfb"
-
- dl_path = os.path.join(backup_folder, tl)
- temp_dl_path = os.path.join(backup_folder, "_temp.zip")
- download_file(dl_url, temp_dl_path)
- actual_size = os.path.getsize(temp_dl_path)
-
- if actual_size >= 207 * 1024: # Check if the file size is >= 207KB
- temp_hash = get_hash(temp_dl_path)
- final_dl_path = os.path.join(backup_folder, f"{temp_hash}.zip")
-
- if not os.path.exists(final_dl_path):
- os.rename(temp_dl_path, final_dl_path)
- else:
- os.remove(temp_dl_path)
- print(f"Skipping existing file: {final_dl_path}")
- else:
- os.remove(temp_dl_path)
- print(f"File size is too small: {tl}")
- time.sleep(60)
复制代码 |