const request = require('request'); // for fetching the feed
const sqlCmds = require('./sql/commands.js')

module.exports = function (link, feedparser, con, callback) {

  const req = request(link, function (error, response) {
    if (error || response.statusCode !== 200)
      console.log(`RSS Request Error: Problem occured while requesting from "${link}"`);
  });

  req.on('error', function (error) {
    console.log('RSS Request Error: ' + error)
    callback()
  });

  req.on('response', function (res) {
    var stream = this;

    if (res.statusCode !== 200) {
      this.emit('error', new Error(`Bad status code`));
    }
    else {
      stream.pipe(feedparser);
    }

  });

}

@@ -162,17 +162,69 @@
 back) %7B%0A
-%0A
+  var attempts = 0;%0A%0A  (function requestStream() %7B%0A  
   const 
@@ -272,24 +272,26 @@
 onse) %7B%0A    
+  
 if (error %7C%7C
@@ -320,16 +320,18 @@
 == 200)%0A
+  
       co
@@ -411,16 +411,18 @@
 nk%7D%22%60);%0A
+  
   %7D);%0A%0A 
@@ -414,32 +414,34 @@
 %22%60);%0A    %7D);%0A%0A  
+ 
+ 
 req.on('error', 
@@ -455,24 +455,26 @@
 n (error) %7B%0A
+  
     console.
@@ -514,28 +514,17 @@
 )%0A  
-  callback()%0A
   %7D);%0A%0A
+  
   re
@@ -561,16 +561,18 @@
 ) %7B%0A    
+ 
+ 
 var stre
@@ -583,16 +583,18 @@
  this;%0A%0A
+  
     if (
@@ -625,16 +625,18 @@
 %7B%0A      
+ 
+ 
 this.emi
@@ -676,30 +676,351 @@
 code
-%60));%0A    %7D%0A    else %7B%0A
+, attempting to reconnect, attempt #$%7Battempts+1%7D%60));%0A        if (attempts %3C 10) %7B%0A          attempts++;%0A          setTimeout(requestStream,1500);%0A        %7D%0A        else %7B%0A          console.log(%60RSS Request Error: Unable to reconnect. Skipping $%7Blink%7D.%60);%0A          callback();%0A        %7D%0A      %7D%0A      else %7B%0A        console.log(%22success%22);%0A  
     
@@ -1054,16 +1054,26 @@
     
+  
 %7D%0A%0A  
+ 
+ 
 %7D);%0A
+  %7D)()
 %0A%7D%0A

handle request errors by looping