mirror of
https://github.com/kemayo/leech
synced 2025-12-06 08:22:56 +01:00
New config option: allow_spaces
Determines whether spaces in filenames will be replaced with underscores
This commit is contained in:
parent
2f21280d76
commit
e3c63bce3c
3 changed files with 10 additions and 7 deletions
|
|
@ -156,7 +156,7 @@ def chapter_html(
|
|||
return chapters
|
||||
|
||||
|
||||
def generate_epub(story, cover_options={}, image_options=None, output_filename=None, output_dir=None, normalize=False):
|
||||
def generate_epub(story, cover_options={}, image_options=None, output_filename=None, output_dir=None, normalize=False, allow_spaces=False):
|
||||
if image_options is None:
|
||||
image_options = {
|
||||
'image_fetch': False,
|
||||
|
|
@ -225,5 +225,6 @@ def generate_epub(story, cover_options={}, image_options=None, output_filename=
|
|||
contents=image.read(), filetype='image/png'),
|
||||
],
|
||||
metadata,
|
||||
output_dir=output_dir
|
||||
output_dir=output_dir,
|
||||
allow_spaces=allow_spaces
|
||||
)
|
||||
|
|
|
|||
|
|
@ -18,7 +18,7 @@ This totally started from http://www.manuel-strehl.de/dev/simple_epub_ebooks_wit
|
|||
EpubFile = namedtuple('EbookFile', 'path, contents, title, filetype', defaults=(False, False, "application/xhtml+xml"))
|
||||
|
||||
|
||||
def sanitize_filename(s):
|
||||
def sanitize_filename(s, allow_spaces=False):
|
||||
"""Take a string and return a valid filename constructed from the string.
|
||||
Uses a whitelist approach: any characters not present in valid_chars are
|
||||
removed. Also spaces are replaced with underscores.
|
||||
|
|
@ -31,16 +31,17 @@ def sanitize_filename(s):
|
|||
"""
|
||||
valid_chars = "-_.() %s%s" % (string.ascii_letters, string.digits)
|
||||
filename = ''.join(c for c in s if c in valid_chars)
|
||||
filename = filename.replace(' ', '_') # I don't like spaces in filenames.
|
||||
if not allow_spaces:
|
||||
filename = filename.replace(' ', '_') # I don't like spaces in filenames.
|
||||
return filename
|
||||
|
||||
|
||||
def make_epub(filename, files, meta, compress=True, output_dir=False):
|
||||
def make_epub(filename, files, meta, compress=True, output_dir=False, allow_spaces=False):
|
||||
unique_id = meta.get('unique_id', False)
|
||||
if not unique_id:
|
||||
unique_id = 'leech_book_' + str(uuid.uuid4())
|
||||
|
||||
filename = sanitize_filename(filename)
|
||||
filename = sanitize_filename(filename, allow_spaces)
|
||||
if output_dir:
|
||||
filename = os.path.join(output_dir, filename)
|
||||
epub = zipfile.ZipFile(filename, 'w', compression=compress and zipfile.ZIP_DEFLATED or zipfile.ZIP_STORED)
|
||||
|
|
|
|||
3
leech.py
3
leech.py
|
|
@ -180,7 +180,8 @@ def download(urls, site_options, cache, verbose, normalize, output_dir, **other_
|
|||
'always_convert_images': options.get('always_convert_images', False)
|
||||
},
|
||||
normalize=normalize,
|
||||
output_dir=output_dir or options.get('output_dir', os.getcwd())
|
||||
output_dir=output_dir or options.get('output_dir', os.getcwd()),
|
||||
allow_spaces=options.get('allow_spaces', False)
|
||||
)
|
||||
logger.info("File created: " + filename)
|
||||
else:
|
||||
|
|
|
|||
Loading…
Reference in a new issue