python-ebooker/ebooker/__init__.py

56 lines
1.5 KiB
Python

#!/usr/bin/env python3
# -*- encoding: utf-8 -*-
import argparse
from pathlib import Path
from .downloaders import AbstractBookDownloader
from .writer import epubwriter
def first_or_none(closure, collection):
for item in collection:
if closure(item):
return item
return None
def parse_cmdln():
parser = argparse.ArgumentParser(
description='Downloads stories into epub ebook.'
)
parser.add_argument(
'link_list',
help='A plain text file with one link per line.'
)
parser.add_argument(
'out_dir',
help='The folder that will contain the ebooks.'
)
args = parser.parse_args()
ll = Path(args.link_list).read_text().splitlines()
od = Path(args.out_dir)
od.mkdir(parents=True, exist_ok=True)
return ll, od
def main():
links, dirout = parse_cmdln()
for link in links:
link = link.strip()
downloader_cls = first_or_none(
lambda d: d.downloads(link),
AbstractBookDownloader._all_subclasses()
)
if downloader_cls is None:
print('No downloader: ', end='')
print(link, end='')
print(' !!!')
else:
print(downloader_cls.__name__, end='')
print(': ', end='')
print(link)
downloader = downloader_cls(dirout)
cached = downloader.fetch(link)
xhtmls, css = downloader.neutralize(cached)
epubwriter(dirout, cached, xhtmls, css)