diff --git a/README.markdown b/README.markdown index 8ba598e..84e69ae 100644 --- a/README.markdown +++ b/README.markdown @@ -40,7 +40,7 @@ Supports Configuration --- -A very small amount of configuration is possible by creating a file called `leech.json` in the project directory. Currently you can define login information for sites that support it. +A very small amount of configuration is possible by creating a file called `leech.json` in the project directory. Currently you can define login information for sites that support it, and some options for book covers. Example: @@ -48,6 +48,12 @@ Example: { "logins": { "QuestionableQuesting": ["username", "password"] + }, + "cover": { + "fontname": "Comic Sans MS", + "fontsize": 30, + "bgcolor": [20, 120, 20], + "textcolor": [180, 20, 180], } } ``` diff --git a/ebook/__init__.py b/ebook/__init__.py index 87a769d..3934dbc 100644 --- a/ebook/__init__.py +++ b/ebook/__init__.py @@ -3,6 +3,7 @@ from .cover import make_cover import datetime import requests +import attr html_template = ''' @@ -59,6 +60,17 @@ frontmatter_template = ''' ''' +@attr.s +class CoverOptions: + fontname = attr.ib(default=None, convert=attr.converters.optional(str)) + fontsize = attr.ib(default=None, convert=attr.converters.optional(int)) + width = attr.ib(default=None, convert=attr.converters.optional(int)) + height = attr.ib(default=None, convert=attr.converters.optional(int)) + wrapat = attr.ib(default=None, convert=attr.converters.optional(int)) + bgcolor = attr.ib(default=None, convert=attr.converters.optional(tuple)) + textcolor = attr.ib(default=None, convert=attr.converters.optional(tuple)) + + def chapter_html(story, titleprefix=None): chapters = [] for i, chapter in enumerate(story): @@ -77,7 +89,7 @@ def chapter_html(story, titleprefix=None): return chapters -def generate_epub(story, output_filename=None): +def generate_epub(story, output_filename=None, cover_options={}): dates = list(story.dates()) metadata = { 'title': story.title, @@ -87,10 +99,13 @@ def generate_epub(story, output_filename=None): 'updated': max(dates), } + cover_options = CoverOptions(**cover_options) + cover_options = attr.asdict(cover_options, filter=lambda k, v: v is not None, retain_collection_types=True) + # The cover is static, and the only change comes from the image which we generate html = [('Cover', 'cover.html', cover_template)] - cover_image = ('images/cover.png', make_cover(story.title, story.author).read(), 'image/png') + cover_image = ('images/cover.png', make_cover(story.title, story.author, **cover_options).read(), 'image/png') html.append(('Front Matter', 'frontmatter.html', frontmatter_template.format(now=datetime.datetime.now(), **metadata))) diff --git a/leech.py b/leech.py index 763c292..b73c26e 100755 --- a/leech.py +++ b/leech.py @@ -26,17 +26,20 @@ def leech(url, session, filename=None, args=None): handler = site(session, args=args) - with open('leech.json') as store_file: - store = json.load(store_file) - login = store.get('logins', {}).get(site.__name__, False) + with open('leech.json') as config_file: + config = json.load(config_file) + + login = config.get('logins', {}).get(site.__name__, False) if login: handler.login(login) + cover_options = config.get('cover', {}) + story = handler.extract(url) if not story: raise Exception("Couldn't extract story") - return ebook.generate_epub(story, filename) + return ebook.generate_epub(story, filename, cover_options=cover_options) if __name__ == '__main__':