You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

http_download.py 901 B

12345678910111213141516171819202122232425262728293031323334353637383940
  1. # -*- coding: utf-8 -*-
  2. import hashlib
  3. import os
  4. import shutil
  5. from tempfile import NamedTemporaryFile
  6. import requests
  7. from megfile import smart_copy, smart_getmd5, smart_getsize
  8. from tqdm import tqdm
  9. from ..logger import get_logger
  10. logger = get_logger(__name__)
  11. CHUNK_SIZE = 1024
  12. HTTP_CONNECTION_TIMEOUT = 5
  13. class HTTPDownloadError(BaseException):
  14. r"""The class that represents http request error."""
  15. class Bar:
  16. def __init__(self, total=100):
  17. self._bar = tqdm(total=total, unit="iB", unit_scale=True, ncols=80)
  18. def __call__(self, bytes_num):
  19. self._bar.update(bytes_num)
  20. def download_from_url(url: str, dst: str):
  21. r"""Downloads file from given url to ``dst``.
  22. Args:
  23. url: source URL.
  24. dst: saving path.
  25. """
  26. dst = os.path.expanduser(dst)
  27. smart_copy(url, dst, callback=Bar(total=smart_getsize(url)))
  28. return smart_getmd5(dst)