Exemple #1
0
def main(argv=None,
         parser=None,
         passed_defaultsini=None,
         passed_personalini=None):
    if argv is None:
        argv = sys.argv[1:]
    # read in args, anything starting with -- will be treated as --<varible>=<value>
    if not parser:
        parser = OptionParser('usage: %prog [options] [STORYURL]...')
    parser.add_option(
        '-f',
        '--format',
        dest='format',
        default='epub',
        help='Write story as FORMAT, epub(default), mobi, txt or html.',
        metavar='FORMAT')
    if passed_defaultsini:
        config_help = 'calibre plugin defaults.ini, calibre plugin personal.ini'
    else:
        config_help = '~/.fanficfare/defaults.ini, $XDG_CONFIG_HOME/fanficfare/defaults.ini, ./defaults.ini'
    parser.add_option(
        '-c',
        '--config',
        action='append',
        dest='configfile',
        default=None,
        help=
        ('Read config from specified file(s) in addition to (ordered lowest to highest priority): '
         + config_help +
         ', ~/.fanficfare/personal.ini, $XDG_CONFIG_HOME/fanficfare/personal.ini, and ./personal.ini.  -c/--config files take highest priority'
         ),
        metavar='CONFIG')
    range_help = '  --begin and --end will be overridden by a chapter range on the STORYURL like STORYURL[1-2], STORYURL[-3], STORYURL[3-] or STORYURL[3]'
    parser.add_option('-b',
                      '--begin',
                      dest='begin',
                      default=None,
                      help='Begin story with Chapter START.' + range_help,
                      metavar='START')
    parser.add_option('-e',
                      '--end',
                      dest='end',
                      default=None,
                      help='End story with Chapter END.' + range_help,
                      metavar='END')
    parser.add_option(
        '-o',
        '--option',
        action='append',
        dest='options',
        help='Set config option NAME=VALUE  Overrides config file setting.',
        metavar='NAME=VALUE')
    parser.add_option(
        '-m',
        '--meta-only',
        action='store_true',
        dest='metaonly',
        help=
        'Retrieve and write metadata to stdout without downloading or saving chapters; saves story file with titlepage only. (See also --json-meta)',
    )
    parser.add_option(
        '-z',
        '--no-meta-chapters',
        action='store_true',
        dest='nometachapters',
        help=
        'Exclude list of chapters("zchapters") from metadata stdout output.  No effect without --meta-only or --json-meta flags',
    )
    parser.add_option(
        '-j',
        '--json-meta',
        action='store_true',
        dest='jsonmeta',
        help=
        'Output metadata as JSON with download, or with --meta-only flag.  (Only JSON will be output with --meta-only flag.)',
    )
    parser.add_option(
        '--no-output',
        action='store_true',
        dest='nooutput',
        help=
        'Do not download chapters and do not write output file.  Intended for testing and with --meta-only.',
    )
    parser.add_option(
        '-u',
        '--update-epub',
        action='store_true',
        dest='update',
        help=
        'Update an existing epub(if present) with new chapters.  Give either epub filename or story URL.',
    )
    parser.add_option(
        '-U',
        '--update-epub-always',
        action='store_true',
        dest='updatealways',
        help=
        "Update an existing epub(if present) even if there aren't new chapters.  Give either epub filename or story URL.",
    )
    parser.add_option(
        '--update-cover',
        action='store_true',
        dest='updatecover',
        help=
        'Update cover in an existing epub, otherwise existing cover (if any) is used on update.  Only valid with --update-epub.',
    )
    parser.add_option(
        '--unnew',
        action='store_true',
        dest='unnew',
        help='Remove (new) chapter marks left by mark_new_chapters setting.',
    )
    parser.add_option(
        '--force',
        action='store_true',
        dest='force',
        help=
        'Force overwrite of an existing epub, download and overwrite all chapters.',
    )
    parser.add_option(
        '-i',
        '--infile',
        help=
        'Give a filename to read for URLs (and/or existing EPUB files with --update-epub).',
        dest='infile',
        default=None,
        metavar='INFILE')

    parser.add_option(
        '-l',
        '--list',
        dest='list',
        default=None,
        metavar='URL',
        help='Get list of valid story URLs from page given.',
    )
    parser.add_option(
        '-n',
        '--normalize-list',
        dest='normalize',
        default=None,
        metavar='URL',
        help=
        'Get list of valid story URLs from page given, but normalized to standard forms.',
    )
    parser.add_option(
        '--download-list',
        dest='downloadlist',
        default=None,
        metavar='URL',
        help=
        'Download story URLs retrieved from page given.  Update existing EPUBs if used with --update-epub.',
    )

    parser.add_option(
        '--imap',
        action='store_true',
        dest='imaplist',
        help=
        'Get list of valid story URLs from unread email from IMAP account configured in ini.',
    )

    parser.add_option(
        '--download-imap',
        action='store_true',
        dest='downloadimap',
        help=
        'Download valid story URLs from unread email from IMAP account configured in ini.  Update existing EPUBs if used with --update-epub.',
    )

    parser.add_option(
        '-s',
        '--sites-list',
        action='store_true',
        dest='siteslist',
        default=False,
        help='Get list of valid story URLs examples.',
    )
    parser.add_option(
        '--non-interactive',
        action='store_false',
        dest='interactive',
        default=sys.stdin.isatty() and sys.stdout.isatty(),
        help='Prevent interactive prompts (for scripting).',
    )
    parser.add_option(
        '-d',
        '--debug',
        action='store_true',
        dest='debug',
        help='Show debug and notice output.',
    )
    parser.add_option(
        '-p',
        '--progressbar',
        action='store_true',
        dest='progressbar',
        help=
        'Display a simple progress bar while downloading--one dot(.) per network fetch.',
    )
    parser.add_option(
        '-v',
        '--version',
        action='store_true',
        dest='version',
        help='Display version and quit.',
    )

    ## undocumented feature for development use.  Save page cache and
    ## cookies between runs.  Saves in PWD as files global_cache and
    ## global_cookies
    parser.add_option(
        '--save-cache',
        '--save_cache',
        action='store_true',
        dest='save_cache',
        help=SUPPRESS_HELP,
    )

    options, args = parser.parse_args(argv)

    if not options.debug:
        logger.setLevel(logging.WARNING)
    else:
        import platform
        logger.debug("    OS Version:%s" % platform.platform())
        logger.debug("Python Version:%s" % sys.version)
        logger.debug("   FFF Version:%s" % version)

    if options.version:
        print("Version: %s" % version)
        return

    list_only = any((
        options.imaplist,
        options.siteslist,
        options.list,
        options.normalize,
    ))

    if list_only and (args or any(
        (options.downloadimap, options.downloadlist))):
        parser.error(
            'Incorrect arguments: Cannot download and list URLs at the same time.'
        )

    if options.siteslist:
        for site, examples in adapters.getSiteExamples():
            print('\n#### %s\nExample URLs:' % site)
            for u in examples:
                print('  * %s' % u)
        return

    # options.updatealways should also invoke most options.update logic.
    if options.updatealways:
        options.update = True

    if options.update and options.format != 'epub':
        parser.error(
            '-u/--update-epub/-U/--update-epub-always only work with epub')

    if options.unnew and options.format != 'epub':
        parser.error('--unnew only works with epub')

    urls = args

    if not list_only and not (args or any(
        (options.infile, options.downloadimap, options.downloadlist))):
        parser.print_help()
        return

    if options.list:
        configuration = get_configuration(options.list, passed_defaultsini,
                                          passed_personalini, options)
        retlist = get_urls_from_page(options.list, configuration)
        print('\n'.join(retlist))

    if options.normalize:
        configuration = get_configuration(options.normalize,
                                          passed_defaultsini,
                                          passed_personalini, options)
        retlist = get_urls_from_page(options.normalize,
                                     configuration,
                                     normalize=True)
        print('\n'.join(retlist))

    if options.downloadlist:
        configuration = get_configuration(options.downloadlist,
                                          passed_defaultsini,
                                          passed_personalini, options)
        retlist = get_urls_from_page(options.downloadlist, configuration)
        urls.extend(retlist)

    if options.imaplist or options.downloadimap:
        # list doesn't have a supported site.
        configuration = get_configuration('test1.com', passed_defaultsini,
                                          passed_personalini, options)
        markread = configuration.getConfig('imap_mark_read') == 'true' or \
            (configuration.getConfig('imap_mark_read') == 'downloadonly' and options.downloadimap)
        retlist = get_urls_from_imap(configuration.getConfig('imap_server'),
                                     configuration.getConfig('imap_username'),
                                     configuration.getConfig('imap_password'),
                                     configuration.getConfig('imap_folder'),
                                     markread)

        if options.downloadimap:
            urls.extend(retlist)
        else:
            print('\n'.join(retlist))

    # for passing in a file list
    if options.infile:
        with open(options.infile, "r") as infile:
            #print("file exists and is readable")
            for url in infile:
                if '#' in url:
                    url = url[:url.find('#')].strip()
                url = url.strip()
                if len(url) > 0:
                    #print("url: (%s)"%url)
                    urls.append(url)

    if options.save_cache:
        try:
            with open(global_cache, 'rb') as jin:
                options.pagecache = pickle_load(jin)
            options.cookiejar = cl.LWPCookieJar()
            options.cookiejar.load(global_cookies)
        except Exception as e:
            print("Didn't load --save-cache %s" % e)

    if not list_only:
        if len(urls) < 1:
            print("No valid story URLs found")
        else:
            for url in urls:
                try:
                    do_download(url, options, passed_defaultsini,
                                passed_personalini)
                    # print("pagecache:%s"%options.pagecache.keys())
                except Exception as e:
                    if len(urls) == 1:
                        raise
                    print(
                        "URL(%s) Failed: Exception (%s). Run URL individually for more detail."
                        % (url, e))
Exemple #2
0
def main(argv=None,
         parser=None,
         passed_defaultsini=None,
         passed_personalini=None):
    if argv is None:
        argv = sys.argv[1:]
    # read in args, anything starting with -- will be treated as --<varible>=<value>
    if not parser:
        parser = OptionParser('usage: %prog [options] [STORYURL]...')
    parser.add_option('-f', '--format', dest='format', default='epub',
                      help='write story as FORMAT, epub(default), mobi, txt or html', metavar='FORMAT')
    if passed_defaultsini:
        config_help = 'read config from specified file(s) in addition to calibre plugin personal.ini, ~/.fanficfare/personal.ini, and ./personal.ini'
    else:
        config_help = 'read config from specified file(s) in addition to ~/.fanficfare/defaults.ini, ~/.fanficfare/personal.ini, ./defaults.ini, and ./personal.ini'
    parser.add_option('-c', '--config',
                      action='append', dest='configfile', default=None,
                      help=config_help, metavar='CONFIG')
    range_help = '  --begin and --end will be overridden by a chapter range on the STORYURL like STORYURL[1-2], STORYURL[-3], STORYURL[3-] or STORYURL[3]'
    parser.add_option('-b', '--begin', dest='begin', default=None,
                      help='Begin with Chapter START.'+range_help, metavar='START')
    parser.add_option('-e', '--end', dest='end', default=None,
                      help='End with Chapter END.'+range_help, metavar='END')
    parser.add_option('-o', '--option',
                      action='append', dest='options',
                      help='set an option NAME=VALUE', metavar='NAME=VALUE')
    parser.add_option('-m', '--meta-only',
                      action='store_true', dest='metaonly',
                      help='Retrieve metadata and stop.  Or, if --update-epub, update metadata title page only.', )
    parser.add_option('-z', '--no-meta-chapters',
                      action='store_true', dest='nometachapters',
                      help='Exclude list of chapters("zchapters") from metadata dump.  No effect without --meta-only flag', )
    parser.add_option('--json-meta',
                      action='store_true', dest='jsonmeta',
                      help='When used with --meta-only, output metadata as JSON.  No effect without --meta-only flag', )
    parser.add_option('-u', '--update-epub',
                      action='store_true', dest='update',
                      help='Update an existing epub(if present) with new chapters.  Give either epub filename or story URL.', )
    parser.add_option('--update-cover',
                      action='store_true', dest='updatecover',
                      help='Update cover in an existing epub, otherwise existing cover (if any) is used on update.  Only valid with --update-epub.', )
    parser.add_option('--unnew',
                      action='store_true', dest='unnew',
                      help='Remove (new) chapter marks left by mark_new_chapters setting.', )
    parser.add_option('--force',
                      action='store_true', dest='force',
                      help='Force overwrite of an existing epub, download and overwrite all chapters.', )
    parser.add_option('-i', '--infile',
                      help='Give a filename to read for URLs (and/or existing EPUB files with --update-epub).',
                      dest='infile', default=None,
                      metavar='INFILE')

    parser.add_option('-l', '--list',
                      dest='list', default=None, metavar='URL',
                      help='Get list of valid story URLs from page given.', )
    parser.add_option('-n', '--normalize-list',
                      dest='normalize', default=None, metavar='URL',
                      help='Get list of valid story URLs from page given, but normalized to standard forms.', )
    parser.add_option('--download-list',
                      dest='downloadlist', default=None, metavar='URL',
                      help='Download story URLs retrieved from page given.  Update existing EPUBs if used with --update-epub.', )

    parser.add_option('--imap',
                      action='store_true', dest='imaplist',
                      help='Get list of valid story URLs from unread email from IMAP account configured in ini.', )

    parser.add_option('--download-imap',
                      action='store_true', dest='downloadimap',
                      help='Download valid story URLs from unread email from IMAP account configured in ini.  Update existing EPUBs if used with --update-epub.', )

    parser.add_option('-s', '--sites-list',
                      action='store_true', dest='siteslist', default=False,
                      help='Get list of valid story URLs examples.', )
    parser.add_option('--non-interactive',
                      action='store_false', dest='interactive', default=sys.stdin.isatty() and sys.stdout.isatty(),
                      help='Prevent interactive prompts (for scripting).', )
    parser.add_option('-d', '--debug',
                      action='store_true', dest='debug',
                      help='Show debug and notice output.', )
    parser.add_option('-p', '--progressbar',
                      action='store_true', dest='progressbar',
                      help='Display a simple progress bar while downloading--one dot(.) per network fetch.', )
    parser.add_option('-v', '--version',
                      action='store_true', dest='version',
                      help='Display version and quit.', )

    ## undocumented feature for development use.  Save page cache and
    ## cookies between runs.  Saves in PWD as files global_cache and
    ## global_cookies
    parser.add_option('--save-cache', '--save_cache',
                      action='store_true', dest='save_cache',
                      help=SUPPRESS_HELP, )

    options, args = parser.parse_args(argv)

    if not options.debug:
        logger.setLevel(logging.WARNING)
    else:
        import platform
        logger.debug("    OS Version:%s"%platform.platform())
        logger.debug("Python Version:%s"%sys.version)
        logger.debug("   FFF Version:%s"%version)

    if options.version:
        print("Version: %s" % version)
        return

    list_only = any((options.imaplist,
                     options.siteslist,
                     options.list,
                     options.normalize,
                     ))

    if list_only and (args or any((options.downloadimap,
                                   options.downloadlist))):
        parser.error('Incorrect arguments: Cannot download and list URLs at the same time.')

    if options.siteslist:
        for site, examples in adapters.getSiteExamples():
            print('\n#### %s\nExample URLs:' % site)
            for u in examples:
                print('  * %s' % u)
        return

    if options.update and options.format != 'epub':
        parser.error('-u/--update-epub only works with epub')

    if options.unnew and options.format != 'epub':
        parser.error('--unnew only works with epub')

    urls=args

    if not list_only and not (args or any((options.infile,
                                           options.downloadimap,
                                           options.downloadlist))):
        parser.print_help();
        return

    if options.list:
        configuration = get_configuration(options.list,
                                          passed_defaultsini,
                                          passed_personalini,options)
        retlist = get_urls_from_page(options.list, configuration)
        print('\n'.join(retlist))

    if options.normalize:
        configuration = get_configuration(options.normalize,
                                          passed_defaultsini,
                                          passed_personalini,options)
        retlist = get_urls_from_page(options.normalize, configuration,normalize=True)
        print('\n'.join(retlist))

    if options.downloadlist:
        configuration = get_configuration(options.downloadlist,
                                          passed_defaultsini,
                                          passed_personalini,options)
        retlist = get_urls_from_page(options.downloadlist, configuration)
        urls.extend(retlist)

    if options.imaplist or options.downloadimap:
        # list doesn't have a supported site.
        configuration = get_configuration('test1.com',passed_defaultsini,passed_personalini,options)
        markread = configuration.getConfig('imap_mark_read') == 'true' or \
            (configuration.getConfig('imap_mark_read') == 'downloadonly' and options.downloadimap)
        retlist = get_urls_from_imap(configuration.getConfig('imap_server'),
                                     configuration.getConfig('imap_username'),
                                     configuration.getConfig('imap_password'),
                                     configuration.getConfig('imap_folder'),
                                     markread)

        if options.downloadimap:
            urls.extend(retlist)
        else:
            print('\n'.join(retlist))

    # for passing in a file list
    if options.infile:
        with open(options.infile,"r") as infile:
            #print("file exists and is readable")
            for url in infile:
                if '#' in url:
                    url = url[:url.find('#')].strip()
                url = url.strip()
                if len(url) > 0:
                    #print("url: (%s)"%url)
                    urls.append(url)

    if options.save_cache:
        try:
            with open(global_cache,'rb') as jin:
                options.pagecache = pickle_load(jin)
            options.cookiejar = cl.LWPCookieJar()
            options.cookiejar.load(global_cookies)
        except Exception as e:
            print("Didn't load --save-cache %s"%e)

    if not list_only:
        if len(urls) < 1:
            print("No valid story URLs found")
        else:
            for url in urls:
                try:
                    do_download(url,
                                options,
                                passed_defaultsini,
                                passed_personalini)
                    # print("pagecache:%s"%options.pagecache.keys())
                except Exception as e:
                    if len(urls) == 1:
                        raise
                    print("URL(%s) Failed: Exception (%s). Run URL individually for more detail."%(url,e))
Exemple #3
0
def main(argv=None,
         parser=None,
         passed_defaultsini=None,
         passed_personalini=None):
    if argv is None:
        argv = sys.argv[1:]
    # read in args, anything starting with -- will be treated as --<varible>=<value>
    if not parser:
        parser = OptionParser('usage: %prog [options] [STORYURL]...')
    parser.add_option(
        '-f',
        '--format',
        dest='format',
        default='epub',
        help='write story as FORMAT, epub(default), mobi, txt or html',
        metavar='FORMAT')

    if passed_defaultsini:
        config_help = 'read config from specified file(s) in addition to calibre plugin personal.ini, ~/.fanficfare/personal.ini, and ./personal.ini'
    else:
        config_help = 'read config from specified file(s) in addition to ~/.fanficfare/defaults.ini, ~/.fanficfare/personal.ini, ./defaults.ini, and ./personal.ini'
    parser.add_option('-c',
                      '--config',
                      action='append',
                      dest='configfile',
                      default=None,
                      help=config_help,
                      metavar='CONFIG')
    parser.add_option('-b',
                      '--begin',
                      dest='begin',
                      default=None,
                      help='Begin with Chapter START',
                      metavar='START')
    parser.add_option('-e',
                      '--end',
                      dest='end',
                      default=None,
                      help='End with Chapter END',
                      metavar='END')
    parser.add_option('-o',
                      '--option',
                      action='append',
                      dest='options',
                      help='set an option NAME=VALUE',
                      metavar='NAME=VALUE')
    parser.add_option(
        '-m',
        '--meta-only',
        action='store_true',
        dest='metaonly',
        help=
        'Retrieve metadata and stop.  Or, if --update-epub, update metadata title page only.',
    )
    parser.add_option(
        '--json-meta',
        action='store_true',
        dest='jsonmeta',
        help=
        'When used with --meta-only, output metadata as JSON.  No effect without --meta-only flag',
    )
    parser.add_option(
        '-u',
        '--update-epub',
        action='store_true',
        dest='update',
        help=
        'Update an existing epub(if present) with new chapters.  Give either epub filename or story URL.',
    )
    parser.add_option(
        '--update-cover',
        action='store_true',
        dest='updatecover',
        help=
        'Update cover in an existing epub, otherwise existing cover (if any) is used on update.  Only valid with --update-epub.',
    )
    parser.add_option(
        '--unnew',
        action='store_true',
        dest='unnew',
        help='Remove (new) chapter marks left by mark_new_chapters setting.',
    )
    parser.add_option(
        '--force',
        action='store_true',
        dest='force',
        help=
        'Force overwrite of an existing epub, download and overwrite all chapters.',
    )
    parser.add_option(
        '-i',
        '--infile',
        help=
        'Give a filename to read for URLs (and/or existing EPUB files with --update-epub).',
        dest='infile',
        default=None,
        metavar='INFILE')

    parser.add_option(
        '-l',
        '--list',
        dest='list',
        default=None,
        metavar='URL',
        help='Get list of valid story URLs from page given.',
    )
    parser.add_option(
        '-n',
        '--normalize-list',
        dest='normalize',
        default=None,
        metavar='URL',
        help=
        'Get list of valid story URLs from page given, but normalized to standard forms.',
    )
    parser.add_option(
        '--download-list',
        dest='downloadlist',
        default=None,
        metavar='URL',
        help=
        'Download story URLs retrieved from page given.  Update existing EPUBs if used with --update-epub.',
    )

    parser.add_option(
        '--imap',
        action='store_true',
        dest='imaplist',
        help=
        'Get list of valid story URLs from unread email from IMAP account configured in ini.',
    )

    parser.add_option(
        '--download-imap',
        action='store_true',
        dest='downloadimap',
        help=
        'Download valid story URLs from unread email from IMAP account configured in ini.  Update existing EPUBs if used with --update-epub.',
    )

    parser.add_option(
        '-s',
        '--sites-list',
        action='store_true',
        dest='siteslist',
        default=False,
        help='Get list of valid story URLs examples.',
    )
    parser.add_option(
        '--non-interactive',
        action='store_false',
        dest='interactive',
        default=sys.stdin.isatty() and sys.stdout.isatty(),
        help='Prevent interactive prompts (for scripting).',
    )
    parser.add_option(
        '-d',
        '--debug',
        action='store_true',
        dest='debug',
        help='Show debug and notice output.',
    )
    parser.add_option(
        '-p',
        '--progressbar',
        action='store_true',
        dest='progressbar',
        help=
        'Display a simple progress bar while downloading--one dot(.) per network fetch.',
    )
    parser.add_option(
        '-v',
        '--version',
        action='store_true',
        dest='version',
        help='Display version and quit.',
    )

    ## undocumented feature for development use.  Save page cache and
    ## cookies between runs.  Saves in PWD as files global_cache and
    ## global_cookies
    parser.add_option(
        '--save-cache',
        action='store_true',
        dest='save_cache',
        help=SUPPRESS_HELP,
    )

    options, args = parser.parse_args(argv)

    if options.version:
        print("Version: %s" % version)
        return

    if not options.debug:
        logger.setLevel(logging.WARNING)

    list_only = any((
        options.imaplist,
        options.siteslist,
        options.list,
        options.normalize,
    ))

    if list_only and (args or any(
        (options.downloadimap, options.downloadlist))):
        parser.error(
            'Incorrect arguments: Cannot download and list URLs at the same time.'
        )

    if options.siteslist:
        for site, examples in adapters.getSiteExamples():
            print '\n#### %s\nExample URLs:' % site
            for u in examples:
                print '  * %s' % u
        return

    if options.update and options.format != 'epub':
        parser.error('-u/--update-epub only works with epub')

    if options.unnew and options.format != 'epub':
        parser.error('--unnew only works with epub')

    urls = args

    if not list_only and not (args or any(
        (options.infile, options.downloadimap, options.downloadlist))):
        parser.print_help()
        return

    if options.list:
        configuration = get_configuration(options.list, passed_defaultsini,
                                          passed_personalini, options)
        retlist = get_urls_from_page(options.list, configuration)
        print '\n'.join(retlist)

    if options.normalize:
        configuration = get_configuration(options.normalize,
                                          passed_defaultsini,
                                          passed_personalini, options)
        retlist = get_urls_from_page(options.normalize,
                                     configuration,
                                     normalize=True)
        print '\n'.join(retlist)

    if options.downloadlist:
        configuration = get_configuration(options.downloadlist,
                                          passed_defaultsini,
                                          passed_personalini, options)
        retlist = get_urls_from_page(options.downloadlist, configuration)
        urls.extend(retlist)

    if options.imaplist or options.downloadimap:
        # list doesn't have a supported site.
        configuration = get_configuration('test1.com', passed_defaultsini,
                                          passed_personalini, options)
        markread = configuration.getConfig('imap_mark_read') == 'true' or \
            (configuration.getConfig('imap_mark_read') == 'downloadonly' and options.downloadimap)
        retlist = get_urls_from_imap(configuration.getConfig('imap_server'),
                                     configuration.getConfig('imap_username'),
                                     configuration.getConfig('imap_password'),
                                     configuration.getConfig('imap_folder'),
                                     markread)

        if options.downloadimap:
            urls.extend(retlist)
        else:
            print '\n'.join(retlist)

    # for passing in a file list
    if options.infile:
        with open(options.infile, "r") as infile:
            #print "File exists and is readable"
            for url in infile:
                if '#' in url:
                    url = url[:url.find('#')].strip()
                url = url.strip()
                if len(url) > 0:
                    #print "URL: (%s)"%url
                    urls.append(url)

    if options.save_cache:
        try:
            with open('global_cache', 'rb') as jin:
                options.pagecache = pickle.load(jin)  # ,encoding="utf-8"
            options.cookiejar = cl.LWPCookieJar()
            options.cookiejar.load('global_cookies')
        except:
            print("Didn't load global_cache")

    if not list_only:
        if len(urls) < 1:
            print "No valid story URLs found"
        else:
            for url in urls:
                try:
                    do_download(url, options, passed_defaultsini,
                                passed_personalini)
                #print("pagecache:%s"%options.pagecache.keys())
                except Exception, e:
                    if len(urls) == 1:
                        raise
                    print "URL(%s) Failed: Exception (%s). Run URL individually for more detail." % (
                        url, e)
Exemple #4
0
    if options.update and not options.updatecover:
        configuration.set('overrides', 'never_make_cover', 'true')

    # images only for epub, even if the user mistakenly turned it
    # on else where.
    if options.format not in ('epub', 'html'):
        configuration.set('overrides', 'include_images', 'false')

    if options.options:
        for opt in options.options:
            (var, val) = opt.split('=')
            configuration.set('overrides', var, val)

    if options.list or options.normalize:
        retlist = get_urls_from_page(arg,
                                     configuration,
                                     normalize=options.normalize)
        print '\n'.join(retlist)
        return

    try:
        adapter = adapters.getAdapter(configuration, url)

        if not hasattr(options, 'pagecache'):
            options.pagecache = adapter.get_empty_pagecache()
            options.cookiejar = adapter.get_empty_cookiejar()

        adapter.set_pagecache(options.pagecache)
        adapter.set_cookiejar(options.cookiejar)

        adapter.setChaptersRange(options.begin, options.end)
Exemple #5
0
def main(argv=None,
         parser=None,
         passed_defaultsini=None,
         passed_personalini=None):
    if argv is None:
        argv = sys.argv[1:]
    # read in args, anything starting with -- will be treated as --<varible>=<value>
    if not parser:
        parser = OptionParser('usage: %prog [options] [STORYURL]...')
    parser.add_option('-f', '--format', dest='format', default='epub',
                      help='write story as FORMAT, epub(default), mobi, text or html', metavar='FORMAT')

    if passed_defaultsini:
        config_help = 'read config from specified file(s) in addition to calibre plugin personal.ini, ~/.fanficfare/personal.ini, and ./personal.ini'
    else:
        config_help = 'read config from specified file(s) in addition to ~/.fanficfare/defaults.ini, ~/.fanficfare/personal.ini, ./defaults.ini, and ./personal.ini'
    parser.add_option('-c', '--config',
                      action='append', dest='configfile', default=None,
                      help=config_help, metavar='CONFIG')
    parser.add_option('-b', '--begin', dest='begin', default=None,
                      help='Begin with Chapter START', metavar='START')
    parser.add_option('-e', '--end', dest='end', default=None,
                      help='End with Chapter END', metavar='END')
    parser.add_option('-o', '--option',
                      action='append', dest='options',
                      help='set an option NAME=VALUE', metavar='NAME=VALUE')
    parser.add_option('-m', '--meta-only',
                      action='store_true', dest='metaonly',
                      help='Retrieve metadata and stop.  Or, if --update-epub, update metadata title page only.', )
    parser.add_option('-u', '--update-epub',
                      action='store_true', dest='update',
                      help='Update an existing epub(if present) with new chapters.  Give either epub filename or story URL.', )
    parser.add_option('--update-cover',
                      action='store_true', dest='updatecover',
                      help='Update cover in an existing epub, otherwise existing cover (if any) is used on update.  Only valid with --update-epub.', )
    parser.add_option('--unnew',
                      action='store_true', dest='unnew',
                      help='Remove (new) chapter marks left by mark_new_chapters setting.', )
    parser.add_option('--force',
                      action='store_true', dest='force',
                      help='Force overwrite of an existing epub, download and overwrite all chapters.', )
    parser.add_option('-i', '--infile',
                      help='Give a filename to read for URLs (and/or existing EPUB files with --update-epub).',
                      dest='infile', default=None,
                      metavar='INFILE')

    parser.add_option('-l', '--list',
                      dest='list', default=None, metavar='URL',
                      help='Get list of valid story URLs from page given.', )
    parser.add_option('-n', '--normalize-list',
                      dest='normalize', default=None, metavar='URL',
                      help='Get list of valid story URLs from page given, but normalized to standard forms.', )
    parser.add_option('--download-list',
                      dest='downloadlist', default=None, metavar='URL',
                      help='Download story URLs retrieved from page given.  Update existing EPUBs if used with --update-epub.', )

    parser.add_option('--imap',
                      action='store_true', dest='imaplist',
                      help='Get list of valid story URLs from unread email from IMAP account configured in ini.', )

    parser.add_option('--download-imap',
                      action='store_true', dest='downloadimap',
                      help='Download valid story URLs from unread email from IMAP account configured in ini.  Update existing EPUBs if used with --update-epub.', )

    parser.add_option('-s', '--sites-list',
                      action='store_true', dest='siteslist', default=False,
                      help='Get list of valid story URLs examples.', )
    parser.add_option('-d', '--debug',
                      action='store_true', dest='debug',
                      help='Show debug and notice output.', )
    parser.add_option('-v', '--version',
                      action='store_true', dest='version',
                      help='Display version and quit.', )

    options, args = parser.parse_args(argv)

    if options.version:
        print("Version: %s" % version)
        return

    if not options.debug:
        logger = logging.getLogger('fanficfare')
        logger.setLevel(logging.WARNING)

    list_only = any((options.imaplist,
                     options.siteslist,
                     options.list,
                     options.normalize,
                     ))

    if list_only and (args or any((options.downloadimap,
                                   options.downloadlist))):
        parser.error('Incorrect arguments: Cannot download and list URLs at the same time.')

    if options.siteslist:
        for site, examples in adapters.getSiteExamples():
            print '\n#### %s\nExample URLs:' % site
            for u in examples:
                print '  * %s' % u
        return

    if options.update and options.format != 'epub':
        parser.error('-u/--update-epub only works with epub')

    if options.unnew and options.format != 'epub':
        parser.error('--unnew only works with epub')

    urls=args

    if not list_only and not (args or any((options.infile,
                                           options.downloadimap,
                                           options.downloadlist))):
        parser.print_help();
        return

    if options.list:
        configuration = get_configuration(options.list,
                                          passed_defaultsini,
                                          passed_personalini,options)
        retlist = get_urls_from_page(options.list, configuration)
        print '\n'.join(retlist)

    if options.normalize:
        configuration = get_configuration(options.normalize,
                                          passed_defaultsini,
                                          passed_personalini,options)
        retlist = get_urls_from_page(options.normalize, configuration,normalize=True)
        print '\n'.join(retlist)

    if options.downloadlist:
        configuration = get_configuration(options.downloadlist,
                                          passed_defaultsini,
                                          passed_personalini,options)
        retlist = get_urls_from_page(options.downloadlist, configuration)
        urls.extend(retlist)

    if options.imaplist or options.downloadimap:
        # list doesn't have a supported site.
        configuration = get_configuration('test1.com',passed_defaultsini,passed_personalini,options)
        markread = configuration.getConfig('imap_mark_read') == 'true' or \
            (configuration.getConfig('imap_mark_read') == 'downloadonly' and options.downloadimap)
        retlist = get_urls_from_imap(configuration.getConfig('imap_server'),
                                     configuration.getConfig('imap_username'),
                                     configuration.getConfig('imap_password'),
                                     configuration.getConfig('imap_folder'),
                                     markread)

        if options.downloadimap:
            urls.extend(retlist)
        else:
            print '\n'.join(retlist)

    # for passing in a file list
    if options.infile:
        with open(options.infile,"r") as infile:
            #print "File exists and is readable"
            for url in infile:
                if '#' in url:
                    url = url[:url.find('#')].strip()
                url = url.strip()
                if len(url) > 0:
                    #print "URL: (%s)"%url
                    urls.append(url)

    if not list_only:
        if len(urls) < 1:
            print "No valid story URLs found"
        else:
            for url in urls:
                try:
                    do_download(url,
                                options,
                                passed_defaultsini,
                                passed_personalini)
                #print("pagecache:%s"%options.pagecache.keys())
                except Exception, e:
                    if len(urls) == 1:
                        raise
                    print "URL(%s) Failed: Exception (%s). Run URL individually for more detail."%(url,e)
Exemple #6
0
    if options.update and not options.updatecover:
        configuration.set('overrides', 'never_make_cover', 'true')

    # images only for epub, even if the user mistakenly turned it
    # on else where.
    if options.format not in ('epub', 'html'):
        configuration.set('overrides', 'include_images', 'false')

    if options.options:
        for opt in options.options:
            (var, val) = opt.split('=')
            configuration.set('overrides', var, val)

    if options.list or options.normalize:
        retlist = get_urls_from_page(arg, configuration, normalize=options.normalize)
        print '\n'.join(retlist)
        return

    try:
        adapter = adapters.getAdapter(configuration, url)

        if not hasattr(options,'pagecache'):
            options.pagecache = adapter.get_empty_pagecache()
            options.cookiejar = adapter.get_empty_cookiejar()
        
        adapter.set_pagecache(options.pagecache)
        adapter.set_cookiejar(options.cookiejar)
        
        adapter.setChaptersRange(options.begin, options.end)