Skip to content

Commit

Permalink
Auto select fastest server to download secondary subtitles 🏃
Browse files Browse the repository at this point in the history
  • Loading branch information
dannvix committed Apr 26, 2020
1 parent 778e885 commit a1bd47c
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 14 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "NflxMultiSubs",
"description": "Bilingual Subtitles & Enhanced Experiences for Netflix",
"author": "Dan Chen",
"version": "2.0.1",
"version": "2.1.0",
"license": "MIT",
"private": true,
"scripts": {
Expand Down
45 changes: 32 additions & 13 deletions src/nflxmultisubs.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,12 +50,12 @@ let gRenderOptions = Object.assign({}, kDefaultSettings);
////////////////////////////////////////////////////////////////////////////////

class SubtitleBase {
constructor(lang, bcp47, url) {
constructor(lang, bcp47, urls) {
this.state = 'GENESIS';
this.active = false;
this.lang = lang;
this.bcp47 = bcp47;
this.url = url;
this.urls = urls;
this.extentWidth = undefined;
this.extentHeight = undefined;
this.lines = undefined;
Expand Down Expand Up @@ -104,13 +104,33 @@ class SubtitleBase {
[this.extentWidth, this.extentHeight] = [width, height];
}

_download() {
if (!this.urls) return Promise.resolve();

console.log('Selecting fastest server, candidates: ',
this.urls.map(u => u.substr(0, 24)));

let download_started = false;
return new Promise((resolve, reject) => {
this.urls.forEach(url => {
fetch(new Request(url), {method: 'HEAD'}).then(r => {
if (download_started) return;

download_started = true;
console.log('Fastest: ', url.substr(0, 24));
this._extract(fetch(url)).then(() => resolve());
});
});
});
}

_render(lines, options) {
// implemented in derived class
}

_download() {
_extract(fetchPromise) {
// extract contents downloaded from fetch()
// implemented in derived class
return Promise.resolve();
}
}

Expand All @@ -130,9 +150,9 @@ class TextSubtitle extends SubtitleBase {
super(...args);
}

_download() {
_extract(fetchPromise) {
return new Promise((resolve, reject) => {
fetch(this.url)
fetchPromise
.then(r => r.text())
.then(xmlText => {
const xml = new DOMParser().parseFromString(xmlText, 'text/xml');
Expand Down Expand Up @@ -205,10 +225,9 @@ class ImageSubtitle extends SubtitleBase {
this.zip = undefined;
}

_download() {
_extract(fetchPromise) {
return new Promise((resolve, reject) => {
const fetchP = fetch(this.url).then(r => r.blob());
const unzipP = fetchP.then(zipBlob => new JSZip().loadAsync(zipBlob));
const unzipP = fetchPromise.then(r => r.blob()).then(zipBlob => new JSZip().loadAsync(zipBlob));
unzipP.then(zip => {
zip
.file('manifest_ttml2.xml')
Expand Down Expand Up @@ -335,8 +354,8 @@ class SubtitleFactory {
static _buildImageBased(track, lang, bcp47) {
const maxHeight = Math.max(...Object.values(track.ttDownloadables).map(d => d.height));
const d = Object.values(track.ttDownloadables).find(d => d.height === maxHeight);
const url = Object.values(d.downloadUrls)[0];
return new ImageSubtitle(lang, bcp47, url);
const urls = Object.values(d.downloadUrls);
return new ImageSubtitle(lang, bcp47, urls);
}

static _buildTextBased(track, lang, bcp47) {
Expand All @@ -346,8 +365,8 @@ class SubtitleFactory {
console.error(`Cannot find "${targetProfile}" for ${lang}`);
return null;
}
const url = Object.values(d.downloadUrls)[0];
return new TextSubtitle(lang, bcp47, url);
const urls = Object.values(d.downloadUrls);
return new TextSubtitle(lang, bcp47, urls);
}
}

Expand Down

0 comments on commit a1bd47c

Please sign in to comment.