diff --git a/leech.py b/leech.py index 6c2c820..ffc304d 100755 --- a/leech.py +++ b/leech.py @@ -13,6 +13,7 @@ from functools import reduce import sites import ebook +import reader __version__ = 2 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") +@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__': cli() diff --git a/reader/__init__.py b/reader/__init__.py new file mode 100644 index 0000000..6eb8831 --- /dev/null +++ b/reader/__init__.py @@ -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'))