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

Support RAR/ZIP streams and stream.fileMustInclude #72

Open
wants to merge 7 commits 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
34 changes: 30 additions & 4 deletions src/withStreamingServer/convertStream.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
var magnet = require('magnet-uri');
var createTorrent = require('./createTorrent');
var createRar = require('./createRar');
var createZip = require('./createZip');

function convertStream(streamingServerURL, stream, seriesInfo) {
return new Promise(function(resolve, reject) {
Expand All @@ -22,9 +24,9 @@ function convertStream(streamingServerURL, stream, seriesInfo) {
})
:
[];
createTorrent(streamingServerURL, parsedMagnetURI.infoHash, null, sources, seriesInfo)
createTorrent(streamingServerURL, parsedMagnetURI.infoHash, null, null, sources, seriesInfo)
.then(function(torrent) {
resolve({ url: torrent.url, infoHash: torrent.infoHash, fileIdx: torrent.fileIdx });
resolve({ url: torrent.url, infoHash: torrent.infoHash, fileIdx: torrent.fileIdx, fileMustInclude: torrent.fileMustInclude });
})
.catch(function(error) {
reject(error);
Expand All @@ -37,9 +39,33 @@ function convertStream(streamingServerURL, stream, seriesInfo) {
}

if (typeof stream.infoHash === 'string') {
createTorrent(streamingServerURL, stream.infoHash, stream.fileIdx, stream.announce, seriesInfo)
createTorrent(streamingServerURL, stream.infoHash, stream.fileIdx, stream.fileMustInclude, stream.announce, seriesInfo)
.then(function(torrent) {
resolve({ url: torrent.url, infoHash: torrent.infoHash, fileIdx: torrent.fileIdx });
resolve({ url: torrent.url, infoHash: torrent.infoHash, fileIdx: torrent.fileIdx, fileMustInclude: torrent.fileMustInclude });
})
.catch(function(error) {
reject(error);
});

return;
}

if (stream.rarUrls && Array.isArray(stream.rarUrls)) {
createRar(streamingServerURL, stream.rarUrls, stream.fileIdx, stream.fileMustInclude)
.then(function(rarStream) {
resolve({ url: rarStream.url, fileIdx: rarStream.fileIdx, fileMustInclude: rarStream.fileMustInclude });
})
.catch(function(error) {
reject(error);
});

return;
}

if (stream.zipUrls && Array.isArray(stream.zipUrls)) {
createZip(streamingServerURL, stream.zipUrls, stream.fileIdx, stream.fileMustInclude)
.then(function(zipStream) {
resolve({ url: zipStream.url, fileIdx: zipStream.fileIdx, fileMustInclude: zipStream.fileMustInclude });
})
.catch(function(error) {
reject(error);
Expand Down
43 changes: 43 additions & 0 deletions src/withStreamingServer/createRar.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
var url = require('url');

function buildRarStream(streamingServerURL, key, fileIdx, fileMustInclude) {
var opts = {};
if (fileIdx && typeof fileIdx === 'number') {
opts.fileIdx = fileIdx;
}
if (fileMustInclude && Array.isArray(fileMustInclude)) {
opts.fileMustInclude = fileMustInclude;
}
return {
url: url.resolve(streamingServerURL, '/rar/stream?key=' + encodeURIComponent(key) + (Object.keys(opts).length ? '&o=' + encodeURIComponent(JSON.stringify(opts)) : '')),
fileIdx: fileIdx,
fileMustInclude: fileMustInclude
};
}

function createRar(streamingServerURL, rarUrls, fileIdx, fileMustInclude) {
if (!(rarUrls || []).length) {
return Promise.reject('No RAR URLs provided');
}

return fetch(url.resolve(streamingServerURL, '/rar/create'), {
method: 'POST',
headers: {
'content-type': 'application/json'
},
body: JSON.stringify(rarUrls)
}).then(function(resp) {
if (resp.ok) {
return resp.json();
}

throw new Error(resp.status + ' (' + resp.statusText + ')');
}).then(function(resp) {
if (!resp.key) {
throw new Error('Could not retrieve RAR stream key');
}
return buildRarStream(streamingServerURL, resp.key, fileIdx, fileMustInclude);
});
}

module.exports = createRar;
23 changes: 17 additions & 6 deletions src/withStreamingServer/createTorrent.js
Original file line number Diff line number Diff line change
@@ -1,23 +1,30 @@
var url = require('url');

function buildTorrent(streamingServerURL, infoHash, fileIdx, sources) {
function buildTorrent(streamingServerURL, infoHash, fileIdx, fileMustInclude, sources) {
var query = Array.isArray(sources) && sources.length > 0 ?
'?' + new URLSearchParams(sources.map(function(source) {
return ['tr', source];
}))
:
'';
query = query + (Array.isArray(fileMustInclude) && fileMustInclude.length > 0 ?
(query ? '&' : '?') + new URLSearchParams(fileMustInclude.map(function(mustInclude) {
return ['f', mustInclude];
}))
:
'');
return {
url: url.resolve(streamingServerURL, '/' + encodeURIComponent(infoHash) + '/' + encodeURIComponent(fileIdx)) + query,
url: url.resolve(streamingServerURL, '/' + encodeURIComponent(infoHash) + '/' + encodeURIComponent(fileIdx || -1)) + query,
infoHash: infoHash,
fileIdx: fileIdx,
fileMustInclude: fileMustInclude,
sources: sources
};
}

function createTorrent(streamingServerURL, infoHash, fileIdx, sources, seriesInfo) {
if ((!Array.isArray(sources) || sources.length === 0) && (fileIdx !== null && isFinite(fileIdx))) {
return Promise.resolve(buildTorrent(streamingServerURL, infoHash, fileIdx, sources));
function createTorrent(streamingServerURL, infoHash, fileIdx, fileMustInclude, sources, seriesInfo) {
if ((!Array.isArray(sources) || sources.length === 0) && ((fileIdx !== null && isFinite(fileIdx)) || (fileMustInclude || []).length)) {
return Promise.resolve(buildTorrent(streamingServerURL, infoHash, fileIdx, fileMustInclude, sources));
}

var body = {
Expand All @@ -36,6 +43,10 @@ function createTorrent(streamingServerURL, infoHash, fileIdx, sources, seriesInf
};
}

if (fileMustInclude && Array.isArray(fileMustInclude) && fileMustInclude.length) {
body.fileMustInclude = fileMustInclude;
}

if (fileIdx === null || !isFinite(fileIdx)) {
body.guessFileIdx = {};
if (seriesInfo) {
Expand Down Expand Up @@ -63,7 +74,7 @@ function createTorrent(streamingServerURL, infoHash, fileIdx, sources, seriesInf

throw new Error(resp.status + ' (' + resp.statusText + ')');
}).then(function(resp) {
return buildTorrent(streamingServerURL, infoHash, body.guessFileIdx ? resp.guessedFileIdx : fileIdx, body.peerSearch ? body.peerSearch.sources : []);
return buildTorrent(streamingServerURL, infoHash, body.guessFileIdx ? resp.guessedFileIdx : fileIdx, fileMustInclude, body.peerSearch ? body.peerSearch.sources : []);
});
}

Expand Down
43 changes: 43 additions & 0 deletions src/withStreamingServer/createZip.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
var url = require('url');

function buildZipStream(streamingServerURL, key, fileIdx, fileMustInclude) {
var opts = {};
if (fileIdx && typeof fileIdx === 'number') {
opts.fileIdx = fileIdx;
}
if (fileMustInclude && Array.isArray(fileMustInclude)) {
opts.fileMustInclude = fileMustInclude;
}
return {
url: url.resolve(streamingServerURL, '/zip/stream?key=' + encodeURIComponent(key) + (Object.keys(opts).length ? '&o=' + encodeURIComponent(JSON.stringify(opts)) : '')),
fileIdx: fileIdx,
fileMustInclude: fileMustInclude
};
}

function createZip(streamingServerURL, zipUrls, fileIdx, fileMustInclude) {
if (!(zipUrls || []).length) {
return Promise.reject('No ZIP URLs provided');
}

return fetch(url.resolve(streamingServerURL, '/zip/create'), {
method: 'POST',
headers: {
'content-type': 'application/json'
},
body: JSON.stringify(zipUrls)
}).then(function(resp) {
if (resp.ok) {
return resp.json();
}

throw new Error(resp.status + ' (' + resp.statusText + ')');
}).then(function(resp) {
if (!resp.key) {
throw new Error('Could not retrieve ZIP stream key');
}
return buildZipStream(streamingServerURL, resp.key, fileIdx, fileMustInclude);
});
}

module.exports = createZip;
Loading