mirror of
https://github.com/knightcrawler-stremio/knightcrawler.git
synced 2024-12-20 03:29:51 +00:00
adds addon module
This commit is contained in:
52
addon/lib/cache.js
Normal file
52
addon/lib/cache.js
Normal file
@@ -0,0 +1,52 @@
|
||||
const cacheManager = require('cache-manager');
|
||||
const mangodbStore = require('cache-manager-mongodb');
|
||||
|
||||
const GLOBAL_KEY_PREFIX = 'torrentio-addon';
|
||||
const STREAM_KEY_PREFIX = `${GLOBAL_KEY_PREFIX}|stream`;
|
||||
|
||||
const STREAM_TTL = process.env.STREAM_TTL || 24 * 60 * 60; // 24 hours
|
||||
const STREAM_EMPTY_TTL = process.env.STREAM_EMPTY_TTL || 30 * 60; // 30 minutes
|
||||
// When the streams are empty we want to cache it for less time in case of timeouts or failures
|
||||
|
||||
const MONGO_URI = process.env.MONGODB_URI;
|
||||
const NO_CACHE = process.env.NO_CACHE || false;
|
||||
|
||||
const cache = initiateCache();
|
||||
|
||||
function initiateCache() {
|
||||
if (NO_CACHE) {
|
||||
return null;
|
||||
} else if (MONGO_URI) {
|
||||
return cacheManager.caching({
|
||||
store: mangodbStore,
|
||||
uri: MONGO_URI,
|
||||
options: {
|
||||
collection: 'torrentio-addon',
|
||||
ttl: STREAM_TTL
|
||||
},
|
||||
ttl: STREAM_TTL,
|
||||
ignoreCacheErrors: true
|
||||
});
|
||||
} else {
|
||||
return cacheManager.caching({
|
||||
store: 'memory',
|
||||
ttl: STREAM_TTL
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
function cacheWrap(key, method, options) {
|
||||
if (NO_CACHE || !cache) {
|
||||
return method();
|
||||
}
|
||||
return cache.wrap(key, method, options);
|
||||
}
|
||||
|
||||
function cacheWrapStream(id, method) {
|
||||
return cacheWrap(`${STREAM_KEY_PREFIX}:${id}`, method, {
|
||||
ttl: (streams) => streams.length ? STREAM_TTL : STREAM_EMPTY_TTL
|
||||
});
|
||||
}
|
||||
|
||||
module.exports = { cacheWrapStream };
|
||||
|
||||
84
addon/lib/repository.js
Normal file
84
addon/lib/repository.js
Normal file
@@ -0,0 +1,84 @@
|
||||
const { Sequelize } = require('sequelize');
|
||||
const Op = Sequelize.Op;
|
||||
|
||||
const DATABASE_URI = process.env.DATABASE_URI;
|
||||
|
||||
const database = new Sequelize(DATABASE_URI, { logging: false });
|
||||
|
||||
const Torrent = database.define('torrent',
|
||||
{
|
||||
infoHash: { type: Sequelize.STRING(64), primaryKey: true },
|
||||
provider: { type: Sequelize.STRING(32), allowNull: false },
|
||||
torrentId: { type: Sequelize.STRING(128) },
|
||||
title: { type: Sequelize.STRING(256), allowNull: false },
|
||||
size: { type: Sequelize.BIGINT },
|
||||
type: { type: Sequelize.STRING(16), allowNull: false },
|
||||
uploadDate: { type: Sequelize.DATE, allowNull: false },
|
||||
seeders: { type: Sequelize.SMALLINT },
|
||||
trackers: { type: Sequelize.STRING(4096) }
|
||||
}
|
||||
);
|
||||
|
||||
const File = database.define('file',
|
||||
{
|
||||
id: { type: Sequelize.BIGINT, autoIncrement: true, primaryKey: true },
|
||||
infoHash: {
|
||||
type: Sequelize.STRING(64),
|
||||
allowNull: false,
|
||||
references: { model: Torrent, key: 'infoHash' },
|
||||
onDelete: 'CASCADE'
|
||||
},
|
||||
fileIndex: { type: Sequelize.INTEGER },
|
||||
title: { type: Sequelize.STRING(256), allowNull: false },
|
||||
size: { type: Sequelize.BIGINT },
|
||||
imdbId: { type: Sequelize.STRING(32) },
|
||||
imdbSeason: { type: Sequelize.INTEGER },
|
||||
imdbEpisode: { type: Sequelize.INTEGER },
|
||||
kitsuId: { type: Sequelize.INTEGER },
|
||||
kitsuEpisode: { type: Sequelize.INTEGER }
|
||||
},
|
||||
);
|
||||
|
||||
Torrent.hasMany(File, { foreignKey: 'infoHash', constraints: false });
|
||||
File.belongsTo(Torrent, { constraints: false });
|
||||
|
||||
function getImdbIdMovieEntries(imdbId) {
|
||||
return File.findAll({
|
||||
where: {
|
||||
imdbId: { [Op.eq]: imdbId }
|
||||
},
|
||||
include: [Torrent]
|
||||
});
|
||||
}
|
||||
|
||||
function getImdbIdSeriesEntries(imdbId, season, episode) {
|
||||
return File.findAll({
|
||||
where: {
|
||||
imdbId: { [Op.eq]: imdbId },
|
||||
imdbSeason: { [Op.eq]: season },
|
||||
imdbEpisode: { [Op.eq]: episode }
|
||||
},
|
||||
include: [Torrent]
|
||||
});
|
||||
}
|
||||
|
||||
function getKitsuIdMovieEntries(kitsuId) {
|
||||
return File.findAll({
|
||||
where: {
|
||||
kitsuId: { [Op.eq]: kitsuId }
|
||||
},
|
||||
include: [Torrent]
|
||||
});
|
||||
}
|
||||
|
||||
function getKitsuIdSeriesEntries(kitsuId, episode) {
|
||||
return File.findAll({
|
||||
where: {
|
||||
kitsuId: { [Op.eq]: kitsuId },
|
||||
kitsuEpisode: { [Op.eq]: episode }
|
||||
},
|
||||
include: [Torrent]
|
||||
});
|
||||
}
|
||||
|
||||
module.exports = { getImdbIdMovieEntries, getImdbIdSeriesEntries, getKitsuIdMovieEntries, getKitsuIdSeriesEntries };
|
||||
Reference in New Issue
Block a user