Scan Downloader
    Here we will attempt to download all of the scans that have completed between
    now and AGE days ago.

    sc = SecurityCenter5 object
    age = how many days back do we want to pull? (default: 0)
    unzip = Do we want to uncompress the nessus files? (default: False)
    path = Path where the resulting data will be placed. (default: scans)

def download_scans(sc, age=0, unzip=False, path='scans'):
    '''Scan Downloader
    Here we will attempt to download all of the scans that have completed between
    now and AGE days ago.

    sc = SecurityCenter5 object
    age = how many days back do we want to pull? (default: 0)
    unzip = Do we want to uncompress the nessus files? (default: False)
    path = Path where the resulting data will be placed. (default: scans)
    '''

    # if the download path doesn't exist, we need to create it.
    if not os.path.exists(path):
        logger.debug('scan path didn\'t exist. creating it.')
        os.makedirs(path)

    # Now we will need to comuter the timestamp for the date that the age has
    # apecified.  The API expects this in a unix timestamp format.
    findate = (date.today() - timedelta(days=age))

    # Lets get the list of scans that had completed within the timefram that we
    # had specified.
    logger.debug('getting scan results for parsing')
    resp = sc.get('scanResult', params={
        'startTime': int(time.mktime(findate.timetuple())),
        'fields': 'name,finishTime,downloadAvailable,repository',
    })

    for scan in resp.json()['response']['usable']:
        # If this particular scan does not have any results (either it was a 
        # partial, failed, or incomplete scan) then we have nothing further to
        # do and should simply ignore this scan.
        if scan['downloadAvailable'] == 'false':
            logger.debug('%s/"%s" not available for download' % (scan['id'], 
                                                                    scan['name']))
        else:
            # Well look, this scan actually has results, lets go ahead and pull
            # them down.
            logger.debug('%s/"%s" downloading' % (scan['id'], scan['name']))
            scandata = sc.post('scanResult/%s/download' % scan['id'], 
                              json={'downloadType': 'v2'})
            sfin = datetime.fromtimestamp(int(scan['finishTime']))

            # The filename is being computed generically here.  As this will be
            # used whether we extract the .nessus file out of the zipfile or
            # not.
            filename = '%s-%s.%s.%s' % (scan['id'], 
                                        scan['name'].replace(' ', '_'), 
                                        scan['repository']['id'],
                                        sfin.strftime('%Y.%m.%d-%H.%M'))
            if unzip:
                # Unzip that .nessus file!
                logger.debug('extracting %s/%s' % (scan['id'], scan['name']))
                zfile = ZipFile(StringIO(buf=scandata.content))
                scanfile = zfile.filelist[0]
                scanfile.filename = '%s.nessus' % filename
                zfile.extract(scanfile, path=path)
            else:
                # We want to keep it compressed, just dump to disk.
                logger.debug('writing zip for %s/%s' % (scan['id'], scan['name']))
                with open('%s.zip' % filename, 'wb') as zfile:
                    zfile.write(scandata.content)

            # Were done with this scan file!!!
            logger.info('%s/"%s" downloaded' % (scan['id'], scan['name']))