1
0
Fork 0
mirror of https://github.com/kemayo/leech synced 2025-12-28 19:23:13 +01:00

Add cover config to leech.json

This commit is contained in:
David Lynch 2017-10-12 11:20:45 -05:00
parent 619a013bcb
commit 8ac1aa8bb0
3 changed files with 31 additions and 7 deletions

View file

@ -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],
}
}
```

View file

@ -3,6 +3,7 @@ from .cover import make_cover
import datetime
import requests
import attr
html_template = '''<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:epub="http://www.idpf.org/2007/ops">
@ -59,6 +60,17 @@ frontmatter_template = '''<?xml version="1.0" encoding="UTF-8" standalone="no"?>
'''
@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)))

View file

@ -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__':