mirror of
https://github.com/knightcrawler-stremio/knightcrawler.git
synced 2024-12-20 03:29:51 +00:00
Initial commit
This commit is contained in:
84
lib/repository.js
Normal file
84
lib/repository.js
Normal file
@@ -0,0 +1,84 @@
|
||||
const { Sequelize }= require('sequelize');
|
||||
|
||||
const POSTGRES_URI = process.env.POSTGRES_URI || 'postgres://torrentio:postgres@localhost:5432/torrentio';
|
||||
|
||||
const database = new Sequelize(POSTGRES_URI, { logging: false });
|
||||
|
||||
const Provider = database.define('provider', {
|
||||
name: { type: Sequelize.STRING(16), primaryKey: true},
|
||||
lastScraped: { type: Sequelize.DATE }
|
||||
});
|
||||
|
||||
const Torrent = database.define('torrent', {
|
||||
infoHash: { type: Sequelize.STRING(64), primaryKey: true },
|
||||
provider: { type: Sequelize.STRING(16), allowNull: false },
|
||||
title: { type: Sequelize.STRING(128), allowNull: false },
|
||||
imdbId: { type: Sequelize.STRING(12) },
|
||||
uploadDate: { type: Sequelize.DATE, allowNull: false },
|
||||
seeders: { type: Sequelize.SMALLINT },
|
||||
files: { type: Sequelize.ARRAY(Sequelize.TEXT) },
|
||||
});
|
||||
|
||||
const SkipTorrent = database.define('skip_torrent', {
|
||||
infoHash: {type: Sequelize.STRING(64), primaryKey: true},
|
||||
});
|
||||
|
||||
const FailedImdbTorrent = database.define('failed_imdb_torrent', {
|
||||
infoHash: {type: Sequelize.STRING(64), primaryKey: true},
|
||||
});
|
||||
|
||||
function connect() {
|
||||
return database.sync({ alter: true });
|
||||
}
|
||||
|
||||
function getProvider(provider) {
|
||||
return Provider.findOrCreate({ where: { name: provider.name }, defaults: provider });
|
||||
}
|
||||
|
||||
function updateProvider(provider) {
|
||||
return Provider.update(provider);
|
||||
}
|
||||
|
||||
function getTorrent(torrent) {
|
||||
return Torrent.findByPk(torrent.infoHash)
|
||||
.then((result) =>{
|
||||
if (!result) {
|
||||
throw new Error(`torrent not found: ${torrent.infoHash}`);
|
||||
}
|
||||
return result.dataValues;
|
||||
})
|
||||
}
|
||||
|
||||
function updateTorrent(torrent) {
|
||||
return Torrent.upsert(torrent);
|
||||
}
|
||||
|
||||
function getSkipTorrent(torrent) {
|
||||
return SkipTorrent.findByPk(torrent.infoHash)
|
||||
.then((result) =>{
|
||||
if (!result) {
|
||||
return getFailedImdbTorrent(torrent);
|
||||
}
|
||||
return result.dataValues;
|
||||
})
|
||||
}
|
||||
|
||||
function createSkipTorrent(torrent) {
|
||||
return SkipTorrent.upsert({ infoHash: torrent.infoHash });
|
||||
}
|
||||
|
||||
function getFailedImdbTorrent(torrent) {
|
||||
return FailedImdbTorrent.findByPk(torrent.infoHash)
|
||||
.then((result) =>{
|
||||
if (!result) {
|
||||
throw new Error(`torrent not found: ${torrent.infoHash}`);
|
||||
}
|
||||
return result.dataValues;
|
||||
})
|
||||
}
|
||||
|
||||
function createFailedImdbTorrent(torrent) {
|
||||
return FailedImdbTorrent.upsert({ infoHash: torrent.infoHash });
|
||||
}
|
||||
|
||||
module.exports = { connect, getProvider, updateProvider, getTorrent, updateTorrent, getSkipTorrent, createSkipTorrent, createFailedImdbTorrent };
|
||||
Reference in New Issue
Block a user