コード例 #1
0
ファイル: html2epub.py プロジェクト: Te0reTiK/html2epub
def main():
    args = parse_args(sys.argv[1:]) or exit()

    if args.verbose:
        root_logger = getLogger()
        root_logger.setLevel(logging.DEBUG)

    if args.quiet:
        root_logger = getLogger()
        root_logger.setLevel(logging.CRITICAL)

    grabber_manager = GrabberManager()
    grabber_manager.load_from_dir(config['grabbers_dir'])
    if args.grabbers:
        for grabber in grabber_manager.all_grabbers():
            logging.info("%s '%s'", grabber.name, grabber.info)
        exit()

    if args.grabber:
        grabber = grabber_manager.get(args.grabber)
        if grabber is None:
            fatal("Grabber '%s' does not exists", args.grabber)
        logging.debug("Grab by '%s' grabber", grabber.name)
    else:
        grabber = None

    title = args.title
    epub_name = None
    logging.debug('Getting pages...')
    if len(args.sources) == 1:
        url = args.sources[0]
        grabber = grabber or grabber_manager.get_suitable(url)
        info, stream, images = grabber.grab(url)
        title = title or info['title'] or args.out
        if not title:
            url = url.rstrip('/')
            title = os.path.split(url)[1]

        if args.out:
            try:
                epub_name = args.out.format(**info)
            except KeyError as exc:
                fatal("Error in output format key: %s", exc)
        else:
            fmt = '{date} {title}'.format(**info)
            if fmt.strip():
                epub_name = fmt
        lang = args.lang or info.get('lang')
        builder = EpubBuilder(title, lang=lang,
            creator=info.get('creator'), date=info.get('create_date'))
        add2builder(builder, 'ch1.html', stream, info, images)
    else:
        # TODO pass arg 'lang' to EpubBuilder(..., lang=language)
        builder = EpubBuilder(title)
        for i, url in enumerate(args.sources, 1):
            grabber = grabber or grabber_manager.get_suitable(url)
            info, stream, images = grabber.grab(url)
            add2builder(builder, 'ch{}.html'.format(i), stream, info, images)

    dest = epub_name or title[:config['autoname_len']]
    if os.path.splitext(dest)[1] != '.epub':
        dest = '{}.epub'.format(dest)
    if args.no_spaces:
        dest = dest.replace(' ', '_')

    logging.debug('Building book...')
    builder.build(dest=dest)
    logging.info('Complete. Saved to "%s"', dest)
コード例 #2
0
ファイル: testmain.py プロジェクト: wanziforever/tools
#!/usr/bin/env python

from grabber_manager import GrabberManager
from grabber_worker import GrabberWorker
from calc_manager import CalcManager
from calc_worker import CalcWorker

from core.msgh import MsghMgr

msgh = MsghMgr()

if __name__ == "__main__":
    gmgr = GrabberManager(msgh)
    gmgr.set_worker('grabberworker', GrabberWorker,
                    {'start':0, 'end': 1})
    gmgr.start()

    cmgr = CalcManager(msgh)
    cmgr.set_worker('calcworker', CalcWorker,
                    {'start':0, 'end': 1})
    cmgr.start()