1
0
Fork 0
mirror of https://github.com/kemayo/leech synced 2025-12-06 16:33:16 +01:00
This commit is contained in:
Zomega 2025-03-14 15:08:19 +00:00 committed by GitHub
commit cbb8916718
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 70 additions and 0 deletions

View file

@ -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
View 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'))