mirror of
https://github.com/knightcrawler-stremio/knightcrawler.git
synced 2024-12-20 03:29:51 +00:00
[addon] adds sorting by size options
This commit is contained in:
@@ -183,12 +183,12 @@ button:active {
|
|||||||
}
|
}
|
||||||
`;
|
`;
|
||||||
const { Providers } = require('./manifest');
|
const { Providers } = require('./manifest');
|
||||||
const { SortType } = require('./sort');
|
const { SortOptions } = require('./sort');
|
||||||
|
|
||||||
function landingTemplate(manifest, config = {}) {
|
function landingTemplate(manifest, config = {}) {
|
||||||
const providers = config.providers || [];
|
const providers = config.providers || [];
|
||||||
const realDebridApiKey = config.realdebrid || '';
|
const realDebridApiKey = config.realdebrid || '';
|
||||||
const sort = config.sort === SortType.SEEDERS ? SortType.SEEDERS : SortType.QUALITY;
|
const sort = config.sort || SortOptions.options.qualitySeeders.key;
|
||||||
const limit = config.limit || '';
|
const limit = config.limit || '';
|
||||||
const background = manifest.background || 'https://dl.strem.io/addon-background.jpg';
|
const background = manifest.background || 'https://dl.strem.io/addon-background.jpg';
|
||||||
const logo = manifest.logo || 'https://dl.strem.io/addon-logo.png';
|
const logo = manifest.logo || 'https://dl.strem.io/addon-logo.png';
|
||||||
@@ -200,6 +200,9 @@ function landingTemplate(manifest, config = {}) {
|
|||||||
const providersHTML = Providers
|
const providersHTML = Providers
|
||||||
.map(provider => `<option value="${provider.toLowerCase()}">${provider}</option>`)
|
.map(provider => `<option value="${provider.toLowerCase()}">${provider}</option>`)
|
||||||
.join('\n');
|
.join('\n');
|
||||||
|
const sortOptionsHTML = Object.values(SortOptions.options)
|
||||||
|
.map((option, i) => `<option value="${option.key}" ${i === 0 ? 'selected' : ''}>${option.description}</option>`)
|
||||||
|
.join('\n');
|
||||||
const stylizedTypes = manifest.types
|
const stylizedTypes = manifest.types
|
||||||
.map(t => t[0].toUpperCase() + t.slice(1) + (t !== 'series' ? 's' : ''));
|
.map(t => t[0].toUpperCase() + t.slice(1) + (t !== 'series' ? 's' : ''));
|
||||||
|
|
||||||
@@ -245,8 +248,7 @@ function landingTemplate(manifest, config = {}) {
|
|||||||
|
|
||||||
<label class="label" for="iSort">Sorting:</label>
|
<label class="label" for="iSort">Sorting:</label>
|
||||||
<select id="iSort" class="input" onchange="sortModeChange()">
|
<select id="iSort" class="input" onchange="sortModeChange()">
|
||||||
<option value="${SortType.QUALITY}" selected>Quality</option>
|
${sortOptionsHTML}
|
||||||
<option value="${SortType.SEEDERS}">Seeders</option>
|
|
||||||
</select>
|
</select>
|
||||||
|
|
||||||
<label class="label" id="iLimitLabel" for="iLimit">Max results per quality:</label>
|
<label class="label" id="iLimitLabel" for="iLimit">Max results per quality:</label>
|
||||||
@@ -276,7 +278,7 @@ function landingTemplate(manifest, config = {}) {
|
|||||||
});
|
});
|
||||||
|
|
||||||
function sortModeChange() {
|
function sortModeChange() {
|
||||||
if ($('#iSort').val() === '${SortType.SEEDERS}') {
|
if (['${SortOptions.options.qualitySeeders.key}', '${SortOptions.options.qualitySize.key}'].includes($('#iSort').val())) {
|
||||||
$("#iLimitLabel").text("Max results:");
|
$("#iLimitLabel").text("Max results:");
|
||||||
} else {
|
} else {
|
||||||
$("#iLimitLabel").text("Max results per quality:");
|
$("#iLimitLabel").text("Max results per quality:");
|
||||||
@@ -292,7 +294,7 @@ function landingTemplate(manifest, config = {}) {
|
|||||||
|
|
||||||
const providers = providersValue && providersValue.length ? 'providers=' + providersValue : '';
|
const providers = providersValue && providersValue.length ? 'providers=' + providersValue : '';
|
||||||
const realDebrid = realDebridValue && realDebridValue.length ? 'realdebrid=' + realDebridValue : '';
|
const realDebrid = realDebridValue && realDebridValue.length ? 'realdebrid=' + realDebridValue : '';
|
||||||
const sort = sortValue === '${SortType.SEEDERS}' ? 'sort=' + sortValue : '';
|
const sort = sortValue !== '${SortOptions.options.qualitySeeders.key}' ? 'sort=' + sortValue : '';
|
||||||
const limit = /^[1-9][0-9]*$/.test(limitValue) ? 'limit=' + limitValue : '';
|
const limit = /^[1-9][0-9]*$/.test(limitValue) ? 'limit=' + limitValue : '';
|
||||||
|
|
||||||
const configurationValue = [providers, sort, limit, realDebrid].filter(value => value.length).join('|');
|
const configurationValue = [providers, sort, limit, realDebrid].filter(value => value.length).join('|');
|
||||||
|
|||||||
@@ -3,34 +3,67 @@ const SEEDED_SEEDERS = 1;
|
|||||||
const MIN_HEALTHY_COUNT = 10;
|
const MIN_HEALTHY_COUNT = 10;
|
||||||
const MAX_UNHEALTHY_COUNT = 5;
|
const MAX_UNHEALTHY_COUNT = 5;
|
||||||
|
|
||||||
const SortType = {
|
const SortOptions = {
|
||||||
QUALITY: 'quality',
|
key: 'sort',
|
||||||
SEEDERS: 'seeders',
|
options: {
|
||||||
};
|
qualitySeeders: {
|
||||||
|
key: 'quality',
|
||||||
|
description: 'By quality then seeders'
|
||||||
|
},
|
||||||
|
qualitySize: {
|
||||||
|
key: 'qualitysize',
|
||||||
|
description: 'By quality then size'
|
||||||
|
},
|
||||||
|
seeders: {
|
||||||
|
key: 'seeders',
|
||||||
|
description: 'By seeders'
|
||||||
|
},
|
||||||
|
size: {
|
||||||
|
key: 'size',
|
||||||
|
description: 'By size'
|
||||||
|
},
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
function sortStreams(streams, config) {
|
function sortStreams(streams, config) {
|
||||||
const sort = config.sort && config.sort.toLowerCase() || undefined;
|
const sort = config.sort && config.sort.toLowerCase() || undefined;
|
||||||
const limit = /^[1-9][0-9]*$/.test(config.limit) && parseInt(config.limit) || undefined;
|
const limit = /^[1-9][0-9]*$/.test(config.limit) && parseInt(config.limit) || undefined;
|
||||||
if (sort === SortType.SEEDERS) {
|
if (sort === SortOptions.options.seeders.key) {
|
||||||
return sortBySeeders(streams).slice(0, limit)
|
return sortBySeeders(streams, limit);
|
||||||
|
} else if (sort === SortOptions.options.size.key) {
|
||||||
|
return sortBySize(streams, limit);
|
||||||
}
|
}
|
||||||
return sortByVideoQuality(streams, limit)
|
const nestedSort = sort === SortOptions.options.qualitySize.key ? sortBySize : noopSort;
|
||||||
|
return sortByVideoQuality(streams, nestedSort, limit)
|
||||||
}
|
}
|
||||||
|
|
||||||
function sortBySeeders(streams) {
|
function noopSort(streams) {
|
||||||
|
return streams;
|
||||||
|
}
|
||||||
|
|
||||||
|
function sortBySeeders(streams, limit) {
|
||||||
// streams are already presorted by seeders and upload date
|
// streams are already presorted by seeders and upload date
|
||||||
const healthy = streams.filter(stream => extractSeeders(stream.title) >= HEALTHY_SEEDERS);
|
const healthy = streams.filter(stream => extractSeeders(stream.title) >= HEALTHY_SEEDERS);
|
||||||
const seeded = streams.filter(stream => extractSeeders(stream.title) >= SEEDED_SEEDERS);
|
const seeded = streams.filter(stream => extractSeeders(stream.title) >= SEEDED_SEEDERS);
|
||||||
|
|
||||||
if (healthy.length >= MIN_HEALTHY_COUNT) {
|
if (healthy.length >= MIN_HEALTHY_COUNT) {
|
||||||
return healthy;
|
return healthy.slice(0, limit);
|
||||||
} else if (seeded.length >= MAX_UNHEALTHY_COUNT) {
|
} else if (seeded.length >= MAX_UNHEALTHY_COUNT) {
|
||||||
return seeded.slice(0, MIN_HEALTHY_COUNT);
|
return seeded.slice(0, MIN_HEALTHY_COUNT).slice(0, limit);
|
||||||
}
|
}
|
||||||
return streams.slice(0, MAX_UNHEALTHY_COUNT);
|
return streams.slice(0, MAX_UNHEALTHY_COUNT).slice(0, limit);
|
||||||
}
|
}
|
||||||
|
|
||||||
function sortByVideoQuality(streams, limit) {
|
function sortBySize(streams, limit) {
|
||||||
|
return streams
|
||||||
|
.sort((a, b) => {
|
||||||
|
const aSize = extractSize(a.title);
|
||||||
|
const bSize = extractSize(b.title);
|
||||||
|
return bSize - aSize;
|
||||||
|
}).slice(0, limit);
|
||||||
|
}
|
||||||
|
|
||||||
|
function sortByVideoQuality(streams, nestedSort, limit) {
|
||||||
const qualityMap = sortBySeeders(streams)
|
const qualityMap = sortBySeeders(streams)
|
||||||
.reduce((map, stream) => {
|
.reduce((map, stream) => {
|
||||||
const quality = extractQuality(stream.name);
|
const quality = extractQuality(stream.name);
|
||||||
@@ -51,7 +84,7 @@ function sortByVideoQuality(streams, limit) {
|
|||||||
return a < b ? -1 : b < a ? 1 : 0; // otherwise sort by alphabetic order
|
return a < b ? -1 : b < a ? 1 : 0; // otherwise sort by alphabetic order
|
||||||
});
|
});
|
||||||
return sortedQualities
|
return sortedQualities
|
||||||
.map(quality => qualityMap[quality].slice(0, limit))
|
.map(quality => nestedSort(qualityMap[quality]).slice(0, limit))
|
||||||
.reduce((a, b) => a.concat(b), []);
|
.reduce((a, b) => a.concat(b), []);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -73,5 +106,27 @@ function extractSeeders(title) {
|
|||||||
return seedersMatch && parseInt(seedersMatch[1]) || 0;
|
return seedersMatch && parseInt(seedersMatch[1]) || 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function extractSize(title) {
|
||||||
|
const seedersMatch = title.match(/💾 ([\d.]+ \w+)/);
|
||||||
|
return seedersMatch && parseSize(seedersMatch[1]) || 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
function parseSize(sizeText) {
|
||||||
|
if (!sizeText) {
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
let scale = 1;
|
||||||
|
if (sizeText.includes('TB')) {
|
||||||
|
scale = 1024 * 1024 * 1024 * 1024
|
||||||
|
} else if (sizeText.includes('GB')) {
|
||||||
|
scale = 1024 * 1024 * 1024
|
||||||
|
} else if (sizeText.includes('MB')) {
|
||||||
|
scale = 1024 * 1024;
|
||||||
|
} else if (sizeText.includes('kB')) {
|
||||||
|
scale = 1024;
|
||||||
|
}
|
||||||
|
return Math.floor(parseFloat(sizeText.replace(/,/g, '')) * scale);
|
||||||
|
}
|
||||||
|
|
||||||
module.exports = sortStreams;
|
module.exports = sortStreams;
|
||||||
module.exports.SortType = SortType;
|
module.exports.SortOptions = SortOptions;
|
||||||
Reference in New Issue
Block a user