1
0
Fork 0
mirror of https://github.com/kemayo/leech synced 2025-12-06 16:33:16 +01:00

New config option: allow_spaces

Determines whether spaces in filenames will be replaced with underscores
This commit is contained in:
David Lynch 2024-11-30 14:06:30 -06:00
parent 2f21280d76
commit e3c63bce3c
3 changed files with 10 additions and 7 deletions

View file

@ -156,7 +156,7 @@ def chapter_html(
return chapters 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: if image_options is None:
image_options = { image_options = {
'image_fetch': False, 'image_fetch': False,
@ -225,5 +225,6 @@ def generate_epub(story, cover_options={}, image_options=None, output_filename=
contents=image.read(), filetype='image/png'), contents=image.read(), filetype='image/png'),
], ],
metadata, metadata,
output_dir=output_dir output_dir=output_dir,
allow_spaces=allow_spaces
) )

View file

@ -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")) 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. """Take a string and return a valid filename constructed from the string.
Uses a whitelist approach: any characters not present in valid_chars are Uses a whitelist approach: any characters not present in valid_chars are
removed. Also spaces are replaced with underscores. removed. Also spaces are replaced with underscores.
@ -31,16 +31,17 @@ def sanitize_filename(s):
""" """
valid_chars = "-_.() %s%s" % (string.ascii_letters, string.digits) valid_chars = "-_.() %s%s" % (string.ascii_letters, string.digits)
filename = ''.join(c for c in s if c in valid_chars) filename = ''.join(c for c in s if c in valid_chars)
if not allow_spaces:
filename = filename.replace(' ', '_') # I don't like spaces in filenames. filename = filename.replace(' ', '_') # I don't like spaces in filenames.
return filename 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) unique_id = meta.get('unique_id', False)
if not unique_id: if not unique_id:
unique_id = 'leech_book_' + str(uuid.uuid4()) unique_id = 'leech_book_' + str(uuid.uuid4())
filename = sanitize_filename(filename) filename = sanitize_filename(filename, allow_spaces)
if output_dir: if output_dir:
filename = os.path.join(output_dir, filename) filename = os.path.join(output_dir, filename)
epub = zipfile.ZipFile(filename, 'w', compression=compress and zipfile.ZIP_DEFLATED or zipfile.ZIP_STORED) epub = zipfile.ZipFile(filename, 'w', compression=compress and zipfile.ZIP_DEFLATED or zipfile.ZIP_STORED)

View file

@ -180,7 +180,8 @@ def download(urls, site_options, cache, verbose, normalize, output_dir, **other_
'always_convert_images': options.get('always_convert_images', False) 'always_convert_images': options.get('always_convert_images', False)
}, },
normalize=normalize, 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) logger.info("File created: " + filename)
else: else: