[addon] cache presorted streams without additional info to reduce byte size

This commit is contained in:
TheBeastLT
2020-03-20 17:16:13 +01:00
parent c164000bc2
commit 701a782986
3 changed files with 44 additions and 49 deletions

View File

@@ -1,9 +1,10 @@
const { addonBuilder } = require('stremio-addon-sdk');
const { Type } = require('./lib/types');
const { manifest } = require('./lib/manifest');
const { cacheWrapStream } = require('./lib/cache');
const { toStreamInfo, sanitizeStreamInfo } = require('./lib/streamInfo');
const { toStreamInfo } = require('./lib/streamInfo');
const repository = require('./lib/repository');
const applyStreamSorting = require('./lib/sort');
const applySorting = require('./lib/sort');
const applyMochs = require('./moch/moch');
const CACHE_MAX_AGE = process.env.CACHE_MAX_AGE || 4 * 60 * 60; // 4 hours in seconds
@@ -18,17 +19,13 @@ builder.defineStreamHandler((args) => {
return Promise.resolve({ streams: [] });
}
const handlers = {
series: () => seriesRecordsHandler(args).then(records => records.map(record => toStreamInfo(record))),
movie: () => movieRecordsHandler(args).then(records => records.map(record => toStreamInfo(record))),
fallback: () => Promise.reject('not supported type')
};
return cacheWrapStream(args.id, (handlers[args.type] || handlers.fallback))
.then(streams => filterStreamByProvider(streams, args.extra.providers))
.then(streams => applyStreamSorting(streams, args.extra))
return cacheWrapStream(args.id, () => streamHandler(args)
.then(records => records
.sort((a, b) => b.torrent.seeders - a.torrent.seeders || b.torrent.uploadDate - a.torrent.uploadDate)
.map(record => toStreamInfo(record))))
.then(streams => filterByProvider(streams, args.extra.providers))
.then(streams => applySorting(streams, args.extra))
.then(streams => applyMochs(streams, args.extra))
.then(streams => streams.map(stream => sanitizeStreamInfo(stream)))
.then(streams => ({
streams: streams,
cacheMaxAge: streams.length ? CACHE_MAX_AGE : CACHE_MAX_AGE_EMPTY,
@@ -41,6 +38,15 @@ builder.defineStreamHandler((args) => {
});
});
async function streamHandler(args) {
if (args.type === Type.MOVIE) {
return movieRecordsHandler(args);
} else if (args.type === Type.SERIES) {
return seriesRecordsHandler(args);
}
return Promise.reject('not supported type');
}
async function seriesRecordsHandler(args) {
if (args.id.match(/tt\d+/)) {
const parts = args.id.split(':');
@@ -66,7 +72,7 @@ async function movieRecordsHandler(args) {
return Promise.reject(`Unsupported id type: ${args.id}`);
}
function filterStreamByProvider(streams, providers) {
function filterByProvider(streams, providers) {
if (!providers || !providers.length) {
return streams;
}

View File

@@ -18,45 +18,54 @@ function sortStreams(streams, config) {
}
function sortBySeeders(streams) {
const sortedStreams = streams
.sort((a, b) => b.filters.seeders - a.filters.seeders || b.filters.uploadDate - a.filters.uploadDate);
const healthy = sortedStreams.filter(stream => stream.filters.seeders >= HEALTHY_SEEDERS);
const seeded = sortedStreams.filter(stream => stream.filters.seeders >= SEEDED_SEEDERS);
// streams are already presorted by seeders and upload date
const healthy = streams.filter(stream => extractSeeders(stream.title) >= HEALTHY_SEEDERS);
const seeded = streams.filter(stream => extractSeeders(stream.title) >= SEEDED_SEEDERS);
if (healthy.length >= MIN_HEALTHY_COUNT) {
return healthy;
} else if (seeded.length >= MAX_UNHEALTHY_COUNT) {
return seeded.slice(0, MIN_HEALTHY_COUNT);
}
return sortedStreams.slice(0, MAX_UNHEALTHY_COUNT);
return streams.slice(0, MAX_UNHEALTHY_COUNT);
}
function sortByVideoQuality(streams, limit) {
const qualityMap = sortBySeeders(streams)
.reduce((map, stream) => {
const quality = stream.filters.quality;
const quality = extractQuality(stream.title);
map[quality] = (map[quality] || []).concat(stream);
return map;
}, {});
const sortedQualities = Object.keys(qualityMap)
.sort((a, b) => {
const aQuality = a === '4k' ? '2160p' : a;
const bQuality = b === '4k' ? '2160p' : b;
const aResolution = aQuality && aQuality.match(/\d+p/) && parseInt(aQuality, 10);
const bResolution = bQuality && bQuality.match(/\d+p/) && parseInt(bQuality, 10);
const aResolution = a && a.match(/\d+p/) && parseInt(a, 10);
const bResolution = b && b.match(/\d+p/) && parseInt(b, 10);
if (aResolution && bResolution) {
return bResolution - aResolution; // higher resolution first;
} else if (aResolution) {
return -1;
return -1; // remain higher if resolution is there
} else if (bResolution) {
return 1;
return 1; // move downward if other stream has resolution
}
return a < b ? -1 : b < a ? 1 : 0;
return a < b ? -1 : b < a ? 1 : 0; // otherwise sort by alphabetic order
});
return sortedQualities
.map(quality => qualityMap[quality].slice(0, limit))
.reduce((a, b) => a.concat(b), []);
}
function extractQuality(title) {
const qualityMatch = title.match(/📺 (.*)/);
const qualityDesc = qualityMatch && qualityMatch[1];
const resolutionMatch = qualityDesc && qualityDesc.match(/\d+p/);
return resolutionMatch && resolutionMatch[0] || qualityDesc;
}
function extractSeeders(title) {
const seedersMatch = title.match(/👤 (\d+)/);
return seedersMatch && parseInt(seedersMatch[1]) || 0;
}
module.exports = sortStreams;
module.exports.SortType = SortType;

View File

@@ -10,16 +10,6 @@ function toStreamInfo(record) {
return seriesStream(record);
}
function sanitizeStreamInfo(stream) {
if (stream.filters) {
delete stream.filters;
}
if (stream.fileIdx === undefined || stream.fileIdx === null) {
delete stream.fileIdx;
}
return stream;
}
function movieStream(record) {
const titleInfo = titleParser.parse(record.title);
const sameInfo = record.title === record.torrent.title;
@@ -38,12 +28,7 @@ function movieStream(record) {
name: `${ADDON_NAME}\n${record.torrent.provider}`,
title: title,
infoHash: record.infoHash,
fileIdx: record.fileIndex,
filters: {
quality: titleInfo.resolution || record.torrent.resolution || titleInfo.source,
seeders: record.torrent.seeders,
uploadDate: new Date(record.torrent.uploadDate)
}
fileIdx: record.fileIndex
};
}
@@ -70,12 +55,7 @@ function seriesStream(record) {
name: `${ADDON_NAME}\n${record.torrent.provider}`,
title: title,
infoHash: record.infoHash,
fileIdx: record.fileIndex,
filters: {
quality: tInfo.resolution || eInfo.resolution || record.torrent.resolution || tInfo.source || eInfo.source,
seeders: record.torrent.seeders,
uploadDate: new Date(record.torrent.uploadDate)
}
fileIdx: record.fileIndex
};
}
@@ -85,4 +65,4 @@ function joinDetailParts(parts, prefix = '', delimiter = ' ') {
return filtered.length > 0 ? `${prefix}${filtered}` : undefined;
}
module.exports = { toStreamInfo, sanitizeStreamInfo };
module.exports = { toStreamInfo };