var stream = require("stream");
var needle = require("needle");
var fs = require("fs");
var iconv = require("iconv-lite");
var unzip = require("unzip");
var charsetDetector = require("jschardet");
var zlib = require("zlib");
var _ = require("lodash");

function streamFromPath(path, agent)
{
	if (path instanceof stream.Readable) return path;
	if (path.match("^http")) return needle.get(path, { agent: agent });
	return fs.createReadStream(path);
};

function retrieveSrt(path, cb, options)
{
    var callback = _.once(function(err, res) {
        if (err && options && options.agent) return retrieveSrt(path, cb); // Re-try without agent
        cb(err, res);
    });
    
	var stream = streamFromPath(path, options && options.agent);
    stream.on("error", function(err) { callback(err) });
    if (path.match("zip$")) {
        var foundSrt = false, bufs = [];
        stream.pipe(unzip.Parse())
        .on("entry", function(entry) {
            // TODO: think about the case when we find multiple srt's; currently this is an unseen case
            if (!entry.path.match("srt$") || foundSrt) return;
            foundSrt = true;
            entry.on("data", function(dat) { bufs.push(dat) });
            entry.on("end", onDownloaded);
            entry.on("error", function(e) { callback(e) });
        })
        .on("error", function(e) { callback(e) })
        .on("close", function() {
            if (! foundSrt) { callback(new Error("no srt found in zip file "+path)); }
        });
    } else {
        if (path.match("gz$")) var stream = stream.pipe(zlib.createUnzip());
        var bufs = [];
        stream.on("data", function(dat) { bufs.push(dat) })
        stream.on("end", onDownloaded);
        stream.on("error", function(e) { callback(e) });                
    }

    function onDownloaded() {
        //if (! bufs.length) HANDLE ERROR

        var buf = Buffer.concat(bufs);

        if (options.charset && options.charset != "auto") var charset = options.charset;
        else var charset = charsetDetector.detect(buf).encoding;

        try { buf = iconv.decode(buf, charset) } catch(e) { callback(e); return };

        callback(null, buf);           
    };    
};

module.exports = {
	streamFromPath: streamFromPath,
	retrieveSrt: retrieveSrt,
};

@@ -529,16 +529,23 @@
 err, res
+, subnm
 ) %7B%0A    
@@ -658,16 +658,23 @@
 err, res
+, subnm
 );%0A    %7D
@@ -676,20 +676,16 @@
     %7D);%0A
-    
 %0A%09var st
@@ -1060,16 +1060,17 @@
      if 
+(
 (!entry.
@@ -1087,16 +1087,75 @@
 (%22srt$%22)
+ && !entry.path.match(%22sub$%22) && !entry.path.match(%22vtt$%22))
  %7C%7C foun
@@ -1285,36 +1285,71 @@
 y.on(%22end%22, 
-onDownloaded
+function() %7B%0A%09%09%09%09onDownloaded(entry.path);%0A%09%09%09%7D
 );%0A         
@@ -1548,18 +1548,23 @@
 or(%22no s
-rt
+ubtitle
  found i
@@ -1569,15 +1569,14 @@
  in 
-zip fil
+archiv
 e %22+
@@ -1870,24 +1870,8 @@
  %7D);
-                
 %0A   
@@ -1900,16 +1900,21 @@
 nloaded(
+subnm
 ) %7B%0A    
@@ -2261,21 +2261,17 @@
  buf
-);           
+, subnm);
 %0A   

Return Selected Subtitle Filename from Archives