Skip to content

Commit

Permalink
language selector for movies
Browse files Browse the repository at this point in the history
  • Loading branch information
ivan1986 committed Jul 11, 2021
1 parent d2be9f4 commit f4ea638
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 34 deletions.
5 changes: 2 additions & 3 deletions src/app/butter-provider/movie.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,6 @@ class MovieApi extends Generic {
if (movie.torrents) {
const curLang = movie.torrents[this.contentLanguage]
? this.contentLanguage : Object.keys(movie.torrents)[0];
let langs = {};
langs[curLang] = movie.torrents[curLang];
results.push({
type: 'movie',
imdb_id: movie.imdb_id,
Expand All @@ -38,7 +36,8 @@ class MovieApi extends Generic {
trailer: movie.trailer !== null ? movie.trailer : false,
certification: movie.certification,
torrents: movie.torrents[curLang],
langs: langs,
langs: movie.torrents,
defaultAudio: curLang,
locale: movie.locale || null,
});
}
Expand Down
27 changes: 15 additions & 12 deletions src/app/butter-provider/yts.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,18 @@ class YTSApi extends Generic {
if (movies) {
movies.forEach(movie => {
if (movie.torrents) {
let torrents = movie.torrents.reduceRight(function (torrents, torrent) {
torrents[torrent.quality] = {
url: torrent.url,
magnet: `magnet:?xt=urn:btih:${torrent.hash}`,
size: torrent.size_bytes,
filesize: torrent.size,
seed: torrent.seeds,
peer: torrent.peers
};
return torrents;
}, {});
let curLang = movie.language.replace('cn', 'zh-cn');
results.push({
type: 'movie',
imdb_id: movie.imdb_code,
Expand All @@ -31,18 +43,9 @@ class YTSApi extends Generic {
synopsis: movie.description_full,
trailer: 'https://www.youtube.com/watch?v=' + movie.yt_trailer_code || false,
certification: movie.mpa_rating,
torrents: movie.torrents.reduceRight(function (torrents, torrent) {
torrents[torrent.quality] = {
url: torrent.url,
magnet: `magnet:?xt=urn:btih:${torrent.hash}`,
size: torrent.size_bytes,
filesize: torrent.size,
seed: torrent.seeds,
peer: torrent.peers
};
return torrents;
}, {}),
langs: {[movie.language.replace('cn', 'zh-cn')]: {}}
torrents: torrents,
defaultAudio: curLang,
langs: {[curLang]: torrents}
});
}
});
Expand Down
9 changes: 8 additions & 1 deletion src/app/lib/views/play_control.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,9 @@
this.model.get('title')
);
if (!this.model.get('langs')) {
this.model.set('langs', { en: undefined });
this.model.set('langs', { en: this.model.get('torrents') });
} else {
this.model.set('torrents', this.model.get('langs')[this.model.get('defaultAudio')]);
}

App.vent.on('sub:lang', this.switchSubtitle.bind(this));
Expand Down Expand Up @@ -211,6 +213,11 @@
$('#audio-dropdown .flag.toggle').attr('title', App.Localization.nativeName(lang) + '<br>(' + App.Localization.name(lang).replace(/\(|\)/g, '') + ')').tooltip({delay: {show: 800, hide: 100}, html: true}).tooltip('fixTitle');
}
console.info('Audios: ' + lang);

if (this.getRegion('qualitySelector').currentView) {
this.model.set('torrents', audios[lang]);
this.getRegion('qualitySelector').currentView.updateTorrents(audios[lang]);
}
},

downloadTorrent: function() {
Expand Down
42 changes: 24 additions & 18 deletions src/app/lib/views/quality-selector.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,30 @@
},
collator: new Intl.Collator(undefined, {numeric: true, sensitivity: 'base'}),
initialize: function () {
var self = this;
this.updateTorrents(this.model.get('torrents'));
},

onAttach: function () {
this.initQuality();
},

var required = this.model.get('required');
var torrents = this.model.get('torrents');
initQuality: function() {
var selectedKey = null;
for (let [key, torrent] of Object.entries(this.model.get('sortedTorrents'))) {
if (!torrent) {
continue;
}
if (!selectedKey || this.collator.compare(key, Settings[this.model.get('defaultQualityKey')]) <= 0) {
selectedKey = key;
}
}
this.selectQuality(selectedKey);
},

updateTorrents: function (torrents) {
let keys = Object.keys(torrents).sort(this.collator.compare);
let sortedTorrents = {};
for (let key of required) {
for (let key of this.model.get('required')) {
sortedTorrents[key] = false;
}
for (let key of keys) {
Expand All @@ -29,20 +45,10 @@
sortedTorrents[key] = torrents[key];
}

console.log(sortedTorrents);
this.model.set('sortedTorrents', sortedTorrents);
},

onAttach: function () {
var selectedKey = null;
for (let [key, torrent] of Object.entries(this.model.get('sortedTorrents'))) {
if (!torrent) {
continue;
}
if (!selectedKey || this.collator.compare(key, Settings[this.model.get('defaultQualityKey')]) <= 0) {
selectedKey = key;
}
}
this.selectQuality(selectedKey);
this.render();
this.initQuality();
},

selectNext: function () {
Expand Down Expand Up @@ -77,7 +83,7 @@
$(this.ui.list).find('div').removeClass('active');
$(this.ui.list).find('div:contains("'+key+'")').addClass('active');
console.log('Select quality: ', key);
var torrents = this.model.get('torrents');
var torrents = this.model.get('sortedTorrents');
var callback = this.model.get('selectCallback');
callback(torrents[key], key);
},
Expand Down

0 comments on commit f4ea638

Please sign in to comment.