|
|
|
@ -1,8 +1,6 @@ |
|
|
|
|
import os |
|
|
|
|
import time |
|
|
|
|
import tempfile |
|
|
|
|
import threading |
|
|
|
|
import urllib.parse |
|
|
|
|
import pycurl |
|
|
|
|
from hashlib import sha256 |
|
|
|
|
from io import BytesIO |
|
|
|
@ -37,7 +35,8 @@ class URLFile: |
|
|
|
|
self._curl = self._tlocal.curl |
|
|
|
|
except AttributeError: |
|
|
|
|
self._curl = self._tlocal.curl = pycurl.Curl() |
|
|
|
|
mkdirs_exists_ok(Paths.download_cache_root()) |
|
|
|
|
if not self._force_download: |
|
|
|
|
mkdirs_exists_ok(Paths.download_cache_root()) |
|
|
|
|
|
|
|
|
|
def __enter__(self): |
|
|
|
|
return self |
|
|
|
@ -65,12 +64,13 @@ class URLFile: |
|
|
|
|
def get_length(self): |
|
|
|
|
if self._length is not None: |
|
|
|
|
return self._length |
|
|
|
|
|
|
|
|
|
file_length_path = os.path.join(Paths.download_cache_root(), hash_256(self._url) + "_length") |
|
|
|
|
if os.path.exists(file_length_path) and not self._force_download: |
|
|
|
|
if not self._force_download and os.path.exists(file_length_path): |
|
|
|
|
with open(file_length_path) as file_length: |
|
|
|
|
content = file_length.read() |
|
|
|
|
self._length = int(content) |
|
|
|
|
return self._length |
|
|
|
|
content = file_length.read() |
|
|
|
|
self._length = int(content) |
|
|
|
|
return self._length |
|
|
|
|
|
|
|
|
|
self._length = self.get_length_online() |
|
|
|
|
if not self._force_download and self._length != -1: |
|
|
|
@ -173,24 +173,4 @@ class URLFile: |
|
|
|
|
|
|
|
|
|
@property |
|
|
|
|
def name(self): |
|
|
|
|
"""Returns a local path to file with the URLFile's contents. |
|
|
|
|
|
|
|
|
|
This can be used to interface with modules that require local files. |
|
|
|
|
""" |
|
|
|
|
if self._local_file is None: |
|
|
|
|
_, ext = os.path.splitext(urllib.parse.urlparse(self._url).path) |
|
|
|
|
local_fd, local_path = tempfile.mkstemp(suffix=ext) |
|
|
|
|
try: |
|
|
|
|
os.write(local_fd, self.read()) |
|
|
|
|
local_file = open(local_path, "rb") |
|
|
|
|
except Exception: |
|
|
|
|
os.remove(local_path) |
|
|
|
|
raise |
|
|
|
|
finally: |
|
|
|
|
os.close(local_fd) |
|
|
|
|
|
|
|
|
|
self._local_file = local_file |
|
|
|
|
self.read = self._local_file.read |
|
|
|
|
self.seek = self._local_file.seek |
|
|
|
|
|
|
|
|
|
return self._local_file.name |
|
|
|
|
return self._url |
|
|
|
|