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)
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)