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

Reworks how site-specific options work.

This commit is contained in:
Will Oursler 2017-10-13 19:37:13 -04:00
parent db48233cf4
commit c702337040
3 changed files with 39 additions and 13 deletions

View file

@ -41,28 +41,41 @@ def uses_session(command):
def uses_story(command):
"""Decorator for click commands that need a story."""
@click.argument('url')
@click.option('--include-index', default=False, help='[Xenforo only] Should the chapter marked as an index be included?')
@click.option('--offset', type=int, default=None, help='[Xenforo only] The chapter to start from.')
@click.option('--limit', type=int, default=None, help='[Xenforo only] The chapter to end with.')
@click.option('--skip-spoilers/--include-spoilers', default=True, help='[Xenforo only] If the story should include content enclosed in spoiler tags.')
@click.option(
'--site-options',
default='{}',
help='JSON object encoding any site specific option.'
)
@uses_session
def wrapper(url, session, include_index, offset, limit, skip_spoilers, **kwargs):
def wrapper(url, session, site_options, **kwargs):
site, url = sites.get(url)
if not site:
raise Exception("No site handler found")
handler = site(session, options={
'offset': offset,
'limit': limit,
'skip_spoilers': skip_spoilers,
'include_index': include_index,
})
default_site_options = site.get_default_options()
with open('leech.json') as store_file:
store = json.load(store_file)
login = store.get('logins', {}).get(site.__name__, False)
if login:
handler.login(login)
configured_site_options = store.get('site_options', {}).get(site.__name__, {})
overridden_site_options = json.loads(site_options)
# The final options dictionary is computed by layering the default, configured,
# and overridden options together in that order.
options = dict(
list(default_site_options.items()) +
list(configured_site_options.items()) +
list(overridden_site_options.items())
)
handler = site(
session,
options=options
)
if login:
handler.login(login)
story = handler.extract(url)
if not story:

View file

@ -64,6 +64,10 @@ class Site:
footnotes = attr.ib(default=attr.Factory(list), init=False)
options = attr.ib(default=attr.Factory(dict))
@staticmethod
def get_default_options():
return {}
@staticmethod
def matches(url):
raise NotImplementedError()

View file

@ -10,6 +10,15 @@ class XenForo(Site):
domain = False
@staticmethod
def get_default_options():
return {
'offset': None,
'limit': None,
'skip_spoilers': True,
'include_index': False,
}
@classmethod
def matches(cls, url):
match = re.match(r'^(https?://%s/threads/[^/]*\d+)/?.*' % cls.domain, url)