1
0
mirror of https://github.com/mikf/gallery-dl.git synced 2024-11-25 04:02:32 +01:00

add option to set alternate location of .part files (#29)

Note: The path set for 'downloader.*.part-directory' needs to point to an
already existing directory.
This commit is contained in:
Mike Fährmann 2017-10-26 00:07:32 +02:00
parent ea8ca4cfa4
commit caf26412dd
No known key found for this signature in database
GPG Key ID: 5680CA389D365A88
7 changed files with 38 additions and 7 deletions

View File

@ -107,6 +107,15 @@ Description Controls the use of ``.part`` files during file downloads.
=========== =====
downloader.part-directory
-------------------------
=========== =====
Type ``string``
Default ``null``
Description Path to an existing directory to store ``.part`` files in.
=========== =====
downloader.http.retries
-----------------------
=========== =====

View File

@ -4,6 +4,7 @@
"downloader":
{
"part": true,
"part-directory": null,
"http":
{
"retries": 5,

View File

@ -17,6 +17,7 @@ class DownloaderBase():
"""Base class for downloaders"""
retries = 1
part = True
partdir = None
def __init__(self, session, output):
self.session = session
@ -28,6 +29,9 @@ class DownloaderBase():
"""Download the resource at 'url' and write it to a file-like object"""
try:
self.download_impl(url, pathfmt)
except Exception:
print()
raise
finally:
# remove file from incomplete downloads
if self.downloading and not self.part:
@ -42,16 +46,16 @@ class DownloaderBase():
msg = ""
if self.part:
pathfmt.part_enable()
pathfmt.part_enable(self.partdir)
while True:
self.reset()
if tries:
self.out.error(pathfmt.path, msg, tries, self.retries)
if tries >= self.retries:
return False
time.sleep(1)
tries += 1
self.reset()
# check for .part file
filesize = pathfmt.part_size()

View File

@ -22,6 +22,7 @@ class Downloader(DownloaderBase):
timeout = _conf("timeout", 30)
verify = _conf("verify", True)
part = _conf("part", True)
partdir = util.expand_path(_conf("part-directory"))
def __init__(self, session, output):
DownloaderBase.__init__(self, session, output)

View File

@ -9,11 +9,16 @@
"""Downloader module for text: URLs"""
from .common import DownloaderBase
from .. import config
from .. import config, util
def _conf(key, default=None):
return config.interpolate(("downloader", "text", key), default)
class Downloader(DownloaderBase):
part = config.interpolate(("downloader", "text", "part"), True)
part = _conf("part", True)
partdir = util.expand_path(_conf("part-directory"))
def __init__(self, session, output):
DownloaderBase.__init__(self, session, output)

View File

@ -395,28 +395,37 @@ class PathFormat():
self.path = self.directory + filename
self.realpath = self.realdirectory + filename
def part_enable(self):
def part_enable(self, part_directory=None):
"""Enable .part file usage"""
if self.has_extension:
self.partpath = self.realpath + ".part"
else:
self.set_extension("part", False)
self.partpath = self.realpath
if part_directory:
self.partpath = os.path.join(
part_directory,
os.path.basename(self.partpath),
)
def part_size(self):
if self.partpath and os.path.isfile(self.partpath):
"""Return size of .part file"""
if self.partpath:
try:
return os.path.getsize(self.partpath)
return os.stat(self.partpath).st_size
except OSError:
pass
return 0
def part_move(self):
"""Rename .part file to its actual filename"""
try:
os.rename(self.partpath, self.realpath)
return
except OSError:
pass
shutil.copyfile(self.partpath, self.realpath)
os.unlink(self.partpath)
def _exists_abort(self):
if self.has_extension and os.path.exists(self.realpath):

View File

@ -74,6 +74,8 @@ skip = [
"archivedmoe", "archiveofsins", "thebarchive",
# temporary issues
"imgtrex",
"imagefap",
"flickr",
]
# enable selective testing for direct calls
if __name__ == '__main__' and len(sys.argv) > 1: