webseeded-torrent-generator
Version:
Generate webseeded torrent files from urls to files.
148 lines (136 loc) • 4.25 kB
JavaScript
var Torrent = require('../lib/torrent');
var fs = require('fs');
var path = require('path');
var q = require('q');
var getMetadata = function (opts) {
var torrent = new Torrent(opts.base, opts.name, opts.files);
torrent.getMetadata().then(function (metadata) {
console.log('metadata', metadata.toString());
console.log('output', opts.output);
fs.writeFileSync(opts.output, metadata);
torrent.getFiles().then(function (files) {
console.log('files');
console.log(JSON.stringify(files, null, 4));
});
}).fail(function (err) {
console.warn(err);
});
};
var getFiles = function (opts) {
var torrent = new Torrent(opts.base, opts.name, opts.files);
torrent.getFiles().then(function (files) {
console.log('files');
console.log(JSON.stringify(files, null, 4));
var bencode = require('bencode');
console.log(bencode.decode(bencode.encode(files)));
}).fail(function (err) {
console.warn(err);
});
};
var getDebug = function (opts) {
var torrent = new Torrent(opts.base, opts.name, opts.files);
return q.all([
torrent.getMetadata(),
torrent.getOriginator(),
torrent.getUpdateUrl(),
torrent.getAnnounce(),
torrent.getAnnounceList(),
torrent.getInfo(),
torrent.getPrivateKeys(),
torrent.getSignatures(),
torrent.getInfoHash(),
torrent.getUrlList(),
torrent.getName(),
torrent.getPieceLength(),
torrent.getPieces(),
torrent.getFiles()
]).spread(function (metadata, originator, updateUrl, announce, announceList, info, privateKeys, signatures, infoHash, urlList, name, pieceLength, pieces, files) {
console.log('updateUrl\n', updateUrl, '\n\n');
console.log('announce\n', announce, '\n\n');
console.log('announceList\n', JSON.stringify(announceList, null, 4), '\n\n');
console.log('privateKeys\n', privateKeys, '\n\n');
console.log('signatures\n', signatures, '\n\n');
console.log('infoHash\n', infoHash, '\n\n');
console.log('urlList\n', JSON.stringify(urlList, null, 4), '\n\n');
console.log('name\n', name, '\n\n');
console.log('pieceLength\n', pieceLength, '\n\n');
console.log('pieces\n', pieces.length, '\n\n');
console.log('files\n', JSON.stringify(files, null, 4), '\n\n');
});
torrent.debug().then(function () {
console.log('done');
}).fail(function (err) {
console.warn(err);
});
};
var parser = require('nomnom');
parser.command('metadata')
.help('get webseeded torrent metadata')
.option('output', {
abbr: 'o',
metavar: 'PATH',
help: 'Where to write the output file'
})
.option('base', {
abbr: 'b',
required: true,
help: 'Base of the file urls'
})
.option('name', {
abbr: 'n',
required: true,
help: 'Name of the torrent'
})
.option('files', {
abbr: 'f',
required: true,
list: true,
help: 'List of files to add to torrent'
})
.callback(function (opts) {
getMetadata(opts);
});
parser.command('files')
.help('get webseeded torrent files')
.option('base', {
abbr: 'b',
required: true,
help: 'Base of the file urls'
})
.option('name', {
abbr: 'n',
required: true,
help: 'Name of the torrent'
})
.option('files', {
abbr: 'f',
required: true,
list: true,
help: 'List of files to add to torrent'
})
.callback(function (opts) {
getFiles(opts);
});
parser.command('debug')
.help('get webseeded torrent debug info')
.option('base', {
abbr: 'b',
required: true,
help: 'Base of the file urls'
})
.option('name', {
abbr: 'n',
required: true,
help: 'Name of the torrent'
})
.option('files', {
abbr: 'f',
required: true,
list: true,
help: 'List of files to add to torrent'
})
.callback(function (opts) {
getDebug(opts);
});
parser.parse();