Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added eslint configuration #2

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions .eslintrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"extends": "eslint-config-es5"
}

4 changes: 4 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,5 +31,9 @@
"promise-streams": "^1.0.1",
"requestretry": "^1.8.0",
"sync-request": "^3.0.1"
},
"devDependencies": {
"eslint": "^4.19.1",
"eslint-config-es5": "^0.5.0"
}
}
28 changes: 14 additions & 14 deletions src/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,20 @@
var path = require('path');
var start = require('./index');
var pessimist = require('pessimist')
.usage('Fetch and save the contents of an HLS playlist locally.\nUsage: $0 ')
.alias('i', 'input')
.demand('i')
.describe('i', 'uri to m3u8 (required)')
.alias('o', 'output')
.default('o', './hls-fetcher')
.describe('o', "output path (default:'./hls-fetcher')")
.alias('c', 'concurrency')
.default('c', Infinity)
.describe('c', 'number of simultaneous fetches (default: Infinity)')
.alias('d', 'decrypt')
.default('d', false)
.describe('d', 'decrypt and remove enryption from manifest (default: false)')
.argv;
.usage('Fetch and save the contents of an HLS playlist locally.\nUsage: $0 ')
.alias('i', 'input')
.demand('i')
.describe('i', 'uri to m3u8 (required)')
.alias('o', 'output')
.default('o', './hls-fetcher')
.describe('o', "output path (default:'./hls-fetcher')")
.alias('c', 'concurrency')
.default('c', Infinity)
.describe('c', 'number of simultaneous fetches (default: Infinity)')
.alias('d', 'decrypt')
.default('d', false)
.describe('d', 'decrypt and remove enryption from manifest (default: false)')
.argv;

// Make output path
var output = path.resolve(pessimist.o);
Expand Down
2 changes: 1 addition & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ var WalkManifest = require('./walk-manifest');
var WriteData = require('./write-data');

var main = function(options) {
console.log("Gathering Manifest data...");
console.log('Gathering Manifest data...');
var resources = WalkManifest(options.decrypt, options.output, options.input);

return WriteData(options.decrypt, options.concurrency, resources);
Expand Down
2 changes: 1 addition & 1 deletion src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ var Utils = {
fileExists: function(file) {
try {
return fs.statSync(file).isFile();
} catch(e) {
} catch (e) {
return false;
}
},
Expand Down
210 changes: 105 additions & 105 deletions src/walk-manifest.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,18 @@ var path = require('path');
var fs = require('fs');

var joinURI = function(absolute, relative) {
var parse = url.parse(absolute);
parse.pathname = path.join(parse.pathname, relative);
return url.format(parse);
var parse = url.parse(absolute);
parse.pathname = path.join(parse.pathname, relative);
return url.format(parse);
};


var isAbsolute = function(uri) {
var parsed = url.parse(uri);
if (parsed.protocol) {
return true;
}
return false;
var parsed = url.parse(uri);
if (parsed.protocol) {
return true;
}
return false;
};

var mediaGroupPlaylists = function(mediaGroups) {
Expand Down Expand Up @@ -45,111 +45,111 @@ var parseManifest = function(content) {
};

var parseKey = function(basedir, decrypt, resources, manifest, parent) {
if (!manifest.parsed.segments[0] || !manifest.parsed.segments[0].key) {
return {};
}
var key = manifest.parsed.segments[0].key;

var keyUri = key.uri;
if (!isAbsolute(keyUri)) {
keyUri = joinURI(path.dirname(manifest.uri), keyUri);
}

// if we are not decrypting then we just download the key
if (!decrypt) {
// put keys in parent-dir/key-name.key
key.file = basedir;
if (parent) {
key.file = path.dirname(parent.file);
}
key.file = path.join(key.file, path.basename(key.uri));

manifest.content = new Buffer(manifest.content.toString().replace(
key.uri,
path.relative(path.dirname(manifest.file), key.file)
));
key.uri = keyUri;
resources.push(key);
return key;
}

// get the aes key
var keyContent = syncRequest('GET', keyUri).getBody();
key.bytes = new Uint32Array([
keyContent.readUInt32BE(0),
keyContent.readUInt32BE(4),
keyContent.readUInt32BE(8),
keyContent.readUInt32BE(12)
]);

// remove the key from the manifest
manifest.content = new Buffer(manifest.content.toString().replace(
new RegExp('.*' + key.uri + '.*'),
''
));


return key;
if (!manifest.parsed.segments[0] || !manifest.parsed.segments[0].key) {
return {};
}
var key = manifest.parsed.segments[0].key;

var keyUri = key.uri;
if (!isAbsolute(keyUri)) {
keyUri = joinURI(path.dirname(manifest.uri), keyUri);
}

// if we are not decrypting then we just download the key
if (!decrypt) {
// put keys in parent-dir/key-name.key
key.file = basedir;
if (parent) {
key.file = path.dirname(parent.file);
}
key.file = path.join(key.file, path.basename(key.uri));

manifest.content = new Buffer(manifest.content.toString().replace(
key.uri,
path.relative(path.dirname(manifest.file), key.file)
));
key.uri = keyUri;
resources.push(key);
return key;
}

// get the aes key
var keyContent = syncRequest('GET', keyUri).getBody();
key.bytes = new Uint32Array([
keyContent.readUInt32BE(0),
keyContent.readUInt32BE(4),
keyContent.readUInt32BE(8),
keyContent.readUInt32BE(12)
]);

// remove the key from the manifest
manifest.content = new Buffer(manifest.content.toString().replace(
new RegExp('.*' + key.uri + '.*'),
''
));


return key;
};

var walkPlaylist = function(decrypt, basedir, uri, parent, manifestIndex) {
var resources = [];
var manifest = {};
manifest.uri = uri;
manifest.file = path.join(basedir, path.basename(uri));
resources.push(manifest);

// if we are not the master playlist
if (parent) {
manifest.file = path.join(
path.dirname(parent.file),
'manifest' + manifestIndex,
path.basename(manifest.file)
);
// get the real uri of this playlist
if (!isAbsolute(manifest.uri)) {
manifest.uri = joinURI(path.dirname(parent.uri), manifest.uri);
}
// replace original uri in file with new file path
parent.content = new Buffer(parent.content.toString().replace(uri, path.relative(path.dirname(parent.file), manifest.file)));
}
var resources = [];
var manifest = {};
manifest.uri = uri;
manifest.file = path.join(basedir, path.basename(uri));
resources.push(manifest);

// if we are not the master playlist
if (parent) {
manifest.file = path.join(
path.dirname(parent.file),
'manifest' + manifestIndex,
path.basename(manifest.file)
);
// get the real uri of this playlist
if (!isAbsolute(manifest.uri)) {
manifest.uri = joinURI(path.dirname(parent.uri), manifest.uri);
}
// replace original uri in file with new file path
parent.content = new Buffer(parent.content.toString().replace(uri, path.relative(path.dirname(parent.file), manifest.file)));
}

manifest.content = syncRequest('GET', manifest.uri).getBody();
manifest.parsed = parseManifest(manifest.content);
manifest.parsed.segments = manifest.parsed.segments || [];
manifest.parsed.playlists = manifest.parsed.playlists || [];
manifest.parsed.mediaGroups = manifest.parsed.mediaGroups || {};
manifest.parsed.segments = manifest.parsed.segments || [];
manifest.parsed.playlists = manifest.parsed.playlists || [];
manifest.parsed.mediaGroups = manifest.parsed.mediaGroups || {};

var playlists = manifest.parsed.playlists.concat(mediaGroupPlaylists(manifest.parsed.mediaGroups));
var key = parseKey(basedir, decrypt, resources, manifest, parent);

// SEGMENTS
manifest.parsed.segments.forEach(function(s, i) {
if (!s.uri) {
return;
}
// put segments in manifest-name/segment-name.ts
s.file = path.join(path.dirname(manifest.file), path.basename(s.uri));
if (!isAbsolute(s.uri)) {
s.uri = joinURI(path.dirname(manifest.uri), s.uri);
}
if (key) {
s.key = key;
s.key.iv = s.key.iv || new Uint32Array([0, 0, 0, manifest.parsed.mediaSequence, i]);
}
manifest.content = new Buffer(manifest.content.toString().replace(s.uri, path.basename(s.uri)));
resources.push(s);
});

// SUB Playlists
playlists.forEach(function(p, z) {
if (!p.uri) {
return;
}
resources = resources.concat(walkPlaylist(decrypt, basedir, p.uri, manifest, z));
});

return resources;
var key = parseKey(basedir, decrypt, resources, manifest, parent);

// SEGMENTS
manifest.parsed.segments.forEach(function(s, i) {
if (!s.uri) {
return;
}
// put segments in manifest-name/segment-name.ts
s.file = path.join(path.dirname(manifest.file), path.basename(s.uri));
if (!isAbsolute(s.uri)) {
s.uri = joinURI(path.dirname(manifest.uri), s.uri);
}
if (key) {
s.key = key;
s.key.iv = s.key.iv || new Uint32Array([0, 0, 0, manifest.parsed.mediaSequence, i]);
}
manifest.content = new Buffer(manifest.content.toString().replace(s.uri, path.basename(s.uri)));
resources.push(s);
});

// SUB Playlists
playlists.forEach(function(p, z) {
if (!p.uri) {
return;
}
resources = resources.concat(walkPlaylist(decrypt, basedir, p.uri, manifest, z));
});

return resources;
};

module.exports = walkPlaylist;
16 changes: 8 additions & 8 deletions src/write-data.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,19 +31,19 @@ var requestFile = function(uri) {
};

var toArrayBuffer = function(buffer) {
var ab = new ArrayBuffer(buffer.length);
var view = new Uint8Array(ab);
for (var i = 0; i < buffer.length; ++i) {
view[i] = buffer[i];
}
return ab;
var ab = new ArrayBuffer(buffer.length);
var view = new Uint8Array(ab);
for (var i = 0; i < buffer.length; ++i) {
view[i] = buffer[i];
}
return ab;
};

var decryptFile = function(content, encryption) {
return new Promise(function(resolve, reject) {
var d = new aesDecrypter(toArrayBuffer(content), encryption.bytes, encryption.iv, function(err, bytes) {
return resolve(new Buffer(bytes));
})
});
});
};

Expand All @@ -59,7 +59,7 @@ var WriteData = function(decrypt, concurrency, resources) {
return requestFile(r.uri).then(function(content) {
return decryptFile(content, r.key);
}).then(function(content) {
return writeFile(r.file, content)
return writeFile(r.file, content);
});
});
} else if (inProgress.indexOf(r.uri) === -1) {
Expand Down
Loading