def do_download(service_name, comic_id, account):
    service = account.services[service_name]
    issue = service.get_issue(comic_id)
    active_downloads[comic_id] = {
            'title': '%s #%s' % (issue.title, issue.num),
            'message': '',
            'progress': 0
            }

    def on_cbz_progress(value=None, message=None, error=None):
        if value is not None:
            active_downloads[comic_id]['progress'] = value
        if message is not None:
            active_downloads[comic_id]['message'] = message
        if error is not None:
            active_downloads[comic_id]['message'] = "ERROR: " + error

    try:
        library = CbzLibrary(g.account.library_path)
        builder = CbzBuilder()
        builder.set_watermark(service_name, service.username)
        builder.set_temp_folder(cache_dir)
        builder.set_progress_subscriber(on_cbz_progress)
        app.logger.debug('Downloading %s [%s] to: %s' % (issue.title, comic_id, account.library_path))
        builder.save(issue, in_library=library)
    except Exception as e:
        app.logger.error('Error downloading comic: %s' % e)
    finally:
        active_downloads.pop(comic_id)
def sync(service_name, series_id):
    if service_name is None:
        services = [v for k, v in g.account.services.iteritems()]
    else:
        services = [g.account.services[service_name]]

    library = CbzLibrary(g.account.library_path)
    builder = CbzBuilder()
    builder.set_temp_folder(cache_dir)
    # TODO: progress subscriber
    for service in services:
        builder.set_watermark(service.service_name, service.username)

        # Get all issues for the given series, or all issues ever if `None`.
        issues = service.get_all_issues(series_id)
        library.sync_issues(builder, issues) 
def sync(query=None, service_name=None, series_id=None, new_only=False, metadata_only=False, lib_dir=None):
    ''' Synchronizes the local comicbook library with the connected or specified services.
    '''
    if query is not None and series_id is not None:
        raise Exception("Can't specify both a query and a series ID.")

    service = _get_service_safe(service_name)

    if lib_dir is None:
        account = _get_account()
        lib_dir = account.library_path
    out_path = lib_dir.strip('\'" ')
    library = CbzLibrary(out_path)

    if series_id is None:
        if query is None:
            app.logger.info("Getting all issues from %s..." % service.service_label)
            issues = service.get_collection().get_issues()
        else:
            issues = []
            query = query.strip('\'" ')
            collection = service.get_collection()
            for series in collection:
                if not re.search(query, series.get_display_title(), re.IGNORECASE):
                    continue
                app.logger.info("Getting issues from %s for: %s" % (service.service_label, series.get_display_title()))
                issues += series.get_issues()
    else:
        app.logger.info("Getting issues from %s for series ID %s" % (service.service_label, series_id))
        issues = service.get_collection().get_series(series_id).get_issues()

    app.logger.info("Syncing issues...")
    builder = CbzBuilder(service, subscriber=CliDownloadProgress(), temp_folder=cache_dir)
    builder.username = service.username
    library.sync_issues(builder, issues, 
            new_only=new_only,
            metadata_only=metadata_only)
def download(issue_id, service_name=None, output=None, metadata_only=False):
    ''' Downloads comicbook issues.
    '''
    service = _get_service_safe(service_name)

    issue = service.get_issue(issue_id)
    app.logger.info("[%s] %s" % (issue.comic_id, issue.get_display_title()))
    
    if output is None:
        account = _get_account()
        library = CbzLibrary(account.library_path)
        output = library.get_issue_path(issue)
    out_path = output.strip('\'" ')

    builder = CbzBuilder(service, subscriber=CliDownloadProgress(), temp_folder=cache_dir)
    builder.username = service.username
    if metadata_only:
        builder.update(issue, out_path=out_path)
        app.logger.info("Issue updated at: %s" % out_path)
    else:
        builder.save(issue, out_path=out_path)
        app.logger.info("Issue saved at: %s" % out_path)