var async = require('async'),
    request = require('request'),
    models  = require('../../models'),
    fs = require('fs'),
    filingQueue = require('./import');

var lookAhead = 100,
    lookBehind = 100,
    interval = 3000;

var temp_dir = path.resolve(__dirname + '/../../data/fec/downloaded');

function checkForFiling(filing_id,cb) {
    console.log('checking for ' + filing_id);

    var filePath = temp_dir + '/' + filing_id + '.fec';

    fs.exists(filePath,function (exists) {
        if (!exists) {
           request
                .get('http://docquery.fec.gov/dcdev/posted/' + filing_id + '.fec')
                .on('error', function(err) {
                    console.log(err);

                    setTimeout(cb,interval);
                })
                .on('end',function () {
                    filingQueue.push({
                        name: filing_id,
                        openStream: function (cb) {
                            fs.createReadStream(filePath, cb);
                        }
                    });

                    setTimeout(cb,interval);
                })
                .pipe(fs.createWriteStream(filePath));
        }
        else {
            cb();
        }
    });
}

function queueFilingsToCheck() {
    models.fec_filing.findAll({
        attributes: ['filing_id'],
        limit: lookBehind,
        order: [['filing_id','DESC']]
    })
    .then(function (filings) {
        filings = filings.map(function (filing) {
            return filing.filing_id;
        });

        var q = async.queue(checkForFiling,1);

        for (var i = filings[filings.length-1]; i <= filings[0]+lookAhead; i++) {
            if (filings.indexOf(i) === -1) {
                q.push(i);
            }
        }

        q.drain = queueFilingsToCheck;
    });
}

@@ -172,26 +172,25 @@
 lookAhead = 
-10
+5
 0,%0A    lookB
@@ -197,17 +197,17 @@
 ehind = 
-1
+2
 00,%0A    
@@ -340,55 +340,8 @@
 ) %7B%0A
-    console.log('checking for ' + filing_id);%0A%0A
     
@@ -451,32 +451,86 @@
  if (!exists) %7B%0A
+            console.log('downloading ' + filing_id);%0A%0A
            reque
@@ -799,24 +799,85 @@
 nction () %7B%0A
+                    console.log('downloaded ' + filing_id);%0A%0A
             
@@ -942,16 +942,21 @@
 iling_id
+ + ''
 ,%0A      
@@ -1029,16 +1029,24 @@
         
+cb(null,
 fs.creat
@@ -1069,12 +1069,9 @@
 Path
-, cb
+)
 );%0A 
@@ -1874,16 +1874,40 @@
 heck;%0A    %7D);%0A%7D%0A
+%0AqueueFilingsToCheck();%0A

Fix filing check