mirror of
https://github.com/kemayo/leech
synced 2025-12-08 01:14:10 +01:00
Reworks how site-specific options work.
This commit is contained in:
parent
db48233cf4
commit
c702337040
3 changed files with 39 additions and 13 deletions
35
leech.py
35
leech.py
|
|
@ -41,26 +41,39 @@ def uses_session(command):
|
||||||
def uses_story(command):
|
def uses_story(command):
|
||||||
"""Decorator for click commands that need a story."""
|
"""Decorator for click commands that need a story."""
|
||||||
@click.argument('url')
|
@click.argument('url')
|
||||||
@click.option('--include-index', default=False, help='[Xenforo only] Should the chapter marked as an index be included?')
|
@click.option(
|
||||||
@click.option('--offset', type=int, default=None, help='[Xenforo only] The chapter to start from.')
|
'--site-options',
|
||||||
@click.option('--limit', type=int, default=None, help='[Xenforo only] The chapter to end with.')
|
default='{}',
|
||||||
@click.option('--skip-spoilers/--include-spoilers', default=True, help='[Xenforo only] If the story should include content enclosed in spoiler tags.')
|
help='JSON object encoding any site specific option.'
|
||||||
|
)
|
||||||
@uses_session
|
@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)
|
site, url = sites.get(url)
|
||||||
if not site:
|
if not site:
|
||||||
raise Exception("No site handler found")
|
raise Exception("No site handler found")
|
||||||
|
|
||||||
handler = site(session, options={
|
default_site_options = site.get_default_options()
|
||||||
'offset': offset,
|
|
||||||
'limit': limit,
|
|
||||||
'skip_spoilers': skip_spoilers,
|
|
||||||
'include_index': include_index,
|
|
||||||
})
|
|
||||||
|
|
||||||
with open('leech.json') as store_file:
|
with open('leech.json') as store_file:
|
||||||
store = json.load(store_file)
|
store = json.load(store_file)
|
||||||
login = store.get('logins', {}).get(site.__name__, False)
|
login = store.get('logins', {}).get(site.__name__, False)
|
||||||
|
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:
|
if login:
|
||||||
handler.login(login)
|
handler.login(login)
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -64,6 +64,10 @@ class Site:
|
||||||
footnotes = attr.ib(default=attr.Factory(list), init=False)
|
footnotes = attr.ib(default=attr.Factory(list), init=False)
|
||||||
options = attr.ib(default=attr.Factory(dict))
|
options = attr.ib(default=attr.Factory(dict))
|
||||||
|
|
||||||
|
@staticmethod
|
||||||
|
def get_default_options():
|
||||||
|
return {}
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def matches(url):
|
def matches(url):
|
||||||
raise NotImplementedError()
|
raise NotImplementedError()
|
||||||
|
|
|
||||||
|
|
@ -10,6 +10,15 @@ class XenForo(Site):
|
||||||
|
|
||||||
domain = False
|
domain = False
|
||||||
|
|
||||||
|
@staticmethod
|
||||||
|
def get_default_options():
|
||||||
|
return {
|
||||||
|
'offset': None,
|
||||||
|
'limit': None,
|
||||||
|
'skip_spoilers': True,
|
||||||
|
'include_index': False,
|
||||||
|
}
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def matches(cls, url):
|
def matches(cls, url):
|
||||||
match = re.match(r'^(https?://%s/threads/[^/]*\d+)/?.*' % cls.domain, url)
|
match = re.match(r'^(https?://%s/threads/[^/]*\d+)/?.*' % cls.domain, url)
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue