mirror of
https://github.com/kemayo/leech
synced 2025-12-06 16:33:16 +01:00
Merge abd9acb2a7 into cfd073fb5c
This commit is contained in:
commit
cbb8916718
2 changed files with 70 additions and 0 deletions
23
leech.py
23
leech.py
|
|
@ -13,6 +13,7 @@ from functools import reduce
|
||||||
|
|
||||||
import sites
|
import sites
|
||||||
import ebook
|
import ebook
|
||||||
|
import reader
|
||||||
|
|
||||||
__version__ = 2
|
__version__ = 2
|
||||||
USER_AGENT = 'Leech/%s +http://davidlynch.org' % __version__
|
USER_AGENT = 'Leech/%s +http://davidlynch.org' % __version__
|
||||||
|
|
@ -193,5 +194,27 @@ def download(urls, site_options, cache, verbose, normalize, output_dir, **other_
|
||||||
logger.warning("No ebook created")
|
logger.warning("No ebook created")
|
||||||
|
|
||||||
|
|
||||||
|
@cli.command()
|
||||||
|
@click.argument('url')
|
||||||
|
@click.option(
|
||||||
|
'--site-options',
|
||||||
|
default='{}',
|
||||||
|
help='JSON object encoding any site specific option.'
|
||||||
|
)
|
||||||
|
@click.option('--cache/--no-cache', default=True)
|
||||||
|
@click.option('--verbose', '-v', is_flag=True, help="Verbose debugging output")
|
||||||
|
@site_specific_options # Includes other click.options specific to sites
|
||||||
|
def read(url, site_options, cache, verbose, **other_flags):
|
||||||
|
"""Launches an in terminal reader to preview or read a story."""
|
||||||
|
configure_logging(verbose)
|
||||||
|
session = create_session(cache)
|
||||||
|
|
||||||
|
site, url = sites.get(url)
|
||||||
|
options, login = create_options(site, site_options, other_flags)
|
||||||
|
story = open_story(site, url, session, login, options)
|
||||||
|
|
||||||
|
reader.launch_reader(story)
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
cli()
|
cli()
|
||||||
|
|
|
||||||
47
reader/__init__.py
Normal file
47
reader/__init__.py
Normal file
|
|
@ -0,0 +1,47 @@
|
||||||
|
import pypandoc
|
||||||
|
import pydoc
|
||||||
|
import pick
|
||||||
|
import sys
|
||||||
|
|
||||||
|
|
||||||
|
def description(description):
|
||||||
|
"""Decorator to make it possible to quickly attach a description to a function or class."""
|
||||||
|
def wrapper(action):
|
||||||
|
action.description = description
|
||||||
|
return action
|
||||||
|
return wrapper
|
||||||
|
|
||||||
|
|
||||||
|
def launch_reader(story):
|
||||||
|
chapters = story.contents
|
||||||
|
chapter_index = -1
|
||||||
|
|
||||||
|
@description('Next Chapter')
|
||||||
|
def next_chapter_action():
|
||||||
|
nonlocal chapter_index
|
||||||
|
chapter_index += 1
|
||||||
|
|
||||||
|
@description('Start from the Beginning')
|
||||||
|
def start_from_beginning_action():
|
||||||
|
nonlocal chapter_index
|
||||||
|
chapter_index = 0
|
||||||
|
|
||||||
|
@description('Select Chapter')
|
||||||
|
def select_chapter_action():
|
||||||
|
nonlocal chapter_index
|
||||||
|
_, chapter_index = pick.pick(
|
||||||
|
[chapter.title for chapter in chapters],
|
||||||
|
"Which chapter?",
|
||||||
|
default_index=max(0, chapter_index)
|
||||||
|
)
|
||||||
|
|
||||||
|
@description('Quit')
|
||||||
|
def quit_action():
|
||||||
|
sys.exit(0)
|
||||||
|
|
||||||
|
actions = [next_chapter_action, start_from_beginning_action, select_chapter_action, quit_action]
|
||||||
|
|
||||||
|
while True:
|
||||||
|
_, action_index = pick.pick([action.description for action in actions], "What to do?")
|
||||||
|
actions[action_index]()
|
||||||
|
pydoc.pager(pypandoc.convert_text(chapters[chapter_index].contents, 'rst', format='html'))
|
||||||
Loading…
Reference in a new issue