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AS,WACf,QAAQ,IAAI,oDACZ,IACc,GAAG,UAAU,EAAS,IAAM,EAAK,cACnC,UACN,GAAG,WAAW,EAAS,IAAM,EAAK,aAExC,CAAE,MAAO,GAET,CACA,EAAK,KAAK,QAAS,EAAK,QAAS,EAAK,MAC1C,IACA,EAAK,QAAQ,cAAgB,EAAS,IAAM,EAAK,aACjD,EAAK,QAAQ,WAAa,EAC1B,IAII,GAFA,MAAQ,GAAG,UAAU,EAAS,IAAM,EAAK,cAErC,MAAM,SACN,QAAQ,IAAI,0BAA4B,EAAS,IAAM,EAAK,cAC3C,GAAG,iBAAiB,EAAS,IAAM,EAAK,cAC9C,KAAK,EAExB,CACA,MAAO,GACL,KAAK,IAAI,EAAK,SAAS,SAAU,GAC/B,EAAS,KAAK,GAAa,GAAG,UAAU,WACpC,GAAG,OAAO,EAAS,IAAM,EAAK,aAAe,YAAa,EAAS,IAAM,EAAK,cAAc,SAAU,IACtG,QAAQ,IAAI,gBAAkB,EAAS,IAAM,EAAK,cACnC,IAID,EAAK,KAAK,QAAS,EAAK,QAAS,EAAK,OACtC,QAAQ,IAAI,+BAAiC,EAAS,IAAM,EAAK,eAJhD,GAAG,iBAAiB,EAAS,IAAM,EAAK,cAC9C,KAAK,EAK/B,GACH,GACF,IAAG,GAAG,SAAS,SAAS,GACtB,QAAQ,IAAI,GACZ,EAAK,KAAK,QAAS,EAAK,QAAS,EAAK,MACxC,GACF,CACJ,CACH,EAED,KAAK,SAAS,OAAO,QAAU","file":"/npm/m3u8-downloader@0.0.4/lib/downloader.js","sourceRoot":"","sourcesContent":["/**\n * Minified by jsDelivr using Terser v5.39.0.\n * Original file: /npm/m3u8-downloader@0.0.4/lib/downloader.js\n *\n * Do NOT use SRI with dynamically generated files! More information: https://www.jsdelivr.com/using-sri-with-dynamic-files\n */\n","var m3u8 = require('m3u8');\nvar http = require('http');\nvar fs   = require('fs');\nvar util = require('util');\nvar touch = require('touch');\nvar util = require('util'),\n    EventEmitter = require('events').EventEmitter;\n\nmodule.exports = function (options , tmpDir) {\n    this.options = options;\n    this.tmpDir = tmpDir;\n    this.queue = {};\n    this.baseUrl = options.path.substring(0, options.path.lastIndexOf('/')+1);\n    this.done = [];\n    if (!fs.existsSync(tmpDir)){\n        fs.mkdirSync(tmpDir);\n    }\n    this.playlistFile = options.path.split('/').pop();\n    //TODO ::  If the file Content has changed I want to Download It.\n    var  fileDownloadAndUpdateStatus = function (fileUrl, self ,cb){\n        var queue = self.queue;\n        queue[fileUrl].started = true;\n        stats = fs.stat(tmpDir + '/' + fileUrl, function (err, stat){\n          if(err === null) {\n            console.log('Already Segment is downloaded ' + fileUrl);\n            queue[fileUrl].finished = true;\n            queue[fileUrl].error = null;\n            queue[fileUrl].errorCount = 0;\n            cb(self);\n          } else if (err.code == 'ENOENT') {\n              // file does not existsSync\n              var writeStream = fs.createWriteStream(tmpDir + '/' + fileUrl + '.download');\n              var options = JSON.parse(JSON.stringify(self.options));\n              options.path = self.baseUrl + fileUrl;\n              console.log('New Segment to download ' + fileUrl);\n\n              var request = http.get(options, function (response) {\n                  response.pipe(writeStream);\n                  writeStream.on('finish', function () {\n                      if (response.headers['last-modified'])\n                          touch.ftouchSync(fd, { 'mtime': new Date(response.headers['last-modified']) });\n                      writeStream.close();\n                      fs.rename(tmpDir + '/' + fileUrl + '.download', tmpDir + '/' + fileUrl, function (err) {\n                          if (!err) {\n                              queue[fileUrl].finished = true;\n                              queue[fileUrl].error = null;\n                              queue[fileUrl].errorCount = 0;\n                              cb(self);\n                          } else {\n                              queue[fileUrl].error = err;\n                              queue[fileUrl].errorCount++;\n                              cb(self);\n                          }\n                      });\n                  });\n              }, function (err) {\n                  //TODO :: If an error occur\n                  console.log('Error Ocuured in HTTP Get ' + fileUrl);\n                  queue[fileUrl].error = err;\n                  queue[fileUrl].errorCount++;\n                  cb(self);\n              }).on('error', function(err){\n                console.log(err);\n                queue[fileUrl].error = err;\n                queue[fileUrl].errorCount++;\n                cb(self);\n              });\n\n          } else {\n            console.log('Some other error: ', err.code);\n            queue[fileUrl].error = err;\n            queue[fileUrl].errorCount++;\n            cb(self);\n          }\n        });\n    };\n\n    var downloadAllSegments = function(self){\n        var fileUrls = Object.keys(self.queue).filter(function (data){\n            var dd = self.queue[data];\n            return dd.finished !== true  &&  dd.errCount < 5 ;\n        });\n        if (fileUrls.length> 0){\n            var filename = fileUrls[0];\n            console.log(filename);\n            fileDownloadAndUpdateStatus(filename, self, downloadAllSegments);\n        } else {\n            fileUrls = Object.keys(self.queue).filter(function (data){\n                var dd = self.queue[data];\n                return dd.finished === true;\n            });\n\n            if (fileUrls.length == Object.keys(self.queue).length) {\n                self.emit('finished', self.options, self.queue);\n            } else {\n                self.emit('error', self.options, self.queue);\n            }\n        }\n    };\n\n    this.startDownload = function(){\n        console.log('start download called');\n        var parser = m3u8.createStream();\n        var self = this;\n        var writeStream = fs.createWriteStream(tmpDir + '/' + this.playlistFile + '.download');\n        //TODO:: This will be able to read HLS File as stream\n        parser.on('item', function(item) {\n            // emits PlaylistItem, MediaItem, StreamItem, and IframeStreamItem\n            var filename = item.properties.uri;\n            self.queue[filename] = {started : false, errCount : 0};\n        });\n        parser.on('m3u', function(m3u) {\n           // fully parsed m3u file  Now start Downloading files\n           downloadAllSegments(self);\n        });     \n        parser.on('error', function(){\n            console.log('Error occured in parsing m3u8 file. Ignoring ...');\n            try{\n              var stats = fs.lstatSync(tmpDir + '/' + self.playlistFile);\n              if (stats.isFile()) {\n                  fs.unlinkSync(tmpDir + '/' + self.playlistFile);\n              }\n            } catch (err){\n\n            }\n            self.emit('error', self.options, self.queue);\n        });\n        self.options.indexFileName = tmpDir + '/' + self.playlistFile;\n        self.options.folderName = tmpDir;\n        try {\n            // Query the entry\n            stats = fs.lstatSync(tmpDir + '/' + self.playlistFile);\n            // Is it a directory?\n            if (stats.isFile()) {\n                console.log('File Already Downloaded' + tmpDir + '/' + self.playlistFile);\n                var readStream = fs.createReadStream(tmpDir + '/' + self.playlistFile);\n                readStream.pipe(parser);\n            }\n        }\n        catch (e) {\n          http.get(self.options, function (response){\n            response.pipe(writeStream).on('finish', function (){\n                fs.rename(tmpDir + '/' + self.playlistFile + '.download', tmpDir + '/' + self.playlistFile, function (err) {\n                console.log('File renamed ' + tmpDir + '/' + self.playlistFile);\n                          if (!err) {\n                              var readStream = fs.createReadStream(tmpDir + '/' + self.playlistFile);\n                              readStream.pipe(parser);\n                          } else {\n                              self.emit('error', self.options, self.queue);\n                              console.log('File renamed  Errorr Occured' + tmpDir + '/' + self.playlistFile);\n                          }\n               });\n            });\n          }).on('error', function(err){\n            console.log(err);\n            self.emit('error', self.options, self.queue);\n          });\n        }\n    };\n };\n\nutil.inherits(module.exports , EventEmitter);\n"]}