adds addon module

This commit is contained in:
TheBeastLT
2020-03-10 16:17:24 +01:00
parent 58aba322c2
commit a44826757c
8 changed files with 2169 additions and 0 deletions

29
addon/.eslintrc.json Normal file
View File

@@ -0,0 +1,29 @@
{
"extends": "google",
"env": {
"es6": true,
"node": true
},
"parserOptions": {
"ecmaVersion": 2017
},
"rules": {
"no-console": "off",
"no-use-before-define": "off",
"new-cap": "warn",
"max-len": [2, 120, {
"ignoreComments": true
}],
"object-curly-spacing": ["error", "always"],
"comma-dangle": [
"error",
"only-multiline"
],
"newline-after-var": "off",
"require-jsdoc": "off",
"strict": [
"error",
"never"
]
}
}

55
addon/addon.js Normal file
View File

@@ -0,0 +1,55 @@
const { addonBuilder } = require('stremio-addon-sdk');
const { cacheWrapStream } = require('./lib/cache');
const CACHE_MAX_AGE = process.env.CACHE_MAX_AGE || 24 * 60; // 24 hours in seconds
const CACHE_MAX_AGE_EMPTY = 4 * 60; // 4 hours in seconds
const STALE_REVALIDATE_AGE = 4 * 60; // 4 hours
const STALE_ERROR_AGE = 7 * 24 * 60; // 7 days
const EMPTY_OBJECT = {};
const builder = new addonBuilder({
id: 'com.stremio.torrentio.addon',
version: '1.0.0',
name: 'Torrentio',
description: 'Provides torrent stream from scraped torrent providers. Currently support ThePirateBay, 1337x, RARBG, KickassTorrents, HorribleSubs.',
catalogs: [],
resources: ['stream'],
types: ['movie', 'series'],
idPrefixes: ['tt'],
background: `https://i.imgur.com/t8wVwcg.jpg`,
logo: `https://i.imgur.com/dPa2clS.png`,
});
builder.defineStreamHandler((args) => {
if (!args.id.match(/tt\d+/i)) {
return Promise.resolve({ streams: [] });
}
const handlers = {
series: () => seriesStreamHandler(args),
movie: () => movieStreamHandler(args),
fallback: () => Promise.reject('not supported type')
};
return cacheWrapStream(args.id, handlers[args.type] || handlers.fallback)
.then((streams) => ({
streams: streams,
cacheMaxAge: streams.length ? CACHE_MAX_AGE : CACHE_MAX_AGE_EMPTY,
staleRevalidate: STALE_REVALIDATE_AGE,
staleError: STALE_ERROR_AGE
}))
.catch((error) => {
console.log(`Failed request ${args.id}: ${error}`);
throw error;
});
});
async function seriesStreamHandler(args) {
}
async function movieStreamHandler(args) {
}
module.exports = builder.getInterface();

52
addon/lib/cache.js Normal file
View 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
View 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 };

1871
addon/package-lock.json generated Normal file

File diff suppressed because it is too large Load Diff

24
addon/package.json Normal file
View File

@@ -0,0 +1,24 @@
{
"name": "stremio-torrentio",
"version": "1.0.0",
"main": "addon.js",
"scripts": {
"start": "node index.js",
"deploy": "now && now alias"
},
"author": "TheBeastLT <pauliox@beyond.lt>",
"license": "MIT",
"dependencies": {
"cache-manager": "^2.9.0",
"cache-manager-mongodb": "^0.2.1",
"express-rate-limit": "^5.1.1",
"parse-torrent-title": "git://github.com/TheBeastLT/parse-torrent-title.git#master",
"stremio-addon-sdk": "^1.6.1"
},
"devDependencies": {
"eslint": "^6.4.0",
"eslint-config-eslint": "^5.0.1",
"eslint-config-google": "^0.14.0",
"eslint-plugin-node": "^10.0.0"
}
}

26
addon/serverless.js Normal file
View File

@@ -0,0 +1,26 @@
const rateLimit = require('express-rate-limit');
const { getRouter } = require('stremio-addon-sdk');
const landingTemplate = require('stremio-addon-sdk/src/landingTemplate');
const addonInterface = require('./addon');
const router = getRouter(addonInterface);
const limiter = rateLimit({
windowMs: 10 * 1000, // 10 seconds
max: 10, // limit each IP to 10 requests per windowMs
headers: false
});
router.use(limiter);
router.get('/', (_, res) => {
const landingHTML = landingTemplate(addonInterface.manifest);
res.setHeader('content-type', 'text/html');
res.end(landingHTML);
});
module.exports = function (req, res) {
router(req, res, function () {
res.statusCode = 404;
res.end();
});
};

28
now.json Normal file
View File

@@ -0,0 +1,28 @@
{
"version": 2,
"builds": [
{
"src": "/addon/static/**/*",
"use": "@now/static"
},
{
"src": "/addon/**/*.js",
"use": "@now/node"
}
],
"routes": [
{
"src": "/static/(.*)",
"dest": "/addon/static/$1"
},
{
"src": "/(.*)",
"dest": "/addon/serverless.js"
}
],
"env": {
"MONGODB_URI": "@mongodb-uri",
"DATABASE_URI": "@database-uri"
}
}