download_deps.py 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. import os
  2. import requests
  3. DEPS_FILE = "deps.txt"
  4. SAVE_DIR = "downloads"
  5. os.makedirs(SAVE_DIR, exist_ok=True)
  6. def download_file(filename, url):
  7. save_path = os.path.join(SAVE_DIR, filename)
  8. print(f"\nStart download:")
  9. print(f"Filename: {filename}")
  10. print(f"Link: {url}")
  11. r = requests.get(url, stream=True, timeout=60)
  12. r.raise_for_status()
  13. total = 0
  14. with open(save_path, "wb") as f:
  15. for chunk in r.iter_content(chunk_size=8192):
  16. if chunk:
  17. f.write(chunk)
  18. total += len(chunk)
  19. print(f"Download finished: {save_path}")
  20. print(f"File size: {round(total / 1024 / 1024, 2)} MB")
  21. def main():
  22. with open(DEPS_FILE, "r", encoding="utf-8") as f:
  23. lines = f.readlines()
  24. for line in lines:
  25. line = line.strip()
  26. if not line:
  27. continue
  28. if line.startswith("#"):
  29. continue
  30. try:
  31. filename, url = line.split("|", 1)
  32. download_file(
  33. filename.strip(),
  34. url.strip()
  35. )
  36. except Exception as e:
  37. print(f"\nDownload failed:")
  38. print(line)
  39. print(e)
  40. if __name__ == "__main__":
  41. main()