mirror of
https://github.com/kemayo/leech
synced 2025-12-06 08:22:56 +01:00
Config option: output_dir
Can be provided on the command line as `--output-dir`, or in leech.json as `output_dir` (also in the `site_options` in leech.json). Refs #67
This commit is contained in:
parent
312fbc03a1
commit
bb9491cb96
4 changed files with 24 additions and 5 deletions
|
|
@ -67,6 +67,12 @@ Example:
|
|||
"bgcolor": [20, 120, 20],
|
||||
"textcolor": [180, 20, 180],
|
||||
"cover_url": "https://website.com/image.png"
|
||||
},
|
||||
"output_dir": "/tmp/ebooks",
|
||||
"site_options": {
|
||||
"RoyalRoad": {
|
||||
"output_dir": "/tmp/litrpg_isekai_trash"
|
||||
}
|
||||
}
|
||||
}
|
||||
```
|
||||
|
|
|
|||
|
|
@ -99,7 +99,7 @@ def chapter_html(story, titleprefix=None, normalize=False):
|
|||
return chapters
|
||||
|
||||
|
||||
def generate_epub(story, cover_options={}, output_filename=None, normalize=False):
|
||||
def generate_epub(story, cover_options={}, output_filename=None, output_dir=None, normalize=False):
|
||||
dates = list(story.dates())
|
||||
metadata = {
|
||||
'title': story.title,
|
||||
|
|
@ -145,6 +145,6 @@ def generate_epub(story, cover_options={}, output_filename=None, normalize=False
|
|||
|
||||
output_filename = output_filename or story.title + '.epub'
|
||||
|
||||
output_filename = make_epub(output_filename, files, metadata)
|
||||
output_filename = make_epub(output_filename, files, metadata, output_dir=output_dir)
|
||||
|
||||
return output_filename
|
||||
|
|
|
|||
|
|
@ -35,12 +35,14 @@ def sanitize_filename(s):
|
|||
return filename
|
||||
|
||||
|
||||
def make_epub(filename, files, meta, compress=True):
|
||||
def make_epub(filename, files, meta, compress=True, output_dir=False):
|
||||
unique_id = meta.get('unique_id', False)
|
||||
if not unique_id:
|
||||
unique_id = 'leech_book_' + str(uuid.uuid4())
|
||||
|
||||
filename = sanitize_filename(filename)
|
||||
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)
|
||||
|
||||
# The first file must be named "mimetype", and shouldn't be compressed
|
||||
|
|
|
|||
15
leech.py
15
leech.py
|
|
@ -4,6 +4,7 @@ import click
|
|||
import http.cookiejar
|
||||
import json
|
||||
import logging
|
||||
import os
|
||||
import requests
|
||||
import requests_cache
|
||||
import sqlite3
|
||||
|
|
@ -59,11 +60,15 @@ def load_on_disk_options(site):
|
|||
login = store.get('logins', {}).get(site.site_key(), False)
|
||||
configured_site_options = store.get('site_options', {}).get(site.site_key(), {})
|
||||
cover_options = store.get('cover', {})
|
||||
output_dir = store.get('output_dir', False)
|
||||
except FileNotFoundError:
|
||||
logger.info("Unable to locate leech.json. Continuing assuming it does not exist.")
|
||||
login = False
|
||||
configured_site_options = {}
|
||||
cover_options = {}
|
||||
output_dir = False
|
||||
if output_dir and 'output_dir' not in configured_site_options:
|
||||
configured_site_options['output_dir'] = output_dir
|
||||
return configured_site_options, login, cover_options
|
||||
|
||||
|
||||
|
|
@ -143,20 +148,26 @@ def flush(verbose):
|
|||
default='{}',
|
||||
help='JSON object encoding any site specific option.'
|
||||
)
|
||||
@click.option(
|
||||
'--output-dir',
|
||||
default=None,
|
||||
help='Directory to save generated ebooks'
|
||||
)
|
||||
@click.option('--cache/--no-cache', default=True)
|
||||
@click.option('--normalize/--no-normalize', default=True, help="Whether to normalize strange unicode text")
|
||||
@click.option('--verbose', '-v', is_flag=True, help="Verbose debugging output")
|
||||
@site_specific_options # Includes other click.options specific to sites
|
||||
def download(url, site_options, cache, verbose, normalize, **other_flags):
|
||||
def download(url, site_options, cache, verbose, normalize, output_dir, **other_flags):
|
||||
"""Downloads a story and saves it on disk as a ebpub ebook."""
|
||||
configure_logging(verbose)
|
||||
session = create_session(cache)
|
||||
|
||||
site, url = sites.get(url)
|
||||
options, login = create_options(site, site_options, other_flags)
|
||||
output_dir = output_dir or options.get('output_dir', os.getcwd())
|
||||
story = open_story(site, url, session, login, options)
|
||||
if story:
|
||||
filename = ebook.generate_epub(story, options, normalize=normalize)
|
||||
filename = ebook.generate_epub(story, options, normalize=normalize, output_dir=output_dir)
|
||||
logger.info("File created: " + filename)
|
||||
else:
|
||||
logger.warning("No ebook created")
|
||||
|
|
|
|||
Loading…
Reference in a new issue