Moved contents of seasoned_api up to root folder
This commit is contained in:
@@ -1,14 +1,18 @@
|
||||
{
|
||||
"extends": [
|
||||
"airbnb-base"
|
||||
],
|
||||
"rules": {
|
||||
"indent": ["error", 3],
|
||||
"prefer-destructuring": 0,
|
||||
"camelcase": 0,
|
||||
"import/no-unresolved": 0,
|
||||
"import/no-extraneous-dependencies": 0,
|
||||
"object-shorthand": 0,
|
||||
"comma-dangle": 0
|
||||
}
|
||||
"root": true,
|
||||
"parserOptions": {
|
||||
// "parser": "@typescript-eslint/parser",
|
||||
"sourceType": "module"
|
||||
},
|
||||
// "plugins": ["@typescript-eslint"],
|
||||
"extends": [
|
||||
"eslint-config-airbnb-base",
|
||||
// "plugin:@typescript-eslint/recommended",
|
||||
"plugin:prettier/recommended"
|
||||
],
|
||||
"rules": {
|
||||
"no-underscore-dangle": "off",
|
||||
"no-shadow": "off"
|
||||
// "@typescript-eslint/no-shadow": ["error"]
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,31 +0,0 @@
|
||||
{
|
||||
"database": {
|
||||
"host": "../shows.db"
|
||||
},
|
||||
"redis": {
|
||||
"host": "localhost",
|
||||
"port": 6379
|
||||
},
|
||||
"webserver": {
|
||||
"port": 31459,
|
||||
"origins": []
|
||||
},
|
||||
"tmdb": {
|
||||
"apiKey": ""
|
||||
},
|
||||
"plex": {
|
||||
"ip": "localhost",
|
||||
"token": ""
|
||||
},
|
||||
"tautulli": {
|
||||
"apiKey": "",
|
||||
"ip": "",
|
||||
"port": ""
|
||||
},
|
||||
"raven": {
|
||||
"DSN": ""
|
||||
},
|
||||
"authentication": {
|
||||
"secret": "secret"
|
||||
}
|
||||
}
|
||||
@@ -1,20 +0,0 @@
|
||||
{
|
||||
"database": {
|
||||
"host": ":memory:"
|
||||
},
|
||||
"webserver": {
|
||||
"port": 31400
|
||||
},
|
||||
"tmdb": {
|
||||
"apiKey": "bogus-api-key"
|
||||
},
|
||||
"plex": {
|
||||
"ip": "0.0.0.0"
|
||||
},
|
||||
"raven": {
|
||||
"DSN": ""
|
||||
},
|
||||
"authentication": {
|
||||
"secret": "secret"
|
||||
}
|
||||
}
|
||||
@@ -1,44 +0,0 @@
|
||||
const Plex = require("src/plex/plex");
|
||||
const configuration = require("src/config/configuration").getInstance();
|
||||
const plex = new Plex(configuration.get("plex", "ip"));
|
||||
const establishedDatabase = require("src/database/database");
|
||||
|
||||
const queries = {
|
||||
getRequestsNotYetInPlex: `SELECT * FROM requests WHERE status = 'requested' OR status = 'downloading'`,
|
||||
saveNewStatus: `UPDATE requests SET status = ? WHERE id IS ? and type IS ?`
|
||||
};
|
||||
|
||||
const getByStatus = () =>
|
||||
establishedDatabase.all(queries.getRequestsNotYetInPlex);
|
||||
|
||||
const checkIfRequestExistInPlex = async request => {
|
||||
request.existsInPlex = await plex.existsInPlex(request);
|
||||
return request;
|
||||
};
|
||||
|
||||
const commitNewStatus = (status, id, type, title) => {
|
||||
console.log(type, title, "updated to:", status);
|
||||
return establishedDatabase.run(queries.saveNewStatus, [status, id, type]);
|
||||
};
|
||||
|
||||
const getNewRequestMatchesInPlex = async () => {
|
||||
const requests = await getByStatus();
|
||||
|
||||
return Promise.all(requests.map(checkIfRequestExistInPlex))
|
||||
.catch(error =>
|
||||
console.log("error from checking plex for existance:", error)
|
||||
)
|
||||
.then(matchedRequests =>
|
||||
matchedRequests.filter(request => request.existsInPlex)
|
||||
);
|
||||
};
|
||||
|
||||
const updateMatchInDb = (match, status) => {
|
||||
return commitNewStatus(status, match.id, match.type, match.title);
|
||||
};
|
||||
|
||||
getNewRequestMatchesInPlex()
|
||||
.then(newMatches =>
|
||||
Promise.all(newMatches.map(match => updateMatchInDb(match, "downloaded")))
|
||||
)
|
||||
.then(() => process.exit(0));
|
||||
73
seasoned_api/src/cache/redis.js
vendored
73
seasoned_api/src/cache/redis.js
vendored
@@ -1,73 +0,0 @@
|
||||
const { promisify } = require("util");
|
||||
const configuration = require("../config/configuration").getInstance();
|
||||
|
||||
let client;
|
||||
|
||||
try {
|
||||
const redis = require("redis");
|
||||
console.log("Trying to connect with redis..");
|
||||
const host = configuration.get("redis", "host");
|
||||
const port = configuration.get("redis", "port");
|
||||
|
||||
console.log(`redis://${host}:${port}`);
|
||||
client = redis.createClient({
|
||||
url: `redis://${host}:${port}`
|
||||
});
|
||||
|
||||
client.on("connect", () => console.log("Redis connection established!"));
|
||||
|
||||
client.on("error", function (err) {
|
||||
client.quit();
|
||||
console.error("Unable to connect to redis, setting up redis-mock.");
|
||||
|
||||
client = {
|
||||
get: function () {
|
||||
console.log("redis-dummy get", arguments[0]);
|
||||
return Promise.resolve();
|
||||
},
|
||||
set: function () {
|
||||
console.log("redis-dummy set", arguments[0]);
|
||||
return Promise.resolve();
|
||||
}
|
||||
};
|
||||
});
|
||||
} catch (e) {}
|
||||
|
||||
function set(key, value, TTL = 10800) {
|
||||
if (value == null || key == null) return null;
|
||||
|
||||
const json = JSON.stringify(value);
|
||||
client.set(key, json, (error, reply) => {
|
||||
if (reply == "OK") {
|
||||
// successfully set value with key, now set TTL for key
|
||||
client.expire(key, TTL, e => {
|
||||
if (e)
|
||||
console.error(
|
||||
"Unexpected error while setting expiration for key:",
|
||||
key,
|
||||
". Error:",
|
||||
error
|
||||
);
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
return value;
|
||||
}
|
||||
|
||||
function get() {
|
||||
return new Promise((resolve, reject) => {
|
||||
client.get(key, (error, reply) => {
|
||||
if (reply == null) {
|
||||
return reject();
|
||||
}
|
||||
|
||||
resolve(JSON.parse(reply));
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
get,
|
||||
set
|
||||
};
|
||||
@@ -1,47 +0,0 @@
|
||||
const path = require("path");
|
||||
const Field = require("./field.js");
|
||||
|
||||
let instance = null;
|
||||
|
||||
class Config {
|
||||
constructor() {
|
||||
this.location = Config.determineLocation();
|
||||
this.fields = require(`${this.location}`);
|
||||
}
|
||||
|
||||
static getInstance() {
|
||||
if (instance == null) {
|
||||
instance = new Config();
|
||||
}
|
||||
return instance;
|
||||
}
|
||||
|
||||
static determineLocation() {
|
||||
return path.join(__dirname, "..", "..", process.env.SEASONED_CONFIG);
|
||||
}
|
||||
|
||||
get(section, option) {
|
||||
if (
|
||||
this.fields[section] === undefined ||
|
||||
this.fields[section][option] === undefined
|
||||
) {
|
||||
throw new Error(`Field "${section} => ${option}" does not exist.`);
|
||||
}
|
||||
|
||||
const field = new Field(this.fields[section][option]);
|
||||
|
||||
const envField =
|
||||
process.env[[section.toUpperCase(), option.toUpperCase()].join("_")];
|
||||
if (envField !== undefined && envField.length !== 0) {
|
||||
return envField;
|
||||
}
|
||||
|
||||
if (field.value === undefined) {
|
||||
throw new Error(`${section} => ${option} is empty.`);
|
||||
}
|
||||
|
||||
return field.value;
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = Config;
|
||||
@@ -1,15 +0,0 @@
|
||||
class EnvironmentVariables {
|
||||
constructor(variables) {
|
||||
this.variables = variables || process.env;
|
||||
}
|
||||
|
||||
get(variable) {
|
||||
return this.variables[variable];
|
||||
}
|
||||
|
||||
has(variable) {
|
||||
return this.get(variable) !== undefined;
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = EnvironmentVariables;
|
||||
@@ -1,49 +0,0 @@
|
||||
const Filters = require('./filters.js');
|
||||
const EnvironmentVariables = require('./environmentVariables.js');
|
||||
|
||||
class Field {
|
||||
constructor(rawValue, environmentVariables) {
|
||||
this.rawValue = rawValue;
|
||||
this.filters = new Filters(rawValue);
|
||||
this.valueWithoutFilters = this.filters.removeFiltersFromValue();
|
||||
this.environmentVariables = new EnvironmentVariables(environmentVariables);
|
||||
}
|
||||
|
||||
get value() {
|
||||
if (this.filters.isEmpty()) {
|
||||
return this.valueWithoutFilters;
|
||||
}
|
||||
|
||||
if (this.filters.has('base64') && !this.filters.has('env')) {
|
||||
return Field.base64Decode(this.valueWithoutFilters);
|
||||
}
|
||||
|
||||
if (this.environmentVariables.has(this.valueWithoutFilters) &&
|
||||
this.environmentVariables.get(this.valueWithoutFilters) === '') {
|
||||
return undefined;
|
||||
}
|
||||
|
||||
if (!this.filters.has('base64') && this.filters.has('env')) {
|
||||
if (this.environmentVariables.has(this.valueWithoutFilters)) {
|
||||
return this.environmentVariables.get(this.valueWithoutFilters);
|
||||
}
|
||||
return undefined;
|
||||
}
|
||||
|
||||
if (this.filters.has('env') && this.filters.has('base64')) {
|
||||
if (this.environmentVariables.has(this.valueWithoutFilters)) {
|
||||
const encodedEnvironmentVariable = this.environmentVariables.get(this.valueWithoutFilters);
|
||||
return Field.base64Decode(encodedEnvironmentVariable);
|
||||
}
|
||||
return undefined;
|
||||
}
|
||||
|
||||
return this.valueWithoutFilters;
|
||||
}
|
||||
|
||||
static base64Decode(string) {
|
||||
return new Buffer(string, 'base64').toString('utf-8');
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = Field;
|
||||
@@ -1,34 +0,0 @@
|
||||
class Filters {
|
||||
constructor(value) {
|
||||
this.value = value;
|
||||
this.delimiter = '|';
|
||||
}
|
||||
|
||||
get filters() {
|
||||
return this.value.split(this.delimiter).slice(0, -1);
|
||||
}
|
||||
|
||||
isEmpty() {
|
||||
return !this.hasValidType() || this.value.length === 0;
|
||||
}
|
||||
|
||||
has(filter) {
|
||||
return this.filters.includes(filter);
|
||||
}
|
||||
|
||||
hasValidType() {
|
||||
return (typeof this.value === 'string');
|
||||
}
|
||||
|
||||
removeFiltersFromValue() {
|
||||
if (this.hasValidType() === false) {
|
||||
return this.value;
|
||||
}
|
||||
|
||||
let filtersCombined = this.filters.join(this.delimiter);
|
||||
filtersCombined += this.filters.length >= 1 ? this.delimiter : '';
|
||||
return this.value.replace(filtersCombined, '');
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = Filters;
|
||||
@@ -1,13 +0,0 @@
|
||||
const configuration = require("../config/configuration").getInstance();
|
||||
const SqliteDatabase = require("./sqliteDatabase");
|
||||
|
||||
const database = new SqliteDatabase(configuration.get("database", "host"));
|
||||
/**
|
||||
* This module establishes a connection to the database
|
||||
* specified in the confgiuration file. It tries to setup
|
||||
* the required tables after successfully connecting.
|
||||
* If the tables already exists, it simply proceeds.
|
||||
*/
|
||||
Promise.resolve().then(() => database.setUp());
|
||||
|
||||
module.exports = database;
|
||||
@@ -1,95 +0,0 @@
|
||||
CREATE TABLE IF NOT EXISTS user (
|
||||
user_name varchar(127) UNIQUE,
|
||||
password varchar(127),
|
||||
admin boolean DEFAULT 0,
|
||||
email varchar(127) UNIQUE,
|
||||
primary key (user_name)
|
||||
);
|
||||
|
||||
CREATE TABLE IF NOT EXISTS settings (
|
||||
user_name varchar(127) UNIQUE,
|
||||
dark_mode boolean DEFAULT 0,
|
||||
plex_userid varchar(127) DEFAULT NULL,
|
||||
emoji varchar(16) DEFAULT NULL,
|
||||
foreign key(user_name) REFERENCES user(user_name) ON DELETE CASCADE
|
||||
);
|
||||
|
||||
CREATE TABLE IF NOT EXISTS cache (
|
||||
key varchar(255),
|
||||
value blob,
|
||||
time_to_live INTEGER DEFAULT 60,
|
||||
created_at DATE DEFAULT (datetime('now','localtime')),
|
||||
primary key(key)
|
||||
);
|
||||
|
||||
CREATE TABLE IF NOT EXISTS search_history (
|
||||
id integer,
|
||||
user_name varchar(127),
|
||||
search_query varchar(255),
|
||||
primary key (id),
|
||||
foreign key(user_name) REFERENCES user(user_name) ON DELETE CASCADE
|
||||
);
|
||||
|
||||
CREATE TABLE IF NOT EXISTS requests (
|
||||
id NUMBER,
|
||||
title TEXT,
|
||||
year NUMBER,
|
||||
poster_path TEXT DEFAULT NULL,
|
||||
background_path TEXT DEFAULT NULL,
|
||||
requested_by varchar(127) DEFAULT NULL,
|
||||
ip TEXT,
|
||||
date DATE DEFAULT CURRENT_TIMESTAMP,
|
||||
status CHAR(25) DEFAULT 'requested' NOT NULL,
|
||||
user_agent CHAR(255) DEFAULT NULL,
|
||||
type CHAR(50) DEFAULT 'movie'
|
||||
-- foreign key(requested_by) REFERENCES user(user_name) ON DELETE SET NULL
|
||||
);
|
||||
|
||||
CREATE TABLE IF NOT EXISTS request(
|
||||
id int not null,
|
||||
title text not null,
|
||||
year int not null,
|
||||
type char(10) not null,
|
||||
date timestamp default (strftime('%s', 'now'))
|
||||
);
|
||||
|
||||
|
||||
CREATE TABLE IF NOT EXISTS stray_eps(
|
||||
id TEXT UNIQUE,
|
||||
parent TEXT,
|
||||
path TEXT,
|
||||
name TEXT,
|
||||
season NUMBER,
|
||||
episode NUMBER,
|
||||
video_files TEXT,
|
||||
subtitles TEXT,
|
||||
trash TEXT,
|
||||
verified BOOLEAN DEFAULT 0,
|
||||
primary key(id)
|
||||
);
|
||||
|
||||
CREATE TABLE IF NOT EXISTS shows(
|
||||
show_names TEXT,
|
||||
date_added DATE,
|
||||
date_modified DATE DEFUALT CURRENT_DATE NOT NULL
|
||||
);
|
||||
|
||||
CREATE TABLE IF NOT EXISTS requested_torrent (
|
||||
magnet TEXT UNIQUE,
|
||||
torrent_name TEXT,
|
||||
tmdb_id TEXT
|
||||
date_added DATE DEFAULT (datetime('now','localtime'))
|
||||
);
|
||||
|
||||
CREATE TABLE IF NOT EXISTS deluge_torrent (
|
||||
key TEXT UNIQUE,
|
||||
name TEXT,
|
||||
progress TEXT,
|
||||
eta NUMBER,
|
||||
save_path TEXT,
|
||||
state TEXT,
|
||||
paused BOOLEAN,
|
||||
finished BOOLEAN,
|
||||
files TEXT,
|
||||
is_folder BOOLEAN
|
||||
)
|
||||
@@ -1,5 +0,0 @@
|
||||
DROP TABLE IF EXISTS user;
|
||||
DROP TABLE IF EXISTS settings;
|
||||
DROP TABLE IF EXISTS search_history;
|
||||
DROP TABLE IF EXISTS requests;
|
||||
DROP TABLE IF EXISTS request;
|
||||
@@ -1,119 +0,0 @@
|
||||
const fs = require("fs");
|
||||
const path = require("path");
|
||||
const sqlite3 = require("sqlite3").verbose();
|
||||
|
||||
class SqliteDatabase {
|
||||
constructor(host) {
|
||||
this.host = host;
|
||||
this.connection = new sqlite3.Database(this.host);
|
||||
this.execute("pragma foreign_keys = on;");
|
||||
this.schemaDirectory = path.join(__dirname, "schemas");
|
||||
}
|
||||
|
||||
/**
|
||||
* Connect to the database.
|
||||
* @returns {Promise} succeeds if connection was established
|
||||
*/
|
||||
// connect() {
|
||||
// let database = ;
|
||||
// this.connection = database;
|
||||
// return database;
|
||||
// }
|
||||
|
||||
/**
|
||||
* Run a SQL query against the database.
|
||||
* @param {String} sql SQL query
|
||||
* @param {Array} parameters in the SQL query
|
||||
* @returns {Promise}
|
||||
*/
|
||||
run(sql, parameters) {
|
||||
return new Promise((resolve, reject) => {
|
||||
this.connection.run(sql, parameters, (error, result) => {
|
||||
if (error) reject(error);
|
||||
resolve(result);
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Run a SQL query against the database and retrieve all the rows.
|
||||
* @param {String} sql SQL query
|
||||
* @param {Array} parameters in the SQL query
|
||||
* @returns {Promise}
|
||||
*/
|
||||
all(sql, parameters) {
|
||||
return new Promise((resolve, reject) => {
|
||||
this.connection.all(sql, parameters, (err, rows) => {
|
||||
if (err) {
|
||||
reject(err);
|
||||
}
|
||||
resolve(rows);
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Run a SQL query against the database and retrieve one row.
|
||||
* @param {String} sql SQL query
|
||||
* @param {Array} parameters in the SQL query
|
||||
* @returns {Promise}
|
||||
*/
|
||||
get(sql, parameters) {
|
||||
return new Promise((resolve, reject) => {
|
||||
this.connection.get(sql, parameters, (err, rows) => {
|
||||
if (err) {
|
||||
reject(err);
|
||||
}
|
||||
resolve(rows);
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Run a SQL query against the database and retrieve the status.
|
||||
* @param {String} sql SQL query
|
||||
* @param {Array} parameters in the SQL query
|
||||
* @returns {Promise}
|
||||
*/
|
||||
execute(sql) {
|
||||
return new Promise(resolve => {
|
||||
this.connection.exec(sql, (err, database) => {
|
||||
if (err) {
|
||||
console.log("ERROR: ", err);
|
||||
reject(err);
|
||||
}
|
||||
resolve();
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Setup the database by running setup.sql file in schemas/.
|
||||
* @returns {Promise}
|
||||
*/
|
||||
setUp() {
|
||||
const setupSchema = this.readSqlFile("setup.sql");
|
||||
return Promise.resolve(this.execute(setupSchema));
|
||||
}
|
||||
|
||||
/**
|
||||
* Tears down the database by running tearDown.sql file in schemas/.
|
||||
* @returns {Promise}
|
||||
*/
|
||||
tearDown() {
|
||||
const tearDownSchema = this.readSqlFile("teardown.sql");
|
||||
return Promise.resolve(this.execute(tearDownSchema));
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the file contents of a SQL file in schemas/.
|
||||
* @returns {String}
|
||||
*/
|
||||
readSqlFile(filename) {
|
||||
const schemaPath = path.join(this.schemaDirectory, filename);
|
||||
const schema = fs.readFileSync(schemaPath).toString("utf-8");
|
||||
return schema;
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = SqliteDatabase;
|
||||
@@ -1,9 +0,0 @@
|
||||
|
||||
class GitRepository {
|
||||
static dumpHook(body) {
|
||||
/* eslint-disable no-console */
|
||||
console.log(body);
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = GitRepository;
|
||||
@@ -1,19 +0,0 @@
|
||||
|
||||
class Media {
|
||||
constructor(title, year, type) {
|
||||
this.title = title;
|
||||
this.year = year;
|
||||
this.type = type;
|
||||
}
|
||||
|
||||
toString() {
|
||||
return `N: ${this.title} | Y: ${this.year} | T: ${this.type}`;
|
||||
}
|
||||
|
||||
print() {
|
||||
/* eslint-disable no-console */
|
||||
console.log(this.toString());
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = Media;
|
||||
@@ -1,15 +0,0 @@
|
||||
class MediaInfo {
|
||||
constructor() {
|
||||
this.duration = undefined;
|
||||
this.height = undefined;
|
||||
this.width = undefined;
|
||||
this.bitrate = undefined;
|
||||
this.resolution = undefined;
|
||||
this.framerate = undefined;
|
||||
this.protocol = undefined;
|
||||
this.container = undefined;
|
||||
this.audioCodec = undefined;
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = MediaInfo;
|
||||
@@ -1,12 +0,0 @@
|
||||
class Player {
|
||||
constructor(device, address) {
|
||||
this.device = device;
|
||||
this.ip = address;
|
||||
this.platform = undefined;
|
||||
this.product = undefined;
|
||||
this.title = undefined;
|
||||
this.state = undefined;
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = Player;
|
||||
@@ -1,31 +0,0 @@
|
||||
const Media = require("./media");
|
||||
|
||||
class Plex extends Media {
|
||||
constructor(
|
||||
title,
|
||||
year,
|
||||
type,
|
||||
summary,
|
||||
poster_path,
|
||||
background_path,
|
||||
added,
|
||||
seasons,
|
||||
episodes
|
||||
) {
|
||||
super(title, year, type);
|
||||
|
||||
this.summary = summary;
|
||||
this.poster_path = poster_path;
|
||||
this.background_path = background_path;
|
||||
this.added = added;
|
||||
|
||||
this.seasons = seasons;
|
||||
this.episodes = episodes;
|
||||
}
|
||||
|
||||
print() {
|
||||
super.print();
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = Plex;
|
||||
@@ -1,46 +0,0 @@
|
||||
const Media = require("./media");
|
||||
|
||||
class TMDB extends Media {
|
||||
// constructor(...args) {
|
||||
constructor(
|
||||
title,
|
||||
year,
|
||||
type,
|
||||
id,
|
||||
summary,
|
||||
poster_path,
|
||||
background_path,
|
||||
popularity,
|
||||
score,
|
||||
release_status,
|
||||
tagline,
|
||||
seasons,
|
||||
episodes
|
||||
) {
|
||||
super(title, year, type);
|
||||
|
||||
this.id = id;
|
||||
this.summary = summary;
|
||||
this.poster_path = poster_path;
|
||||
this.background_path = background_path;
|
||||
this.popularity = popularity;
|
||||
this.score = score;
|
||||
|
||||
this.release_status = release_status;
|
||||
this.tagline = tagline;
|
||||
|
||||
this.seasons = seasons;
|
||||
this.episodes = episodes;
|
||||
}
|
||||
|
||||
toString() {
|
||||
return `${super.toString()} | ID: ${this.id}`;
|
||||
}
|
||||
|
||||
print() {
|
||||
/* eslint-disable no-console */
|
||||
console.log(this.toString());
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = TMDB;
|
||||
@@ -1,8 +0,0 @@
|
||||
class User {
|
||||
constructor(id, title) {
|
||||
this.id = id;
|
||||
this.title = title;
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = User;
|
||||
@@ -1,35 +0,0 @@
|
||||
const request = require("request");
|
||||
const configuration = require("../config/configuration").getInstance();
|
||||
|
||||
const sendSMS = message => {
|
||||
const apiKey = configuration.get("sms", "apikey");
|
||||
|
||||
if (!apiKey) {
|
||||
console.warning("api key for sms not set, cannot send sms.");
|
||||
return null;
|
||||
}
|
||||
|
||||
const sender = configuration.get("sms", "sender");
|
||||
const recipient = configuration.get("sms", "recipient");
|
||||
|
||||
return new Promise((resolve, reject) => {
|
||||
request.post(
|
||||
{
|
||||
url: `https://gatewayapi.com/rest/mtsms?token=${apiKey}`,
|
||||
json: true,
|
||||
body: {
|
||||
sender,
|
||||
message,
|
||||
recipients: [{ msisdn: `47${recipient}` }]
|
||||
}
|
||||
},
|
||||
function (err, r, body) {
|
||||
console.log(err ? err : body);
|
||||
console.log("sms provider response:", body);
|
||||
resolve();
|
||||
}
|
||||
);
|
||||
});
|
||||
};
|
||||
|
||||
module.exports = { sendSMS };
|
||||
@@ -1,103 +0,0 @@
|
||||
const assert = require("assert");
|
||||
const http = require("http");
|
||||
const { URL } = require("url");
|
||||
const PythonShell = require("python-shell");
|
||||
|
||||
const establishedDatabase = require("../database/database");
|
||||
|
||||
const cache = require("../cache/redis");
|
||||
|
||||
function getMagnetFromURL(url) {
|
||||
return new Promise((resolve, reject) => {
|
||||
const options = new URL(url);
|
||||
if (options.protocol.includes("magnet")) resolve(url);
|
||||
|
||||
http.get(options, res => {
|
||||
if (res.statusCode == 301 || res.statusCode == 302) {
|
||||
resolve(res.headers.location);
|
||||
}
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
async function find(searchterm, callback) {
|
||||
const options = {
|
||||
pythonPath: "../torrent_search/env/bin/python3",
|
||||
scriptPath: "../torrent_search",
|
||||
args: [searchterm, "-s", "jackett", "--print"]
|
||||
};
|
||||
|
||||
PythonShell.run("torrentSearch/search.py", options, callback);
|
||||
// PythonShell does not support return
|
||||
}
|
||||
|
||||
async function callPythonAddMagnet(url, callback) {
|
||||
getMagnetFromURL(url)
|
||||
.then(magnet => {
|
||||
const options = {
|
||||
pythonPath: "../delugeClient/env/bin/python3",
|
||||
scriptPath: "../delugeClient",
|
||||
args: ["add", magnet]
|
||||
};
|
||||
|
||||
PythonShell.run("deluge_cli.py", options, callback);
|
||||
})
|
||||
.catch(err => {
|
||||
console.log(err);
|
||||
throw new Error(err);
|
||||
});
|
||||
}
|
||||
|
||||
async function SearchPiratebay(query) {
|
||||
if (query && query.includes("+")) {
|
||||
query = query.replace("+", "%20");
|
||||
}
|
||||
|
||||
const cacheKey = `pirate/${query}`;
|
||||
|
||||
return new Promise((resolve, reject) =>
|
||||
cache
|
||||
.get(cacheKey)
|
||||
.then(resolve)
|
||||
.catch(() =>
|
||||
find(query, (err, results) => {
|
||||
if (err) {
|
||||
console.log("THERE WAS A FUCKING ERROR!\n", err);
|
||||
reject(Error("There was a error when searching for torrents"));
|
||||
}
|
||||
|
||||
if (results) {
|
||||
const jsonData = JSON.parse(results[1], null, "\t");
|
||||
cache.set(cacheKey, jsonData);
|
||||
resolve(jsonData);
|
||||
}
|
||||
})
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
async function AddMagnet(magnet, name, tmdb_id) {
|
||||
return await new Promise((resolve, reject) =>
|
||||
callPythonAddMagnet(magnet, (err, results) => {
|
||||
if (err) {
|
||||
/* eslint-disable no-console */
|
||||
console.log(err);
|
||||
reject(Error("Enable to add torrent", err));
|
||||
}
|
||||
/* eslint-disable no-console */
|
||||
console.log("result/error:", err, results);
|
||||
|
||||
database = establishedDatabase;
|
||||
insert_query =
|
||||
"INSERT INTO requested_torrent(magnet,torrent_name,tmdb_id) \
|
||||
VALUES (?,?,?)";
|
||||
|
||||
let response = database.run(insert_query, [magnet, name, tmdb_id]);
|
||||
console.log("Response from requsted_torrent insert: " + response);
|
||||
|
||||
resolve({ success: true });
|
||||
})
|
||||
);
|
||||
}
|
||||
|
||||
module.exports = { SearchPiratebay, AddMagnet };
|
||||
@@ -1,24 +0,0 @@
|
||||
const Episode = require("./types/episode");
|
||||
|
||||
function convertPlexToEpisode(plexEpisode) {
|
||||
const episode = new Episode(
|
||||
plexEpisode.title,
|
||||
plexEpisode.grandparentTitle,
|
||||
plexEpisode.year
|
||||
);
|
||||
episode.season = plexEpisode.parentIndex;
|
||||
episode.episode = plexEpisode.index;
|
||||
episode.summary = plexEpisode.summary;
|
||||
episode.rating = plexEpisode.rating;
|
||||
|
||||
if (plexEpisode.viewCount !== undefined) {
|
||||
episode.views = plexEpisode.viewCount;
|
||||
}
|
||||
|
||||
if (plexEpisode.originallyAvailableAt !== undefined) {
|
||||
episode.airdate = new Date(plexEpisode.originallyAvailableAt);
|
||||
}
|
||||
|
||||
return episode;
|
||||
}
|
||||
module.exports = convertPlexToEpisode;
|
||||
@@ -1,15 +0,0 @@
|
||||
const Movie = require("./types/movie");
|
||||
|
||||
function convertPlexToMovie(plexMovie) {
|
||||
const movie = new Movie(plexMovie.title, plexMovie.year);
|
||||
movie.rating = plexMovie.rating;
|
||||
movie.tagline = plexMovie.tagline;
|
||||
|
||||
if (plexMovie.summary !== undefined) {
|
||||
movie.summary = plexMovie.summary;
|
||||
}
|
||||
|
||||
return movie;
|
||||
}
|
||||
|
||||
module.exports = convertPlexToMovie;
|
||||
@@ -1,34 +0,0 @@
|
||||
const Plex = require("../media_classes/plex");
|
||||
|
||||
function translateAdded(date_string) {
|
||||
return new Date(date_string * 1000);
|
||||
}
|
||||
|
||||
function convertPlexToSeasoned(plex) {
|
||||
const title = plex.title;
|
||||
const year = plex.year;
|
||||
const type = plex.type;
|
||||
const summary = plex.summary;
|
||||
const poster_path = plex.thumb;
|
||||
const background_path = plex.art;
|
||||
const added = translateAdded(plex.addedAt);
|
||||
// const genre = plex.genre;
|
||||
const seasons = plex.childCount;
|
||||
const episodes = plex.leafCount;
|
||||
|
||||
const seasoned = new Plex(
|
||||
title,
|
||||
year,
|
||||
type,
|
||||
summary,
|
||||
poster_path,
|
||||
background_path,
|
||||
added,
|
||||
seasons,
|
||||
episodes
|
||||
);
|
||||
// seasoned.print();
|
||||
return seasoned;
|
||||
}
|
||||
|
||||
module.exports = convertPlexToSeasoned;
|
||||
@@ -1,13 +0,0 @@
|
||||
const Show = require("./types/show");
|
||||
|
||||
function convertPlexToShow(plexShow) {
|
||||
const show = new Show(plexShow.title, plexShow.year);
|
||||
show.summary = plexShow.summary;
|
||||
show.rating = plexShow.rating;
|
||||
show.seasons = plexShow.childCount;
|
||||
show.episodes = plexShow.leafCount;
|
||||
|
||||
return show;
|
||||
}
|
||||
|
||||
module.exports = convertPlexToShow;
|
||||
@@ -1,19 +0,0 @@
|
||||
const convertPlexToSeasoned = require("./convertPlexToSeasoned");
|
||||
const convertStreamToMediaInfo = require("./convertStreamToMediaInfo");
|
||||
const convertStreamToPlayer = require("./stream/convertStreamToPlayer");
|
||||
const convertStreamToUser = require("./stream/convertStreamToUser");
|
||||
const ConvertStreamToPlayback = require("./stream/convertStreamToPlayback");
|
||||
|
||||
function convertPlexToStream(plexStream) {
|
||||
const stream = convertPlexToSeasoned(plexStream);
|
||||
const plexStreamMedia = plexStream.Media[0];
|
||||
stream.mediaInfo = convertStreamToMediaInfo(plexStreamMedia);
|
||||
stream.player = convertStreamToPlayer(plexStream.Player);
|
||||
|
||||
stream.user = convertStreamToUser(plexStream.User);
|
||||
stream.playback = new ConvertStreamToPlayback(plexStreamMedia.Part[0]);
|
||||
|
||||
return stream;
|
||||
}
|
||||
|
||||
module.exports = convertPlexToStream;
|
||||
@@ -1,22 +0,0 @@
|
||||
const MediaInfo = require("../media_classes/mediaInfo");
|
||||
|
||||
function convertStreamToMediaInfo(plexStream) {
|
||||
const mediaInfo = new MediaInfo();
|
||||
|
||||
mediaInfo.duration = plexStream.duration;
|
||||
mediaInfo.height = plexStream.height;
|
||||
mediaInfo.width = plexStream.width;
|
||||
|
||||
if (plexStream.bitrate) {
|
||||
mediaInfo.bitrate = plexStream.bitrate;
|
||||
}
|
||||
mediaInfo.resolution = plexStream.videoResolution;
|
||||
mediaInfo.framerate = plexStream.videoFrameRate;
|
||||
mediaInfo.protocol = plexStream.protocol;
|
||||
mediaInfo.container = plexStream.container;
|
||||
mediaInfo.audioCodec = plexStream.audioCodec;
|
||||
|
||||
return mediaInfo;
|
||||
}
|
||||
|
||||
module.exports = convertStreamToMediaInfo;
|
||||
@@ -1,7 +0,0 @@
|
||||
const configuration = require("../config/configuration").getInstance();
|
||||
|
||||
function hookDumpController(req, res) {
|
||||
console.log(req);
|
||||
}
|
||||
|
||||
module.exports = hookDumpController;
|
||||
@@ -1,25 +0,0 @@
|
||||
class mailTemplate {
|
||||
constructor(mediaItem) {
|
||||
this.mediaItem = mediaItem;
|
||||
this.posterURL = 'https://image.tmdb.org/t/p/w600';
|
||||
}
|
||||
|
||||
toText() {
|
||||
return `${this.mediaItem.title} (${this.mediaItem.year})`; // plain text body
|
||||
}
|
||||
|
||||
toHTML() {
|
||||
const info = {
|
||||
name: this.mediaItem.title,
|
||||
year: `(${this.mediaItem.year})`,
|
||||
poster: this.posterURL + this.mediaItem.poster,
|
||||
};
|
||||
|
||||
return `
|
||||
<h1>${info.name} ${info.year}</h1>
|
||||
<img src="${info.poster}">
|
||||
`;
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = mailTemplate;
|
||||
@@ -1,240 +0,0 @@
|
||||
const fetch = require("node-fetch");
|
||||
const convertPlexToMovie = require("./convertPlexToMovie");
|
||||
const convertPlexToShow = require("./convertPlexToShow");
|
||||
const convertPlexToEpisode = require("./convertPlexToEpisode");
|
||||
|
||||
const { Movie, Show, Person } = require("../tmdb/types");
|
||||
|
||||
const redisCache = require("../cache/redis");
|
||||
|
||||
const sanitize = string => string.toLowerCase().replace(/[^\w]/gi, "");
|
||||
|
||||
function fixedEncodeURIComponent(str) {
|
||||
return encodeURIComponent(str).replace(/[!'()*]/g, function (c) {
|
||||
return "%" + c.charCodeAt(0).toString(16).toUpperCase();
|
||||
});
|
||||
}
|
||||
|
||||
const matchingTitleAndYear = (plex, tmdb) => {
|
||||
let matchingTitle, matchingYear;
|
||||
|
||||
if (plex["title"] != null && tmdb["title"] != null) {
|
||||
const plexTitle = sanitize(plex.title);
|
||||
const tmdbTitle = sanitize(tmdb.title);
|
||||
matchingTitle = plexTitle == tmdbTitle;
|
||||
matchingTitle = matchingTitle
|
||||
? matchingTitle
|
||||
: plexTitle.startsWith(tmdbTitle);
|
||||
} else matchingTitle = false;
|
||||
|
||||
if (plex["year"] != null && tmdb["year"] != null)
|
||||
matchingYear = plex.year == tmdb.year;
|
||||
else matchingYear = false;
|
||||
|
||||
return matchingTitle && matchingYear;
|
||||
};
|
||||
|
||||
const successfullResponse = response => {
|
||||
if (response && response["MediaContainer"]) return response;
|
||||
|
||||
if (
|
||||
response == null ||
|
||||
response["status"] == null ||
|
||||
response["statusText"] == null
|
||||
) {
|
||||
throw Error("Unable to decode response");
|
||||
}
|
||||
|
||||
const { status, statusText } = response;
|
||||
|
||||
if (status === 200) {
|
||||
return response.json();
|
||||
} else {
|
||||
throw { message: statusText, status: status };
|
||||
}
|
||||
};
|
||||
|
||||
class Plex {
|
||||
constructor(ip, port = 32400, cache = null) {
|
||||
this.plexIP = ip;
|
||||
this.plexPort = port;
|
||||
|
||||
this.cache = cache || redisCache;
|
||||
this.cacheTags = {
|
||||
machineInfo: "plex/mi",
|
||||
search: "plex/s"
|
||||
};
|
||||
}
|
||||
|
||||
fetchMachineIdentifier() {
|
||||
const cacheKey = `${this.cacheTags.machineInfo}`;
|
||||
const url = `http://${this.plexIP}:${this.plexPort}/`;
|
||||
const options = {
|
||||
timeout: 20000,
|
||||
headers: { Accept: "application/json" }
|
||||
};
|
||||
|
||||
return new Promise((resolve, reject) =>
|
||||
this.cache
|
||||
.get(cacheKey)
|
||||
.then(machineInfo => resolve(machineInfo["machineIdentifier"]))
|
||||
.catch(() => fetch(url, options))
|
||||
.then(response => response.json())
|
||||
.then(machineInfo =>
|
||||
this.cache.set(cacheKey, machineInfo["MediaContainer"], 2628000)
|
||||
)
|
||||
.then(machineInfo => resolve(machineInfo["machineIdentifier"]))
|
||||
.catch(error => {
|
||||
if (error != undefined && error.type === "request-timeout") {
|
||||
reject({
|
||||
message: "Plex did not respond",
|
||||
status: 408,
|
||||
success: false
|
||||
});
|
||||
}
|
||||
|
||||
reject(error);
|
||||
})
|
||||
);
|
||||
}
|
||||
|
||||
matchTmdbAndPlexMedia(plex, tmdb) {
|
||||
let match;
|
||||
|
||||
if (plex == null || tmdb == null) return false;
|
||||
|
||||
if (plex instanceof Array) {
|
||||
let possibleMatches = plex.map(plexItem =>
|
||||
matchingTitleAndYear(plexItem, tmdb)
|
||||
);
|
||||
match = possibleMatches.includes(true);
|
||||
} else {
|
||||
match = matchingTitleAndYear(plex, tmdb);
|
||||
}
|
||||
|
||||
return match;
|
||||
}
|
||||
|
||||
async existsInPlex(tmdb) {
|
||||
const plexMatch = await this.findPlexItemByTitleAndYear(
|
||||
tmdb.title,
|
||||
tmdb.year
|
||||
);
|
||||
return plexMatch ? true : false;
|
||||
}
|
||||
|
||||
findPlexItemByTitleAndYear(title, year) {
|
||||
const query = { title, year };
|
||||
|
||||
return this.search(title).then(plexResults => {
|
||||
const matchesInPlex = plexResults.map(plex =>
|
||||
this.matchTmdbAndPlexMedia(plex, query)
|
||||
);
|
||||
const matchesIndex = matchesInPlex.findIndex(el => el === true);
|
||||
return matchesInPlex != -1 ? plexResults[matchesIndex] : null;
|
||||
});
|
||||
}
|
||||
|
||||
getDirectLinkByTitleAndYear(title, year) {
|
||||
const machineIdentifierPromise = this.fetchMachineIdentifier();
|
||||
const matchingObjectInPlexPromise = this.findPlexItemByTitleAndYear(
|
||||
title,
|
||||
year
|
||||
);
|
||||
|
||||
return Promise.all([
|
||||
machineIdentifierPromise,
|
||||
matchingObjectInPlexPromise
|
||||
]).then(([machineIdentifier, matchingObjectInPlex]) => {
|
||||
if (
|
||||
matchingObjectInPlex == false ||
|
||||
matchingObjectInPlex == null ||
|
||||
matchingObjectInPlex["key"] == null ||
|
||||
machineIdentifier == null
|
||||
)
|
||||
return false;
|
||||
|
||||
const keyUriComponent = fixedEncodeURIComponent(matchingObjectInPlex.key);
|
||||
return `https://app.plex.tv/desktop#!/server/${machineIdentifier}/details?key=${keyUriComponent}`;
|
||||
});
|
||||
}
|
||||
|
||||
search(query) {
|
||||
const cacheKey = `${this.cacheTags.search}:${query}`;
|
||||
|
||||
const url = `http://${this.plexIP}:${
|
||||
this.plexPort
|
||||
}/hubs/search?query=${fixedEncodeURIComponent(query)}`;
|
||||
const options = {
|
||||
timeout: 20000,
|
||||
headers: { Accept: "application/json" }
|
||||
};
|
||||
|
||||
return new Promise((resolve, reject) =>
|
||||
this.cache
|
||||
.get(cacheKey)
|
||||
.catch(() => fetch(url, options)) // else fetch fresh data
|
||||
.then(successfullResponse)
|
||||
.then(results => this.cache.set(cacheKey, results, 21600)) // 6 hours
|
||||
.then(this.mapResults)
|
||||
.then(resolve)
|
||||
.catch(error => {
|
||||
if (error != undefined && error.type === "request-timeout") {
|
||||
reject({
|
||||
message: "Plex did not respond",
|
||||
status: 408,
|
||||
success: false
|
||||
});
|
||||
}
|
||||
|
||||
reject(error);
|
||||
})
|
||||
);
|
||||
}
|
||||
|
||||
// this is not guarenteed to work, but if we see a movie or
|
||||
// show has been imported, this function can be helpfull to call
|
||||
// in order to try bust the cache preventing movieInfo and
|
||||
// showInfo from seeing updates through existsInPlex.
|
||||
bustSearchCacheWithTitle(title) {
|
||||
const query = title;
|
||||
const cacheKey = `${this.cacheTags.search}/${query}*`;
|
||||
|
||||
this.cache.del(
|
||||
cacheKey,
|
||||
(error,
|
||||
response => {
|
||||
if (response == 1) return true;
|
||||
|
||||
// TODO improve cache key matching by lowercasing it on the backend.
|
||||
// what do we actually need to check for if the key was deleted or not
|
||||
// it might be an error or another response code.
|
||||
console.log("Unable to delete, key might not exists");
|
||||
})
|
||||
);
|
||||
}
|
||||
|
||||
mapResults(response) {
|
||||
if (
|
||||
response == null ||
|
||||
response.MediaContainer == null ||
|
||||
response.MediaContainer.Hub == null
|
||||
) {
|
||||
return [];
|
||||
}
|
||||
|
||||
return response.MediaContainer.Hub.filter(category => category.size > 0)
|
||||
.map(category => {
|
||||
if (category.type === "movie") {
|
||||
return category.Metadata;
|
||||
} else if (category.type === "show") {
|
||||
return category.Metadata.map(convertPlexToShow);
|
||||
} else if (category.type === "episode") {
|
||||
return category.Metadata.map(convertPlexToEpisode);
|
||||
}
|
||||
})
|
||||
.filter(result => result !== undefined);
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = Plex;
|
||||
@@ -1,110 +0,0 @@
|
||||
const convertPlexToSeasoned = require("./convertPlexToSeasoned");
|
||||
const convertPlexToStream = require("./convertPlexToStream");
|
||||
const rp = require("request-promise");
|
||||
|
||||
class PlexRepository {
|
||||
constructor(plexIP) {
|
||||
this.plexIP = plexIP;
|
||||
}
|
||||
|
||||
inPlex(tmdbResult) {
|
||||
return Promise.resolve()
|
||||
.then(() => this.search(tmdbResult.title))
|
||||
.then(plexResult => this.compareTmdbToPlex(tmdbResult, plexResult))
|
||||
.catch(error => {
|
||||
console.log(error);
|
||||
tmdbResult.matchedInPlex = false;
|
||||
return tmdbResult;
|
||||
});
|
||||
}
|
||||
|
||||
search(query) {
|
||||
const queryUri = encodeURIComponent(query);
|
||||
const uri = encodeURI(
|
||||
`http://${this.plexIP}:32400/search?query=${queryUri}`
|
||||
);
|
||||
const options = {
|
||||
uri: uri,
|
||||
headers: {
|
||||
Accept: "application/json"
|
||||
},
|
||||
json: true
|
||||
};
|
||||
|
||||
return rp(options)
|
||||
.catch(error => {
|
||||
console.log(error);
|
||||
throw new Error("Unable to search plex.");
|
||||
})
|
||||
.then(result => this.mapResults(result))
|
||||
.then(([mappedResults, resultCount]) => ({
|
||||
results: mappedResults,
|
||||
total_results: resultCount
|
||||
}));
|
||||
}
|
||||
|
||||
compareTmdbToPlex(tmdb, plexResult) {
|
||||
return Promise.resolve().then(() => {
|
||||
if (plexResult.results.length === 0) {
|
||||
tmdb.matchedInPlex = false;
|
||||
} else {
|
||||
// console.log('plex and tmdb:', plexResult, '\n', tmdb)
|
||||
plexResult.results.map(plexItem => {
|
||||
if (tmdb.title === plexItem.title && tmdb.year === plexItem.year)
|
||||
tmdb.matchedInPlex = true;
|
||||
return tmdb;
|
||||
});
|
||||
}
|
||||
return tmdb;
|
||||
});
|
||||
}
|
||||
|
||||
mapResults(response) {
|
||||
return Promise.resolve()
|
||||
.then(() => {
|
||||
if (!response.MediaContainer.hasOwnProperty("Metadata")) return [[], 0];
|
||||
|
||||
const mappedResults = response.MediaContainer.Metadata.filter(
|
||||
element => {
|
||||
return element.type === "movie" || element.type === "show";
|
||||
}
|
||||
).map(element => convertPlexToSeasoned(element));
|
||||
return [mappedResults, mappedResults.length];
|
||||
})
|
||||
.catch(error => {
|
||||
throw new Error(error);
|
||||
});
|
||||
}
|
||||
|
||||
nowPlaying() {
|
||||
const options = {
|
||||
uri: `http://${this.plexIP}:32400/status/sessions`,
|
||||
headers: {
|
||||
Accept: "application/json"
|
||||
},
|
||||
json: true
|
||||
};
|
||||
|
||||
return rp(options)
|
||||
.then(result => {
|
||||
if (result.MediaContainer.size > 0) {
|
||||
const playing =
|
||||
result.MediaContainer.Metadata.map(convertPlexToStream);
|
||||
return { size: Object.keys(playing).length, video: playing };
|
||||
}
|
||||
return { size: 0, video: [] };
|
||||
})
|
||||
.catch(err => {
|
||||
throw new Error(`Error handling plex playing. Error: ${err}`);
|
||||
});
|
||||
}
|
||||
|
||||
// multipleInPlex(tmdbResults) {
|
||||
// const results = tmdbResults.results.map(async (tmdb) => {
|
||||
// return this.inPlex(tmdb)
|
||||
// })
|
||||
// return Promise.all(results)
|
||||
// }
|
||||
}
|
||||
|
||||
module.exports = PlexRepository;
|
||||
@@ -1,130 +0,0 @@
|
||||
const PlexRepository = require("./plexRepository");
|
||||
const configuration = require("../config/configuration").getInstance();
|
||||
const TMDB = require("../tmdb/tmdb");
|
||||
const establishedDatabase = require("../database/database");
|
||||
|
||||
const plexRepository = new PlexRepository(configuration.get("plex", "ip"));
|
||||
const tmdb = new TMDB(configuration.get("tmdb", "apiKey"));
|
||||
|
||||
class RequestRepository {
|
||||
constructor(database) {
|
||||
this.database = database || establishedDatabase;
|
||||
this.queries = {
|
||||
insertRequest: `INSERT INTO requests(id,title,year,poster_path,background_path,requested_by,ip,user_agent,type)
|
||||
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)`,
|
||||
fetchRequestedItems:
|
||||
"SELECT * FROM requests ORDER BY date DESC LIMIT 25 OFFSET ?*25-25",
|
||||
fetchRequestedItemsByStatus:
|
||||
"SELECT * FROM requests WHERE status IS ? AND type LIKE ? ORDER BY date DESC LIMIT 25 OFFSET ?*25-25",
|
||||
updateRequestedById:
|
||||
"UPDATE requests SET status = ? WHERE id is ? AND type is ?",
|
||||
checkIfIdRequested: "SELECT * FROM requests WHERE id IS ? AND type IS ?",
|
||||
userRequests:
|
||||
"SELECT * FROM requests WHERE requested_by IS ? ORDER BY date DESC"
|
||||
};
|
||||
this.cacheTags = {
|
||||
search: "se",
|
||||
lookup: "i"
|
||||
};
|
||||
}
|
||||
|
||||
search(query, type, page) {
|
||||
return Promise.resolve()
|
||||
.then(() => tmdb.search(query, type, page))
|
||||
.catch(error => Error(`error in the house${error}`));
|
||||
}
|
||||
|
||||
lookup(identifier, type = "movie") {
|
||||
return Promise.resolve()
|
||||
.then(() => tmdb.lookup(identifier, type))
|
||||
.then(tmdbMovie => this.checkID(tmdbMovie))
|
||||
.then(tmdbMovie => plexRepository.inPlex(tmdbMovie))
|
||||
.catch(error => {
|
||||
throw new Error(error);
|
||||
});
|
||||
}
|
||||
|
||||
checkID(tmdbMovie) {
|
||||
return Promise.resolve()
|
||||
.then(() =>
|
||||
this.database.get(this.queries.checkIfIdRequested, [
|
||||
tmdbMovie.id,
|
||||
tmdbMovie.type
|
||||
])
|
||||
)
|
||||
.then((result, error) => {
|
||||
if (error) {
|
||||
throw new Error(error);
|
||||
}
|
||||
tmdbMovie.requested = result ? true : false;
|
||||
return tmdbMovie;
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Send request for given media id.
|
||||
* @param {identifier, type} the id of the media object and type of media must be defined
|
||||
* @returns {Promise} If nothing has gone wrong.
|
||||
*/
|
||||
sendRequest(identifier, type, ip, user_agent, user) {
|
||||
return Promise.resolve()
|
||||
.then(() => tmdb.lookup(identifier, type))
|
||||
.then(movie => {
|
||||
const username = user === undefined ? undefined : user.username;
|
||||
// Add request to database
|
||||
return this.database.run(this.queries.insertRequest, [
|
||||
movie.id,
|
||||
movie.title,
|
||||
movie.year,
|
||||
movie.poster_path,
|
||||
movie.background_path,
|
||||
username,
|
||||
ip,
|
||||
user_agent,
|
||||
movie.type
|
||||
]);
|
||||
});
|
||||
}
|
||||
|
||||
fetchRequested(status, page = "1", type = "%") {
|
||||
return Promise.resolve().then(() => {
|
||||
if (
|
||||
status === "requested" ||
|
||||
status === "downloading" ||
|
||||
status === "downloaded"
|
||||
)
|
||||
return this.database.all(this.queries.fetchRequestedItemsByStatus, [
|
||||
status,
|
||||
type,
|
||||
page
|
||||
]);
|
||||
else return this.database.all(this.queries.fetchRequestedItems, page);
|
||||
});
|
||||
}
|
||||
|
||||
userRequests(username) {
|
||||
return Promise.resolve()
|
||||
.then(() => this.database.all(this.queries.userRequests, username))
|
||||
.catch(error => {
|
||||
if (String(error).includes("no such column")) {
|
||||
throw new Error("Username not found");
|
||||
}
|
||||
throw new Error("Unable to fetch your requests");
|
||||
})
|
||||
.then(result => {
|
||||
// TODO do a correct mapping before sending, not just a dump of the database
|
||||
result.map(item => (item.poster = item.poster_path));
|
||||
return result;
|
||||
});
|
||||
}
|
||||
|
||||
updateRequestedById(id, type, status) {
|
||||
return this.database.run(this.queries.updateRequestedById, [
|
||||
status,
|
||||
id,
|
||||
type
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = RequestRepository;
|
||||
@@ -1,14 +0,0 @@
|
||||
class convertStreamToPlayback {
|
||||
constructor(plexStream) {
|
||||
this.bitrate = plexStream.bitrate;
|
||||
this.width = plexStream.width;
|
||||
this.height = plexStream.height;
|
||||
this.decision = plexStream.decision;
|
||||
this.audioProfile = plexStream.audioProfile;
|
||||
this.videoProfile = plexStream.videoProfile;
|
||||
this.duration = plexStream.duration;
|
||||
this.container = plexStream.container;
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = convertStreamToPlayback;
|
||||
@@ -1,13 +0,0 @@
|
||||
const Player = require("../../media_classes/player");
|
||||
|
||||
function convertStreamToPlayer(plexStream) {
|
||||
const player = new Player(plexStream.device, plexStream.address);
|
||||
player.platform = plexStream.platform;
|
||||
player.product = plexStream.product;
|
||||
player.title = plexStream.title;
|
||||
player.state = plexStream.state;
|
||||
|
||||
return player;
|
||||
}
|
||||
|
||||
module.exports = convertStreamToPlayer;
|
||||
@@ -1,7 +0,0 @@
|
||||
const User = require("../../media_classes/user");
|
||||
|
||||
function convertStreamToUser(plexStream) {
|
||||
return new User(plexStream.id, plexStream.title);
|
||||
}
|
||||
|
||||
module.exports = convertStreamToUser;
|
||||
@@ -1,16 +0,0 @@
|
||||
class Episode {
|
||||
constructor(title, show, year) {
|
||||
this.title = title;
|
||||
this.show = show;
|
||||
this.year = year;
|
||||
this.season = null;
|
||||
this.episode = null;
|
||||
this.summary = null;
|
||||
this.rating = null;
|
||||
this.views = null;
|
||||
this.aired = null;
|
||||
this.type = 'episode';
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = Episode;
|
||||
@@ -1,12 +0,0 @@
|
||||
class Movie {
|
||||
constructor(title, year) {
|
||||
this.title = title;
|
||||
this.year = year;
|
||||
this.summary = null;
|
||||
this.rating = null;
|
||||
this.tagline = null;
|
||||
this.type = 'movie';
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = Movie;
|
||||
@@ -1,12 +0,0 @@
|
||||
class Show {
|
||||
constructor(title, year) {
|
||||
this.title = title;
|
||||
this.year = year;
|
||||
this.summary = null;
|
||||
this.rating = null;
|
||||
this.seasons = null;
|
||||
this.episodes = null;
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = Show;
|
||||
@@ -1,225 +0,0 @@
|
||||
const assert = require("assert");
|
||||
const configuration = require("../config/configuration").getInstance();
|
||||
const TMDB = require("../tmdb/tmdb");
|
||||
const tmdb = new TMDB(configuration.get("tmdb", "apiKey"));
|
||||
const establishedDatabase = require("../database/database");
|
||||
const utils = require("./utils");
|
||||
|
||||
class RequestRepository {
|
||||
constructor(database) {
|
||||
this.database = database || establishedDatabase;
|
||||
this.queries = {
|
||||
add: "insert into requests (id,title,year,poster_path,background_path,requested_by,ip,user_agent,type) values(?,?,?,?,?,?,?,?,?)",
|
||||
fetchAll:
|
||||
'select * from requests where status != "downloaded" order by date desc LIMIT 25 OFFSET ?*25-25',
|
||||
fetchAllFilteredStatus:
|
||||
"select * from requests where status = ? order by date desc LIMIT 25 offset ?*25-25",
|
||||
totalRequests:
|
||||
'select count(*) as totalRequests from requests where status != "downloaded"',
|
||||
totalRequestsFilteredStatus:
|
||||
"select count(*) as totalRequests from requests where status = ?",
|
||||
fetchAllSort: `select id, type from request order by ? ?`,
|
||||
fetchAllFilter: `select id, type from request where ? is "?"`,
|
||||
fetchAllQuery: `select id, type from request where title like "%?%" or year like "%?%"`,
|
||||
fetchAllFilterAndSort: `select id, type from request where ? is "?" order by ? ?`,
|
||||
downloaded:
|
||||
"(select status from requests where id is request.id and type is request.type limit 1)",
|
||||
// deluge: '(select status from deluge_torrent where id is request.id and type is request.type limit 1)',
|
||||
// fetchAllFilterStatus: 'select * from request where '
|
||||
readWithoutUserData:
|
||||
"select id, title, year, type, status, date from requests where id is ? and type is ?",
|
||||
read: "select id, title, year, type, status, requested_by, ip, date, user_agent from requests where id is ? and type is ?"
|
||||
};
|
||||
}
|
||||
|
||||
sortAndFilterToDbQuery(by, direction, filter, query) {
|
||||
let dbQuery = undefined;
|
||||
|
||||
if (query !== undefined) {
|
||||
const dbParams = [query, query];
|
||||
const dbquery = this.queries.fetchAllQuery;
|
||||
|
||||
dbQuery = dbquery
|
||||
.split("")
|
||||
.map(char => (char === "?" ? dbParams.shift() : char))
|
||||
.join("");
|
||||
} else if (by !== undefined && filter !== undefined) {
|
||||
const paramToColumnAndValue = {
|
||||
movie: ["type", "movie"],
|
||||
show: ["type", "show"]
|
||||
};
|
||||
const dbParams = paramToColumnAndValue[filter].concat([by, direction]);
|
||||
const query = this.queries.fetchAllFilterAndSort;
|
||||
|
||||
dbQuery = query
|
||||
.split("")
|
||||
.map(char => (char === "?" ? dbParams.shift() : char))
|
||||
.join("");
|
||||
} else if (by !== undefined) {
|
||||
const dbParams = [by, direction];
|
||||
const query = this.queries.fetchAllSort;
|
||||
|
||||
dbQuery = query
|
||||
.split("")
|
||||
.map(char => (char === "?" ? dbParams.shift() : char))
|
||||
.join("");
|
||||
} else if (filter !== undefined) {
|
||||
const paramToColumnAndValue = {
|
||||
movie: ["type", "movie"],
|
||||
show: ["type", "show"],
|
||||
downloaded: [this.queries.downloaded, "downloaded"]
|
||||
// downloading: [this.database.delugeStatus, 'downloading']
|
||||
};
|
||||
const dbParams = paramToColumnAndValue[filter];
|
||||
const query = this.queries.fetchAllFilter;
|
||||
|
||||
dbQuery = query
|
||||
.split("")
|
||||
.map(char => (char === "?" ? dbParams.shift() : char))
|
||||
.join("");
|
||||
} else {
|
||||
dbQuery = this.queries.fetchAll;
|
||||
}
|
||||
|
||||
return dbQuery;
|
||||
}
|
||||
|
||||
mapToTmdbByType(rows) {
|
||||
return rows.map(row => {
|
||||
if (row.type === "movie") return tmdb.movieInfo(row.id);
|
||||
else if (row.type === "show") return tmdb.showInfo(row.id);
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Add tmdb movie|show to requests
|
||||
* @param {tmdb} tmdb class of movie|show to add
|
||||
* @returns {Promise}
|
||||
*/
|
||||
requestFromTmdb(tmdb, ip, user_agent, username) {
|
||||
return Promise.resolve()
|
||||
.then(() => this.database.get(this.queries.read, [tmdb.id, tmdb.type]))
|
||||
.then(row =>
|
||||
assert.equal(row, undefined, "Id has already been requested")
|
||||
)
|
||||
.then(() =>
|
||||
this.database.run(this.queries.add, [
|
||||
tmdb.id,
|
||||
tmdb.title,
|
||||
tmdb.year,
|
||||
tmdb.poster,
|
||||
tmdb.backdrop,
|
||||
username,
|
||||
ip,
|
||||
user_agent,
|
||||
tmdb.type
|
||||
])
|
||||
)
|
||||
.catch(error => {
|
||||
if (
|
||||
error.name === "AssertionError" ||
|
||||
error.message.endsWith("been requested")
|
||||
) {
|
||||
throw new Error("This id is already requested", error.message);
|
||||
}
|
||||
console.log("Error @ request.addTmdb:", error);
|
||||
throw new Error("Could not add request");
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Get request item by id
|
||||
* @param {String} id
|
||||
* @param {String} type
|
||||
* @returns {Promise}
|
||||
*/
|
||||
getRequestByIdAndType(id, type) {
|
||||
return this.database
|
||||
.get(this.queries.readWithoutUserData, [id, type])
|
||||
.then(row => {
|
||||
assert(row, "Could not find request item with that id and type");
|
||||
return {
|
||||
id: row.id,
|
||||
title: row.title,
|
||||
year: row.year,
|
||||
type: row.type,
|
||||
status: row.status,
|
||||
requested_date: new Date(row.date)
|
||||
};
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Fetch all requests with optional sort and filter params
|
||||
* @param {String} what we are sorting by
|
||||
* @param {String} direction that can be either 'asc' or 'desc', default 'asc'.
|
||||
* @param {String} params to filter by
|
||||
* @param {String} query param to filter result on. Filters on title and year
|
||||
* @returns {Promise}
|
||||
*/
|
||||
fetchAll(
|
||||
page = 1,
|
||||
sort_by = undefined,
|
||||
sort_direction = "asc",
|
||||
filter = undefined,
|
||||
query = undefined
|
||||
) {
|
||||
// TODO implemented sort and filter
|
||||
page = parseInt(page);
|
||||
let fetchQuery = this.queries.fetchAll;
|
||||
let fetchTotalResults = this.queries.totalRequests;
|
||||
let fetchParams = [page];
|
||||
|
||||
if (
|
||||
filter &&
|
||||
(filter === "downloading" ||
|
||||
filter === "downloaded" ||
|
||||
filter === "requested")
|
||||
) {
|
||||
console.log("tes");
|
||||
fetchQuery = this.queries.fetchAllFilteredStatus;
|
||||
fetchTotalResults = this.queries.totalRequestsFilteredStatus;
|
||||
fetchParams = [filter, page];
|
||||
} else {
|
||||
filter = undefined;
|
||||
}
|
||||
|
||||
return Promise.resolve()
|
||||
.then(dbQuery => this.database.all(fetchQuery, fetchParams))
|
||||
.then(async rows => {
|
||||
const sqliteResponse = await this.database.get(
|
||||
fetchTotalResults,
|
||||
filter ? filter : undefined
|
||||
);
|
||||
const totalRequests = sqliteResponse["totalRequests"];
|
||||
const totalPages = Math.ceil(totalRequests / 26);
|
||||
|
||||
return [
|
||||
rows.map(item => {
|
||||
item.poster = item.poster_path;
|
||||
delete item.poster_path;
|
||||
item.backdrop = item.background_path;
|
||||
delete item.background_path;
|
||||
return item;
|
||||
}),
|
||||
totalPages,
|
||||
totalRequests
|
||||
];
|
||||
return Promise.all(this.mapToTmdbByType(rows));
|
||||
})
|
||||
.then(([result, totalPages, totalRequests]) =>
|
||||
Promise.resolve({
|
||||
results: result,
|
||||
total_results: totalRequests,
|
||||
page: page,
|
||||
total_pages: totalPages
|
||||
})
|
||||
)
|
||||
.catch(error => {
|
||||
console.log(error);
|
||||
throw error;
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = RequestRepository;
|
||||
@@ -1,34 +0,0 @@
|
||||
// TODO : test title and date are valid matches to columns in the database
|
||||
const validSortParams = ['title', 'date']
|
||||
const validSortDirs = ['asc', 'desc']
|
||||
const validFilterParams = ['movie', 'show', 'seeding', 'downloading', 'paused', 'finished', 'downloaded']
|
||||
|
||||
function validSort(by, direction) {
|
||||
return new Promise((resolve, reject) => {
|
||||
if (by === undefined) {
|
||||
resolve()
|
||||
}
|
||||
|
||||
if (validSortParams.includes(by) && validSortDirs.includes(direction)) {
|
||||
resolve()
|
||||
} else {
|
||||
reject(new Error(`invalid sort parameter, must be of: ${validSortParams} with optional sort directions: ${validSortDirs} appended with ':'`))
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
function validFilter(filter_param) {
|
||||
return new Promise((resolve, reject) => {
|
||||
if (filter_param === undefined) {
|
||||
resolve()
|
||||
}
|
||||
|
||||
if (filter_param && validFilterParams.includes(filter_param)) {
|
||||
resolve()
|
||||
} else {
|
||||
reject(new Error(`filter parameteres must be of type: ${validFilterParams}`))
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
module.exports = { validSort, validFilter }
|
||||
@@ -1,57 +0,0 @@
|
||||
const establishedDatabase = require("../database/database");
|
||||
|
||||
class SearchHistory {
|
||||
constructor(database) {
|
||||
this.database = database || establishedDatabase;
|
||||
this.queries = {
|
||||
create:
|
||||
"insert into search_history (search_query, user_name) values (?, ?)",
|
||||
read: "select search_query from search_history where user_name = ? order by id desc"
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrive a search queries for a user from the database.
|
||||
* @param {User} user existing user
|
||||
* @returns {Promise}
|
||||
*/
|
||||
read(user) {
|
||||
return new Promise((resolve, reject) =>
|
||||
this.database
|
||||
.all(this.queries.read, user)
|
||||
.then((result, error) => {
|
||||
if (error) throw new Error(error);
|
||||
resolve(result.map(row => row.search_query));
|
||||
})
|
||||
.catch(error => {
|
||||
console.log("Error when fetching history from database:", error);
|
||||
reject("Unable to get history.");
|
||||
})
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a new search entry in the database.
|
||||
* @param {String} username logged in user doing the search
|
||||
* @param {String} searchQuery the query the user searched for
|
||||
* @returns {Promise}
|
||||
*/
|
||||
create(username, searchQuery) {
|
||||
return this.database
|
||||
.run(this.queries.create, [searchQuery, username])
|
||||
.catch(error => {
|
||||
if (error.message.includes("FOREIGN")) {
|
||||
throw new Error("Could not create search history.");
|
||||
}
|
||||
|
||||
throw {
|
||||
success: false,
|
||||
status: 500,
|
||||
message: "An unexpected error occured",
|
||||
source: "database"
|
||||
};
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = SearchHistory;
|
||||
@@ -1,7 +0,0 @@
|
||||
class Stray {
|
||||
constructor(id) {
|
||||
this.id = id;
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = Stray;
|
||||
@@ -1,68 +0,0 @@
|
||||
const assert = require("assert");
|
||||
const Stray = require("./stray");
|
||||
const establishedDatabase = require("../database/database");
|
||||
const pythonShell = require("python-shell");
|
||||
|
||||
class StrayRepository {
|
||||
constructor(database) {
|
||||
this.database = database || establishedDatabase;
|
||||
this.queries = {
|
||||
read: "SELECT * FROM stray_eps WHERE id = ?",
|
||||
readAll: "SELECT id, name, season, episode, verified FROM stray_eps",
|
||||
readAllFiltered:
|
||||
"SELECT id, name, season, episode, verified FROM stray_eps WHERE verified = ",
|
||||
checkVerified: "SELECT id FROM stray_eps WHERE verified = 0 AND id = ?",
|
||||
verify: "UPDATE stray_eps SET verified = 1 WHERE id = ?"
|
||||
};
|
||||
}
|
||||
|
||||
read(strayId) {
|
||||
return this.database.get(this.queries.read, strayId).then(row => {
|
||||
assert.notEqual(
|
||||
row,
|
||||
undefined,
|
||||
`Could not find list with id ${strayId}.`
|
||||
);
|
||||
return row;
|
||||
});
|
||||
}
|
||||
|
||||
readAll(verified = null) {
|
||||
let dbSearchQuery = this.queries.readAll;
|
||||
if (verified != null) {
|
||||
dbSearchQuery = this.queries.readAllFiltered + verified.toString();
|
||||
}
|
||||
return this.database.all(dbSearchQuery).then(rows =>
|
||||
rows.map(row => {
|
||||
const stray = new Stray(row.id);
|
||||
stray.name = row.name;
|
||||
stray.season = row.season;
|
||||
stray.episode = row.episode;
|
||||
stray.verified = row.verified;
|
||||
return stray;
|
||||
})
|
||||
);
|
||||
}
|
||||
|
||||
verifyStray(strayId) {
|
||||
return this.database.get(this.queries.checkVerified, strayId).then(row => {
|
||||
assert.notEqual(row, undefined, `Stray '${strayId}' already verified.`);
|
||||
|
||||
const options = {
|
||||
pythonPath: "../app/env/bin/python3",
|
||||
args: [strayId]
|
||||
};
|
||||
|
||||
pythonShell.run("../app/moveSeasoned.py", options, (err, results) => {
|
||||
if (err) throw err;
|
||||
// TODO Add error handling!! StrayRepository.ERROR
|
||||
// results is an array consisting of messages collected during execution
|
||||
console.log("results: %j", results);
|
||||
});
|
||||
|
||||
return this.database.run(this.queries.verify, strayId);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = StrayRepository;
|
||||
@@ -1,74 +0,0 @@
|
||||
const fetch = require("node-fetch");
|
||||
|
||||
class Tautulli {
|
||||
constructor(apiKey, ip, port) {
|
||||
this.apiKey = apiKey;
|
||||
this.ip = ip;
|
||||
this.port = port;
|
||||
}
|
||||
|
||||
buildUrlWithCmdAndUserid(cmd, user_id) {
|
||||
const url = new URL("api/v2", `http://${this.ip}:${this.port}`);
|
||||
url.searchParams.append("apikey", this.apiKey);
|
||||
url.searchParams.append("cmd", cmd);
|
||||
url.searchParams.append("user_id", user_id);
|
||||
|
||||
return url;
|
||||
}
|
||||
|
||||
logTautulliError(error) {
|
||||
console.error("error fetching from tautulli");
|
||||
|
||||
throw error;
|
||||
}
|
||||
|
||||
getPlaysByDayOfWeek(plex_userid, days, y_axis) {
|
||||
const url = this.buildUrlWithCmdAndUserid(
|
||||
"get_plays_by_dayofweek",
|
||||
plex_userid
|
||||
);
|
||||
url.searchParams.append("time_range", days);
|
||||
url.searchParams.append("y_axis", y_axis);
|
||||
|
||||
return fetch(url.href)
|
||||
.then(resp => resp.json())
|
||||
.catch(error => this.logTautulliError(error));
|
||||
}
|
||||
|
||||
getPlaysByDays(plex_userid, days, y_axis) {
|
||||
const url = this.buildUrlWithCmdAndUserid("get_plays_by_date", plex_userid);
|
||||
url.searchParams.append("time_range", days);
|
||||
url.searchParams.append("y_axis", y_axis);
|
||||
|
||||
return fetch(url.href)
|
||||
.then(resp => resp.json())
|
||||
.catch(error => this.logTautulliError(error));
|
||||
}
|
||||
|
||||
watchTimeStats(plex_userid) {
|
||||
const url = this.buildUrlWithCmdAndUserid(
|
||||
"get_user_watch_time_stats",
|
||||
plex_userid
|
||||
);
|
||||
url.searchParams.append("grouping", 0);
|
||||
|
||||
return fetch(url.href)
|
||||
.then(resp => resp.json())
|
||||
.catch(error => this.logTautulliError(error));
|
||||
}
|
||||
|
||||
viewHistory(plex_userid) {
|
||||
const url = this.buildUrlWithCmdAndUserid("get_history", plex_userid);
|
||||
|
||||
url.searchParams.append("start", 0);
|
||||
url.searchParams.append("length", 50);
|
||||
|
||||
console.log("fetching url", url.href);
|
||||
|
||||
return fetch(url.href)
|
||||
.then(resp => resp.json())
|
||||
.catch(error => this.logTautulliError(error));
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = Tautulli;
|
||||
@@ -1,3 +0,0 @@
|
||||
{
|
||||
"presets": ["@babel/preset-env"]
|
||||
}
|
||||
@@ -1,48 +0,0 @@
|
||||
const assert = require("assert");
|
||||
const establishedDatabase = require("../database/database");
|
||||
|
||||
class Cache {
|
||||
constructor(database) {
|
||||
this.database = database || establishedDatabase;
|
||||
this.queries = {
|
||||
read:
|
||||
'SELECT value, time_to_live, created_at, DATETIME("now", "localtime") as now, ' +
|
||||
'DATETIME(created_at, "+" || time_to_live || " seconds") as expires ' +
|
||||
"FROM cache WHERE key = ? AND now < expires",
|
||||
create:
|
||||
"INSERT OR REPLACE INTO cache (key, value, time_to_live) VALUES (?, ?, ?)"
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieve an unexpired cache entry by key.
|
||||
* @param {String} key of the cache entry
|
||||
* @returns {Object}
|
||||
*/
|
||||
get(key) {
|
||||
return Promise.resolve()
|
||||
.then(() => this.database.get(this.queries.read, [key]))
|
||||
.then(row => {
|
||||
assert(row, "Could not find cache entry with that key.");
|
||||
return JSON.parse(row.value);
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Insert cache entry with key and value.
|
||||
* @param {String} key of the cache entry
|
||||
* @param {String} value of the cache entry
|
||||
* @param {Number} timeToLive the number of seconds before entry expires
|
||||
* @returns {Object}
|
||||
*/
|
||||
set(key, value, timeToLive = 10800) {
|
||||
const json = JSON.stringify(value);
|
||||
return Promise.resolve()
|
||||
.then(() =>
|
||||
this.database.run(this.queries.create, [key, json, timeToLive])
|
||||
)
|
||||
.then(() => value);
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = Cache;
|
||||
@@ -1,293 +0,0 @@
|
||||
const moviedb = require("km-moviedb");
|
||||
const redisCache = require("../cache/redis");
|
||||
|
||||
const { Movie, Show, Person, Credits, ReleaseDates } = require("./types");
|
||||
|
||||
const tmdbErrorResponse = (error, typeString = undefined) => {
|
||||
if (error.status === 404) {
|
||||
let message = error.response.body.status_message;
|
||||
|
||||
throw {
|
||||
status: 404,
|
||||
message: message.slice(0, -1) + " in tmdb."
|
||||
};
|
||||
} else if (error.status === 401) {
|
||||
throw {
|
||||
status: 401,
|
||||
message: error.response.body.status_message
|
||||
};
|
||||
}
|
||||
|
||||
throw {
|
||||
status: 500,
|
||||
message: `An unexpected error occured while fetching ${typeString} from tmdb`
|
||||
};
|
||||
};
|
||||
|
||||
class TMDB {
|
||||
constructor(apiKey, cache, tmdbLibrary) {
|
||||
this.tmdbLibrary = tmdbLibrary || moviedb(apiKey);
|
||||
|
||||
this.cache = cache || redisCache;
|
||||
this.cacheTags = {
|
||||
multiSearch: "mus",
|
||||
movieSearch: "mos",
|
||||
showSearch: "ss",
|
||||
personSearch: "ps",
|
||||
movieInfo: "mi",
|
||||
movieCredits: "mc",
|
||||
movieReleaseDates: "mrd",
|
||||
movieImages: "mimg",
|
||||
showInfo: "si",
|
||||
showCredits: "sc",
|
||||
personInfo: "pi",
|
||||
personCredits: "pc",
|
||||
miscNowPlayingMovies: "npm",
|
||||
miscPopularMovies: "pm",
|
||||
miscTopRatedMovies: "tpm",
|
||||
miscUpcomingMovies: "um",
|
||||
tvOnTheAir: "toa",
|
||||
miscPopularTvs: "pt",
|
||||
miscTopRatedTvs: "trt"
|
||||
};
|
||||
this.defaultTTL = 86400;
|
||||
}
|
||||
|
||||
getFromCacheOrFetchFromTmdb(cacheKey, tmdbMethod, query) {
|
||||
return new Promise((resolve, reject) =>
|
||||
this.cache
|
||||
.get(cacheKey)
|
||||
.then(resolve)
|
||||
.catch(() => this.tmdb(tmdbMethod, query))
|
||||
.then(resolve)
|
||||
.catch(error => reject(tmdbErrorResponse(error, tmdbMethod)))
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieve a specific movie by id from TMDB.
|
||||
* @param {Number} identifier of the movie you want to retrieve
|
||||
* @param {Boolean} add credits (cast & crew) for movie
|
||||
* @param {Boolean} add release dates for every country
|
||||
* @returns {Promise} succeeds if movie was found
|
||||
*/
|
||||
movieInfo(identifier) {
|
||||
const query = { id: identifier };
|
||||
const cacheKey = `tmdb/${this.cacheTags.movieInfo}:${identifier}`;
|
||||
|
||||
return this.getFromCacheOrFetchFromTmdb(cacheKey, "movieInfo", query)
|
||||
.then(movie => this.cache.set(cacheKey, movie, this.defaultTTL))
|
||||
.then(movie => Movie.convertFromTmdbResponse(movie));
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieve credits for a movie
|
||||
* @param {Number} identifier of the movie to get credits for
|
||||
* @returns {Promise} movie cast object
|
||||
*/
|
||||
movieCredits(identifier) {
|
||||
const query = { id: identifier };
|
||||
const cacheKey = `tmdb/${this.cacheTags.movieCredits}:${identifier}`;
|
||||
|
||||
return this.getFromCacheOrFetchFromTmdb(cacheKey, "movieCredits", query)
|
||||
.then(credits => this.cache.set(cacheKey, credits, this.defaultTTL))
|
||||
.then(credits => Credits.convertFromTmdbResponse(credits));
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieve release dates for a movie
|
||||
* @param {Number} identifier of the movie to get release dates for
|
||||
* @returns {Promise} movie release dates object
|
||||
*/
|
||||
movieReleaseDates(identifier) {
|
||||
const query = { id: identifier };
|
||||
const cacheKey = `tmdb/${this.cacheTags.movieReleaseDates}:${identifier}`;
|
||||
|
||||
return this.getFromCacheOrFetchFromTmdb(
|
||||
cacheKey,
|
||||
"movieReleaseDates",
|
||||
query
|
||||
)
|
||||
.then(releaseDates =>
|
||||
this.cache.set(cacheKey, releaseDates, this.defaultTTL)
|
||||
)
|
||||
.then(releaseDates => ReleaseDates.convertFromTmdbResponse(releaseDates));
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieve a specific show by id from TMDB.
|
||||
* @param {Number} identifier of the show you want to retrieve
|
||||
* @param {String} type filter results by type (default show).
|
||||
* @returns {Promise} succeeds if show was found
|
||||
*/
|
||||
showInfo(identifier) {
|
||||
const query = { id: identifier };
|
||||
const cacheKey = `tmdb/${this.cacheTags.showInfo}:${identifier}`;
|
||||
|
||||
return this.getFromCacheOrFetchFromTmdb(cacheKey, "tvInfo", query)
|
||||
.then(show => this.cache.set(cacheKey, show, this.defaultTTL))
|
||||
.then(show => Show.convertFromTmdbResponse(show));
|
||||
}
|
||||
|
||||
showCredits(identifier) {
|
||||
const query = { id: identifier };
|
||||
const cacheKey = `tmdb/${this.cacheTags.showCredits}:${identifier}`;
|
||||
|
||||
return this.getFromCacheOrFetchFromTmdb(cacheKey, "tvCredits", query)
|
||||
.then(credits => this.cache.set(cacheKey, credits, this.defaultTTL))
|
||||
.then(credits => Credits.convertFromTmdbResponse(credits));
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieve a specific person id from TMDB.
|
||||
* @param {Number} identifier of the person you want to retrieve
|
||||
* @param {String} type filter results by type (default person).
|
||||
* @returns {Promise} succeeds if person was found
|
||||
*/
|
||||
personInfo(identifier) {
|
||||
const query = { id: identifier };
|
||||
const cacheKey = `tmdb/${this.cacheTags.personInfo}:${identifier}`;
|
||||
|
||||
return this.getFromCacheOrFetchFromTmdb(cacheKey, "personInfo", query)
|
||||
.then(person => this.cache.set(cacheKey, person, this.defaultTTL))
|
||||
.then(person => Person.convertFromTmdbResponse(person));
|
||||
}
|
||||
|
||||
personCredits(identifier) {
|
||||
const query = { id: identifier };
|
||||
const cacheKey = `tmdb/${this.cacheTags.personCredits}:${identifier}`;
|
||||
|
||||
return this.getFromCacheOrFetchFromTmdb(
|
||||
cacheKey,
|
||||
"personCombinedCredits",
|
||||
query
|
||||
)
|
||||
.then(credits => this.cache.set(cacheKey, credits, this.defaultTTL))
|
||||
.then(credits => Credits.convertFromTmdbResponse(credits));
|
||||
}
|
||||
|
||||
multiSearch(search_query, page = 1, include_adult = true) {
|
||||
const query = { query: search_query, page, include_adult };
|
||||
const cacheKey = `tmdb/${this.cacheTags.multiSearch}:${page}:${search_query}:${include_adult}`;
|
||||
|
||||
return this.getFromCacheOrFetchFromTmdb(cacheKey, "searchMulti", query)
|
||||
.then(response => this.cache.set(cacheKey, response, this.defaultTTL))
|
||||
.then(response => this.mapResults(response));
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrive movie search results from TMDB.
|
||||
* @param {String} text query you want to search for
|
||||
* @param {Number} page representing pagination of results
|
||||
* @returns {Promise} dict with query results, current page and total_pages
|
||||
*/
|
||||
movieSearch(search_query, page = 1, include_adult = true) {
|
||||
const tmdbquery = { query: search_query, page, include_adult };
|
||||
const cacheKey = `tmdb/${this.cacheTags.movieSearch}:${page}:${search_query}:${include_adult}`;
|
||||
|
||||
return this.getFromCacheOrFetchFromTmdb(cacheKey, "searchMovie", tmdbquery)
|
||||
.then(response => this.cache.set(cacheKey, response, this.defaultTTL))
|
||||
.then(response => this.mapResults(response, "movie"));
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrive show search results from TMDB.
|
||||
* @param {String} text query you want to search for
|
||||
* @param {Number} page representing pagination of results
|
||||
* @returns {Promise} dict with query results, current page and total_pages
|
||||
*/
|
||||
showSearch(search_query, page = 1, include_adult = true) {
|
||||
const tmdbquery = { query: search_query, page, include_adult };
|
||||
const cacheKey = `tmdb/${this.cacheTags.showSearch}:${page}:${search_query}:${include_adult}`;
|
||||
|
||||
return this.getFromCacheOrFetchFromTmdb(cacheKey, "searchTv", tmdbquery)
|
||||
.then(response => this.cache.set(cacheKey, response, this.defaultTTL))
|
||||
.then(response => this.mapResults(response, "show"));
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrive person search results from TMDB.
|
||||
* @param {String} text query you want to search for
|
||||
* @param {Number} page representing pagination of results
|
||||
* @returns {Promise} dict with query results, current page and total_pages
|
||||
*/
|
||||
personSearch(search_query, page = 1, include_adult = true) {
|
||||
const tmdbquery = { query: search_query, page, include_adult };
|
||||
const cacheKey = `tmdb/${this.cacheTags.personSearch}:${page}:${search_query}:${include_adult}`;
|
||||
|
||||
return this.getFromCacheOrFetchFromTmdb(cacheKey, "searchPerson", tmdbquery)
|
||||
.then(response => this.cache.set(cacheKey, response, this.defaultTTL))
|
||||
.then(response => this.mapResults(response, "person"));
|
||||
}
|
||||
|
||||
movieList(listname, page = 1) {
|
||||
const query = { page: page };
|
||||
const cacheKey = `tmdb/${this.cacheTags[listname]}:${page}`;
|
||||
|
||||
return this.getFromCacheOrFetchFromTmdb(cacheKey, listname, query)
|
||||
.then(response => this.cache.set(cacheKey, response, this.defaultTTL))
|
||||
.then(response => this.mapResults(response, "movie"));
|
||||
}
|
||||
|
||||
showList(listname, page = 1) {
|
||||
const query = { page: page };
|
||||
const cacheKey = `tmdb/${this.cacheTags[listname]}:${page}`;
|
||||
|
||||
return this.getFromCacheOrFetchFromTmdb(cacheKey, listName, query)
|
||||
.then(response => this.cache.set(cacheKey, response, this.defaultTTL))
|
||||
.then(response => this.mapResults(response, "show"));
|
||||
}
|
||||
|
||||
/**
|
||||
* Maps our response from tmdb api to a movie/show object.
|
||||
* @param {String} response from tmdb.
|
||||
* @param {String} The type declared in listSearch.
|
||||
* @returns {Promise} dict with tmdb results, mapped as movie/show objects.
|
||||
*/
|
||||
mapResults(response, type = undefined) {
|
||||
let results = response.results.map(result => {
|
||||
if (type === "movie" || result.media_type === "movie") {
|
||||
const movie = Movie.convertFromTmdbResponse(result);
|
||||
return movie.createJsonResponse();
|
||||
} else if (type === "show" || result.media_type === "tv") {
|
||||
const show = Show.convertFromTmdbResponse(result);
|
||||
return show.createJsonResponse();
|
||||
} else if (type === "person" || result.media_type === "person") {
|
||||
const person = Person.convertFromTmdbResponse(result);
|
||||
return person.createJsonResponse();
|
||||
}
|
||||
});
|
||||
|
||||
return {
|
||||
results: results,
|
||||
page: response.page,
|
||||
total_results: response.total_results,
|
||||
total_pages: response.total_pages
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Wraps moviedb library to support Promises.
|
||||
* @param {String} method function name in the library
|
||||
* @param {Object} argument argument to function being called
|
||||
* @returns {Promise} succeeds if callback succeeds
|
||||
*/
|
||||
tmdb(method, argument) {
|
||||
return new Promise((resolve, reject) => {
|
||||
const callback = (error, reponse) => {
|
||||
if (error) {
|
||||
return reject(error);
|
||||
}
|
||||
resolve(reponse);
|
||||
};
|
||||
|
||||
if (!argument) {
|
||||
this.tmdbLibrary[method](callback);
|
||||
} else {
|
||||
this.tmdbLibrary[method](argument, callback);
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = TMDB;
|
||||
@@ -1,7 +0,0 @@
|
||||
import { Movie } from './types'
|
||||
|
||||
Movie('str', 123)
|
||||
|
||||
|
||||
|
||||
module.exports = TMDB;
|
||||
@@ -1,7 +0,0 @@
|
||||
const Movie = require('./types/movie.js')
|
||||
const Show = require('./types/show.js')
|
||||
const Person = require('./types/person.js')
|
||||
const Credits = require('./types/credits.js')
|
||||
const ReleaseDates = require('./types/releaseDates.js')
|
||||
|
||||
module.exports = { Movie, Show, Person, Credits, ReleaseDates }
|
||||
@@ -1,64 +0,0 @@
|
||||
interface Movie {
|
||||
adult: boolean;
|
||||
backdrop: string;
|
||||
genres: Genre[];
|
||||
id: number;
|
||||
imdb_id: number;
|
||||
overview: string;
|
||||
popularity: number;
|
||||
poster: string;
|
||||
release_date: Date;
|
||||
rank: number;
|
||||
runtime: number;
|
||||
status: string;
|
||||
tagline: string;
|
||||
title: string;
|
||||
vote_count: number;
|
||||
}
|
||||
|
||||
interface Show {
|
||||
adult: boolean;
|
||||
backdrop: string;
|
||||
episodes: number;
|
||||
genres: Genre[];
|
||||
id: number;
|
||||
imdb_id: number;
|
||||
overview: string;
|
||||
popularity: number;
|
||||
poster: string;
|
||||
rank: number;
|
||||
runtime: number;
|
||||
seasons: number;
|
||||
status: string;
|
||||
tagline: string;
|
||||
title: string;
|
||||
vote_count: number;
|
||||
}
|
||||
|
||||
interface Person {
|
||||
birthday: Date;
|
||||
deathday: Date;
|
||||
id: number;
|
||||
known_for: string;
|
||||
name: string;
|
||||
poster: string;
|
||||
}
|
||||
|
||||
interface SearchResult {
|
||||
adult: boolean;
|
||||
backdrop_path: string;
|
||||
id: number;
|
||||
original_title: string;
|
||||
release_date: Date;
|
||||
poster_path: string;
|
||||
popularity: number;
|
||||
vote_average: number;
|
||||
vote_counte: number;
|
||||
}
|
||||
|
||||
interface Genre {
|
||||
id: number;
|
||||
name: string;
|
||||
}
|
||||
|
||||
export { Movie, Show, Person, Genre }
|
||||
@@ -1,113 +0,0 @@
|
||||
import Movie from "./movie";
|
||||
import Show from "./show";
|
||||
|
||||
class Credits {
|
||||
constructor(id, cast = [], crew = []) {
|
||||
this.id = id;
|
||||
this.cast = cast;
|
||||
this.crew = crew;
|
||||
this.type = "credits";
|
||||
}
|
||||
|
||||
static convertFromTmdbResponse(response) {
|
||||
const { id, cast, crew } = response;
|
||||
|
||||
const allCast = cast.map(cast => {
|
||||
if (cast["media_type"]) {
|
||||
if (cast.media_type === "movie") {
|
||||
return CreditedMovie.convertFromTmdbResponse(cast);
|
||||
} else if (cast.media_type === "tv") {
|
||||
return CreditedShow.convertFromTmdbResponse(cast);
|
||||
}
|
||||
}
|
||||
|
||||
return new CastMember(
|
||||
cast.character,
|
||||
cast.gender,
|
||||
cast.id,
|
||||
cast.name,
|
||||
cast.profile_path
|
||||
);
|
||||
});
|
||||
|
||||
const allCrew = crew.map(crew => {
|
||||
if (cast["media_type"]) {
|
||||
if (cast.media_type === "movie") {
|
||||
return CreditedMovie.convertFromTmdbResponse(cast);
|
||||
} else if (cast.media_type === "tv") {
|
||||
return CreditedShow.convertFromTmdbResponse(cast);
|
||||
}
|
||||
}
|
||||
|
||||
return new CrewMember(
|
||||
crew.department,
|
||||
crew.gender,
|
||||
crew.id,
|
||||
crew.job,
|
||||
crew.name,
|
||||
crew.profile_path
|
||||
);
|
||||
});
|
||||
|
||||
return new Credits(id, allCast, allCrew);
|
||||
}
|
||||
|
||||
createJsonResponse() {
|
||||
return {
|
||||
id: this.id,
|
||||
cast: this.cast.map(cast => cast.createJsonResponse()),
|
||||
crew: this.crew.map(crew => crew.createJsonResponse())
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
class CastMember {
|
||||
constructor(character, gender, id, name, profile_path) {
|
||||
this.character = character;
|
||||
this.gender = gender;
|
||||
this.id = id;
|
||||
this.name = name;
|
||||
this.profile_path = profile_path;
|
||||
this.type = "person";
|
||||
}
|
||||
|
||||
createJsonResponse() {
|
||||
return {
|
||||
character: this.character,
|
||||
gender: this.gender,
|
||||
id: this.id,
|
||||
name: this.name,
|
||||
profile_path: this.profile_path,
|
||||
type: this.type
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
class CrewMember {
|
||||
constructor(department, gender, id, job, name, profile_path) {
|
||||
this.department = department;
|
||||
this.gender = gender;
|
||||
this.id = id;
|
||||
this.job = job;
|
||||
this.name = name;
|
||||
this.profile_path = profile_path;
|
||||
this.type = "person";
|
||||
}
|
||||
|
||||
createJsonResponse() {
|
||||
return {
|
||||
department: this.department,
|
||||
gender: this.gender,
|
||||
id: this.id,
|
||||
job: this.job,
|
||||
name: this.name,
|
||||
profile_path: this.profile_path,
|
||||
type: this.type
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
class CreditedMovie extends Movie {}
|
||||
class CreditedShow extends Show {}
|
||||
|
||||
module.exports = Credits;
|
||||
@@ -1,62 +0,0 @@
|
||||
class Movie {
|
||||
constructor(id, title, year=undefined, overview=undefined, poster=undefined, backdrop=undefined,
|
||||
releaseDate=undefined, rating=undefined, genres=undefined, productionStatus=undefined,
|
||||
tagline=undefined, runtime=undefined, imdb_id=undefined, popularity=undefined) {
|
||||
this.id = id;
|
||||
this.title = title;
|
||||
this.year = year;
|
||||
this.overview = overview;
|
||||
this.poster = poster;
|
||||
this.backdrop = backdrop;
|
||||
this.releaseDate = releaseDate;
|
||||
this.rating = rating;
|
||||
this.genres = genres;
|
||||
this.productionStatus = productionStatus;
|
||||
this.tagline = tagline;
|
||||
this.runtime = runtime;
|
||||
this.imdb_id = imdb_id;
|
||||
this.popularity = popularity;
|
||||
this.type = 'movie';
|
||||
}
|
||||
|
||||
static convertFromTmdbResponse(response) {
|
||||
const { id, title, release_date, overview, poster_path, backdrop_path, vote_average, genres, status,
|
||||
tagline, runtime, imdb_id, popularity } = response;
|
||||
|
||||
const releaseDate = new Date(release_date);
|
||||
const year = releaseDate.getFullYear();
|
||||
const genreNames = genres ? genres.map(g => g.name) : undefined
|
||||
|
||||
return new Movie(id, title, year, overview, poster_path, backdrop_path, releaseDate, vote_average, genreNames, status,
|
||||
tagline, runtime, imdb_id, popularity)
|
||||
}
|
||||
|
||||
static convertFromPlexResponse(response) {
|
||||
// console.log('response', response)
|
||||
const { title, year, rating, tagline, summary } = response;
|
||||
const _ = undefined
|
||||
|
||||
return new Movie(null, title, year, summary, _, _, _, rating, _, _, tagline)
|
||||
}
|
||||
|
||||
createJsonResponse() {
|
||||
return {
|
||||
id: this.id,
|
||||
title: this.title,
|
||||
year: this.year,
|
||||
overview: this.overview,
|
||||
poster: this.poster,
|
||||
backdrop: this.backdrop,
|
||||
release_date: this.releaseDate,
|
||||
rating: this.rating,
|
||||
genres: this.genres,
|
||||
production_status: this.productionStatus,
|
||||
tagline: this.tagline,
|
||||
runtime: this.runtime,
|
||||
imdb_id: this.imdb_id,
|
||||
type: this.type
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = Movie;
|
||||
@@ -1,70 +0,0 @@
|
||||
class Person {
|
||||
constructor(
|
||||
id,
|
||||
name,
|
||||
poster = undefined,
|
||||
birthday = undefined,
|
||||
deathday = undefined,
|
||||
adult = undefined,
|
||||
placeOfBirth = undefined,
|
||||
biography = undefined,
|
||||
knownForDepartment = undefined
|
||||
) {
|
||||
this.id = id;
|
||||
this.name = name;
|
||||
this.poster = poster;
|
||||
this.birthday = birthday;
|
||||
this.deathday = deathday;
|
||||
this.adult = adult;
|
||||
this.placeOfBirth = placeOfBirth;
|
||||
this.biography = biography;
|
||||
this.knownForDepartment = knownForDepartment;
|
||||
this.type = "person";
|
||||
}
|
||||
|
||||
static convertFromTmdbResponse(response) {
|
||||
const {
|
||||
id,
|
||||
name,
|
||||
profile_path,
|
||||
birthday,
|
||||
deathday,
|
||||
adult,
|
||||
place_of_birth,
|
||||
biography,
|
||||
known_for_department
|
||||
} = response;
|
||||
|
||||
const birthDay = new Date(birthday);
|
||||
const deathDay = deathday ? new Date(deathday) : null;
|
||||
|
||||
return new Person(
|
||||
id,
|
||||
name,
|
||||
profile_path,
|
||||
birthDay,
|
||||
deathDay,
|
||||
adult,
|
||||
place_of_birth,
|
||||
biography,
|
||||
known_for_department
|
||||
);
|
||||
}
|
||||
|
||||
createJsonResponse() {
|
||||
return {
|
||||
id: this.id,
|
||||
name: this.name,
|
||||
poster: this.poster,
|
||||
birthday: this.birthday,
|
||||
deathday: this.deathday,
|
||||
place_of_birth: this.placeOfBirth,
|
||||
biography: this.biography,
|
||||
known_for_department: this.knownForDepartment,
|
||||
adult: this.adult,
|
||||
type: this.type
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = Person;
|
||||
@@ -1,78 +0,0 @@
|
||||
class ReleaseDates {
|
||||
constructor(id, releases) {
|
||||
this.id = id;
|
||||
this.releases = releases;
|
||||
}
|
||||
|
||||
static convertFromTmdbResponse(response) {
|
||||
const { id, results } = response;
|
||||
|
||||
const releases = results.map(countryRelease =>
|
||||
new Release(
|
||||
countryRelease.iso_3166_1,
|
||||
countryRelease.release_dates.map(rd => new ReleaseDate(rd.certification, rd.iso_639_1, rd.release_date, rd.type, rd.note))
|
||||
))
|
||||
|
||||
return new ReleaseDates(id, releases)
|
||||
}
|
||||
|
||||
createJsonResponse() {
|
||||
return {
|
||||
id: this.id,
|
||||
results: this.releases.map(release => release.createJsonResponse())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
class Release {
|
||||
constructor(country, releaseDates) {
|
||||
this.country = country;
|
||||
this.releaseDates = releaseDates;
|
||||
}
|
||||
|
||||
createJsonResponse() {
|
||||
return {
|
||||
country: this.country,
|
||||
release_dates: this.releaseDates.map(releaseDate => releaseDate.createJsonResponse())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
class ReleaseDate {
|
||||
constructor(certification, language, releaseDate, type, note) {
|
||||
this.certification = certification;
|
||||
this.language = language;
|
||||
this.releaseDate = releaseDate;
|
||||
this.type = this.releaseTypeLookup(type);
|
||||
this.note = note;
|
||||
}
|
||||
|
||||
releaseTypeLookup(releaseTypeKey) {
|
||||
const releaseTypeEnum = {
|
||||
1: 'Premier',
|
||||
2: 'Limited theatrical',
|
||||
3: 'Theatrical',
|
||||
4: 'Digital',
|
||||
5: 'Physical',
|
||||
6: 'TV'
|
||||
}
|
||||
if (releaseTypeKey <= Object.keys(releaseTypeEnum).length) {
|
||||
return releaseTypeEnum[releaseTypeKey]
|
||||
} else {
|
||||
// TODO log | Release type not defined, does this need updating?
|
||||
return null
|
||||
}
|
||||
}
|
||||
|
||||
createJsonResponse() {
|
||||
return {
|
||||
certification: this.certification,
|
||||
language: this.language,
|
||||
release_date: this.releaseDate,
|
||||
type: this.type,
|
||||
note: this.note
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = ReleaseDates;
|
||||
@@ -1,50 +0,0 @@
|
||||
class Show {
|
||||
constructor(id, title, year=undefined, overview=undefined, poster=undefined, backdrop=undefined,
|
||||
seasons=undefined, episodes=undefined, rank=undefined, genres=undefined, status=undefined,
|
||||
runtime=undefined) {
|
||||
this.id = id;
|
||||
this.title = title;
|
||||
this.year = year;
|
||||
this.overview = overview;
|
||||
this.poster = poster;
|
||||
this.backdrop = backdrop;
|
||||
this.seasons = seasons;
|
||||
this.episodes = episodes;
|
||||
this.rank = rank;
|
||||
this.genres = genres;
|
||||
this.productionStatus = status;
|
||||
this.runtime = runtime;
|
||||
this.type = 'show';
|
||||
}
|
||||
|
||||
static convertFromTmdbResponse(response) {
|
||||
const { id, name, first_air_date, overview, poster_path, backdrop_path, number_of_seasons, number_of_episodes,
|
||||
rank, genres, status, episode_run_time, popularity } = response;
|
||||
|
||||
const year = new Date(first_air_date).getFullYear()
|
||||
const genreNames = genres ? genres.map(g => g.name) : undefined
|
||||
|
||||
return new Show(id, name, year, overview, poster_path, backdrop_path, number_of_seasons, number_of_episodes,
|
||||
rank, genreNames, status, episode_run_time, popularity)
|
||||
}
|
||||
|
||||
createJsonResponse() {
|
||||
return {
|
||||
id: this.id,
|
||||
title: this.title,
|
||||
year: this.year,
|
||||
overview: this.overview,
|
||||
poster: this.poster,
|
||||
backdrop: this.backdrop,
|
||||
seasons: this.seasons,
|
||||
episodes: this.episodes,
|
||||
rank: this.rank,
|
||||
genres: this.genres,
|
||||
production_status: this.productionStatus,
|
||||
runtime: this.runtime,
|
||||
type: this.type
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = Show;
|
||||
@@ -1,41 +0,0 @@
|
||||
const User = require("./user");
|
||||
const jwt = require("jsonwebtoken");
|
||||
|
||||
class Token {
|
||||
constructor(user, admin = false, settings = null) {
|
||||
this.user = user;
|
||||
this.admin = admin;
|
||||
this.settings = settings;
|
||||
}
|
||||
|
||||
/**
|
||||
* Generate a new token.
|
||||
* @param {String} secret a cipher of the token
|
||||
* @returns {String}
|
||||
*/
|
||||
toString(secret) {
|
||||
const { user, admin, settings } = this;
|
||||
|
||||
let data = { username: user.username, settings };
|
||||
if (admin) data["admin"] = admin;
|
||||
|
||||
return jwt.sign(data, secret, { expiresIn: "90d" });
|
||||
}
|
||||
|
||||
/**
|
||||
* Decode a token.
|
||||
* @param {Token} jwtToken an encrypted token
|
||||
* @param {String} secret a cipher of the token
|
||||
* @returns {Token}
|
||||
*/
|
||||
static fromString(jwtToken, secret) {
|
||||
const token = jwt.verify(jwtToken, secret, { clockTolerance: 10000 });
|
||||
if (token.username == null) throw new Error("Malformed token");
|
||||
|
||||
const { username, admin, settings } = token;
|
||||
const user = new User(username);
|
||||
return new Token(user, admin, settings);
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = Token;
|
||||
@@ -1,8 +0,0 @@
|
||||
class User {
|
||||
constructor(username, email=undefined) {
|
||||
this.username = username;
|
||||
this.email = email;
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = User;
|
||||
@@ -1,256 +0,0 @@
|
||||
const assert = require("assert");
|
||||
const establishedDatabase = require("../database/database");
|
||||
|
||||
class UserRepository {
|
||||
constructor(database) {
|
||||
this.database = database || establishedDatabase;
|
||||
this.queries = {
|
||||
read: "select * from user where lower(user_name) = lower(?)",
|
||||
create: "insert into user (user_name) values (?)",
|
||||
change: "update user set password = ? where user_name = ?",
|
||||
retrieveHash: "select * from user where user_name = ?",
|
||||
getAdminStateByUser: "select admin from user where user_name = ?",
|
||||
link: "update settings set plex_userid = ? where user_name = ?",
|
||||
unlink: "update settings set plex_userid = null where user_name = ?",
|
||||
createSettings: "insert into settings (user_name) values (?)",
|
||||
updateSettings:
|
||||
"update settings set user_name = ?, dark_mode = ?, emoji = ?",
|
||||
getSettings: "select * from settings where user_name = ?"
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a user in a database.
|
||||
* @param {User} user the user you want to create
|
||||
* @returns {Promise}
|
||||
*/
|
||||
create(user) {
|
||||
return this.database
|
||||
.get(this.queries.read, user.username)
|
||||
.then(() => this.database.run(this.queries.create, user.username))
|
||||
.catch(error => {
|
||||
if (
|
||||
error.name === "AssertionError" ||
|
||||
error.message.endsWith("user_name")
|
||||
) {
|
||||
throw new Error("That username is already registered");
|
||||
}
|
||||
throw Error(error);
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieve a password from a database.
|
||||
* @param {User} user the user you want to retrieve the password
|
||||
* @returns {Promise}
|
||||
*/
|
||||
retrieveHash(user) {
|
||||
return this.database
|
||||
.get(this.queries.retrieveHash, user.username)
|
||||
.then(row => {
|
||||
assert(row, "The user does not exist.");
|
||||
return row.password;
|
||||
})
|
||||
.catch(err => {
|
||||
console.log(error);
|
||||
throw new Error("Unable to find your user.");
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Change a user's password in a database.
|
||||
* @param {User} user the user you want to create
|
||||
* @param {String} password the new password you want to change
|
||||
* @returns {Promise}
|
||||
*/
|
||||
changePassword(user, password) {
|
||||
return this.database.run(this.queries.change, [password, user.username]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Link plex userid with seasoned user
|
||||
* @param {String} username the user you want to lunk plex userid with
|
||||
* @param {Number} plexUserID plex unique id
|
||||
* @returns {Promsie}
|
||||
*/
|
||||
linkPlexUserId(username, plexUserID) {
|
||||
return new Promise((resolve, reject) => {
|
||||
this.database
|
||||
.run(this.queries.link, [plexUserID, username])
|
||||
.then(row => resolve(row))
|
||||
.catch(error => {
|
||||
// TODO log this unknown db error
|
||||
console.error("db error", error);
|
||||
|
||||
reject({
|
||||
status: 500,
|
||||
message:
|
||||
"An unexpected error occured while linking plex and seasoned accounts",
|
||||
source: "seasoned database"
|
||||
});
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Unlink plex userid with seasoned user
|
||||
* @param {User} user the user you want to lunk plex userid with
|
||||
* @returns {Promsie}
|
||||
*/
|
||||
unlinkPlexUserId(username) {
|
||||
return new Promise((resolve, reject) => {
|
||||
this.database
|
||||
.run(this.queries.unlink, username)
|
||||
.then(row => resolve(row))
|
||||
.catch(error => {
|
||||
// TODO log this unknown db error
|
||||
console.log("db error", error);
|
||||
|
||||
reject({
|
||||
status: 500,
|
||||
message:
|
||||
"An unexpected error occured while unlinking plex and seasoned accounts",
|
||||
source: "seasoned database"
|
||||
});
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if the user has boolean flag set for admin in database
|
||||
* @param {User} user object
|
||||
* @returns {Promsie}
|
||||
*/
|
||||
checkAdmin(user) {
|
||||
return this.database
|
||||
.get(this.queries.getAdminStateByUser, user.username)
|
||||
.then(row => row.admin);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get settings for user matching string username
|
||||
* @param {String} username
|
||||
* @returns {Promsie}
|
||||
*/
|
||||
getSettings(username) {
|
||||
return new Promise((resolve, reject) => {
|
||||
this.database
|
||||
.get(this.queries.getSettings, username)
|
||||
.then(async row => {
|
||||
if (row == null) {
|
||||
console.debug(
|
||||
`settings do not exist for user: ${username}. Creating settings entry.`
|
||||
);
|
||||
|
||||
const userExistsWithUsername = await this.database.get(
|
||||
"select * from user where user_name is ?",
|
||||
username
|
||||
);
|
||||
if (userExistsWithUsername !== undefined) {
|
||||
try {
|
||||
resolve(this.dbCreateSettings(username));
|
||||
} catch (error) {
|
||||
reject(error);
|
||||
}
|
||||
} else {
|
||||
reject({
|
||||
status: 404,
|
||||
message: "User not found, no settings to get"
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
resolve(row);
|
||||
})
|
||||
.catch(error => {
|
||||
console.error(
|
||||
"Unexpected error occured while fetching settings for your account. Error:",
|
||||
error
|
||||
);
|
||||
reject({
|
||||
status: 500,
|
||||
message:
|
||||
"An unexpected error occured while fetching settings for your account",
|
||||
source: "seasoned database"
|
||||
});
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Update settings values for user matching string username
|
||||
* @param {String} username
|
||||
* @param {String} dark_mode
|
||||
* @param {String} emoji
|
||||
* @returns {Promsie}
|
||||
*/
|
||||
updateSettings(username, dark_mode = undefined, emoji = undefined) {
|
||||
const settings = this.getSettings(username);
|
||||
dark_mode = dark_mode !== undefined ? dark_mode : settings.dark_mode;
|
||||
emoji = emoji !== undefined ? emoji : settings.emoji;
|
||||
|
||||
return this.dbUpdateSettings(username, dark_mode, emoji).catch(error => {
|
||||
if (error.status && error.message) {
|
||||
return error;
|
||||
}
|
||||
|
||||
return {
|
||||
status: 500,
|
||||
message:
|
||||
"An unexpected error occured while updating settings for your account"
|
||||
};
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Helper function for creating settings in the database
|
||||
* @param {String} username
|
||||
* @returns {Promsie}
|
||||
*/
|
||||
dbCreateSettings(username) {
|
||||
return this.database
|
||||
.run(this.queries.createSettings, username)
|
||||
.then(() => this.database.get(this.queries.getSettings, username))
|
||||
.catch(error =>
|
||||
rejectUnexpectedDatabaseError(
|
||||
"Unexpected error occured while creating settings",
|
||||
503,
|
||||
error
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Helper function for updating settings in the database
|
||||
* @param {String} username
|
||||
* @returns {Promsie}
|
||||
*/
|
||||
dbUpdateSettings(username, dark_mode, emoji) {
|
||||
return new Promise((resolve, reject) =>
|
||||
this.database
|
||||
.run(this.queries.updateSettings, [username, dark_mode, emoji])
|
||||
.then(row => resolve(row))
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
const rejectUnexpectedDatabaseError = (
|
||||
message,
|
||||
status,
|
||||
error,
|
||||
reject = null
|
||||
) => {
|
||||
console.error(error);
|
||||
const body = {
|
||||
status,
|
||||
message,
|
||||
source: "seasoned database"
|
||||
};
|
||||
|
||||
if (reject == null) {
|
||||
return new Promise((resolve, reject) => reject(body));
|
||||
}
|
||||
reject(body);
|
||||
};
|
||||
|
||||
module.exports = UserRepository;
|
||||
@@ -1,75 +0,0 @@
|
||||
const bcrypt = require("bcrypt");
|
||||
const UserRepository = require("./userRepository");
|
||||
|
||||
class UserSecurity {
|
||||
constructor(database) {
|
||||
this.userRepository = new UserRepository(database);
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a new user in PlanFlix.
|
||||
* @param {User} user the new user you want to create
|
||||
* @param {String} clearPassword a password of the user
|
||||
* @returns {Promise}
|
||||
*/
|
||||
createNewUser(user, clearPassword) {
|
||||
if (user.username.trim() === "") {
|
||||
throw new Error("The username is empty.");
|
||||
} else if (clearPassword.trim() === "") {
|
||||
throw new Error("The password is empty.");
|
||||
} else {
|
||||
return this.userRepository
|
||||
.create(user)
|
||||
.then(() => UserSecurity.hashPassword(clearPassword))
|
||||
.then(hash => this.userRepository.changePassword(user, hash));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Login into PlanFlix.
|
||||
* @param {User} user the user you want to login
|
||||
* @param {String} clearPassword the user's password
|
||||
* @returns {Promise}
|
||||
*/
|
||||
login(user, clearPassword) {
|
||||
return this.userRepository
|
||||
.retrieveHash(user)
|
||||
.then(hash => UserSecurity.compareHashes(hash, clearPassword))
|
||||
.catch(() => {
|
||||
throw new Error("Incorrect username or password.");
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Compare between a password and a hash password from database.
|
||||
* @param {String} hash the hash password from database
|
||||
* @param {String} clearPassword the user's password
|
||||
* @returns {Promise}
|
||||
*/
|
||||
static compareHashes(hash, clearPassword) {
|
||||
return new Promise((resolve, reject) => {
|
||||
bcrypt.compare(clearPassword, hash, (error, match) => {
|
||||
if (match) resolve(true);
|
||||
reject(false);
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Hashes a password.
|
||||
* @param {String} clearPassword the user's password
|
||||
* @returns {Promise}
|
||||
*/
|
||||
static hashPassword(clearPassword) {
|
||||
return new Promise(resolve => {
|
||||
const saltRounds = 10;
|
||||
bcrypt.hash(clearPassword, saltRounds, (error, hash) => {
|
||||
if (error) reject(error);
|
||||
|
||||
resolve(hash);
|
||||
});
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = UserSecurity;
|
||||
@@ -1,239 +0,0 @@
|
||||
const express = require("express");
|
||||
const Raven = require("raven");
|
||||
const cookieParser = require("cookie-parser");
|
||||
const bodyParser = require("body-parser");
|
||||
|
||||
const configuration = require("../config/configuration").getInstance();
|
||||
|
||||
const reqTokenToUser = require("./middleware/reqTokenToUser");
|
||||
const mustBeAuthenticated = require("./middleware/mustBeAuthenticated");
|
||||
const mustBeAdmin = require("./middleware/mustBeAdmin");
|
||||
const mustHaveAccountLinkedToPlex = require("./middleware/mustHaveAccountLinkedToPlex");
|
||||
|
||||
const listController = require("./controllers/list/listController");
|
||||
const tautulli = require("./controllers/user/viewHistory.js");
|
||||
const SettingsController = require("./controllers/user/settings");
|
||||
const AuthenticatePlexAccountController = require("./controllers/user/authenticatePlexAccount");
|
||||
|
||||
// TODO: Have our raven router check if there is a value, if not don't enable raven.
|
||||
Raven.config(configuration.get("raven", "DSN")).install();
|
||||
|
||||
const app = express(); // define our app using express
|
||||
app.use(Raven.requestHandler());
|
||||
app.use(bodyParser.json());
|
||||
app.use(cookieParser());
|
||||
|
||||
const router = express.Router();
|
||||
const allowedOrigins = configuration.get("webserver", "origins");
|
||||
|
||||
// TODO: All JSON handling in a single router
|
||||
// router.use(bodyParser.json());
|
||||
app.use(bodyParser.urlencoded({ extended: true }));
|
||||
|
||||
/* Check header and cookie for authentication and set req.loggedInUser */
|
||||
router.use(reqTokenToUser);
|
||||
|
||||
// TODO: Should have a separate middleware/router for handling headers.
|
||||
router.use((req, res, next) => {
|
||||
// TODO add logging of all incoming
|
||||
// const origin = req.headers.origin;
|
||||
// if (allowedOrigins.indexOf(origin) > -1) {
|
||||
// res.setHeader("Access-Control-Allow-Origin", origin);
|
||||
// }
|
||||
|
||||
res.header(
|
||||
"Access-Control-Allow-Headers",
|
||||
"Content-Type, Authorization, loggedinuser, set-cookie"
|
||||
);
|
||||
|
||||
res.header("Access-Control-Allow-Credentials", "true");
|
||||
res.header("Access-Control-Allow-Methods", "POST, GET, PUT, OPTIONS");
|
||||
|
||||
next();
|
||||
});
|
||||
|
||||
router.get("/", (req, res) => {
|
||||
res.send("welcome to seasoned api");
|
||||
});
|
||||
|
||||
app.use(Raven.errorHandler());
|
||||
app.use((err, req, res, next) => {
|
||||
res.statusCode = 500;
|
||||
res.end(res.sentry + "\n");
|
||||
});
|
||||
|
||||
/**
|
||||
* User
|
||||
*/
|
||||
router.post("/v1/user", require("./controllers/user/register.js"));
|
||||
router.post("/v1/user/login", require("./controllers/user/login.js"));
|
||||
router.post("/v1/user/logout", require("./controllers/user/logout.js"));
|
||||
router.get(
|
||||
"/v1/user/settings",
|
||||
mustBeAuthenticated,
|
||||
SettingsController.getSettingsController
|
||||
);
|
||||
router.put(
|
||||
"/v1/user/settings",
|
||||
mustBeAuthenticated,
|
||||
SettingsController.updateSettingsController
|
||||
);
|
||||
router.get(
|
||||
"/v1/user/search_history",
|
||||
mustBeAuthenticated,
|
||||
require("./controllers/user/searchHistory.js")
|
||||
);
|
||||
router.get(
|
||||
"/v1/user/requests",
|
||||
mustBeAuthenticated,
|
||||
require("./controllers/user/requests.js")
|
||||
);
|
||||
router.post(
|
||||
"/v1/user/link_plex",
|
||||
mustBeAuthenticated,
|
||||
AuthenticatePlexAccountController.link
|
||||
);
|
||||
router.post(
|
||||
"/v1/user/unlink_plex",
|
||||
mustBeAuthenticated,
|
||||
AuthenticatePlexAccountController.unlink
|
||||
);
|
||||
|
||||
router.get(
|
||||
"/v1/user/view_history",
|
||||
mustHaveAccountLinkedToPlex,
|
||||
tautulli.userViewHistoryController
|
||||
);
|
||||
router.get(
|
||||
"/v1/user/watch_time",
|
||||
mustHaveAccountLinkedToPlex,
|
||||
tautulli.watchTimeStatsController
|
||||
);
|
||||
router.get(
|
||||
"/v1/user/plays_by_day",
|
||||
mustHaveAccountLinkedToPlex,
|
||||
tautulli.getPlaysByDaysController
|
||||
);
|
||||
router.get(
|
||||
"/v1/user/plays_by_dayofweek",
|
||||
mustHaveAccountLinkedToPlex,
|
||||
tautulli.getPlaysByDayOfWeekController
|
||||
);
|
||||
|
||||
/**
|
||||
* Seasoned
|
||||
*/
|
||||
router.get("/v1/seasoned/all", require("./controllers/seasoned/readStrays.js"));
|
||||
router.get(
|
||||
"/v1/seasoned/:strayId",
|
||||
require("./controllers/seasoned/strayById.js")
|
||||
);
|
||||
router.post(
|
||||
"/v1/seasoned/verify/:strayId",
|
||||
require("./controllers/seasoned/verifyStray.js")
|
||||
);
|
||||
|
||||
router.get("/v2/search/", require("./controllers/search/multiSearch.js"));
|
||||
router.get("/v2/search/movie", require("./controllers/search/movieSearch.js"));
|
||||
router.get("/v2/search/show", require("./controllers/search/showSearch.js"));
|
||||
router.get(
|
||||
"/v2/search/person",
|
||||
require("./controllers/search/personSearch.js")
|
||||
);
|
||||
|
||||
router.get("/v2/movie/now_playing", listController.nowPlayingMovies);
|
||||
router.get("/v2/movie/popular", listController.popularMovies);
|
||||
router.get("/v2/movie/top_rated", listController.topRatedMovies);
|
||||
router.get("/v2/movie/upcoming", listController.upcomingMovies);
|
||||
router.get("/v2/movie/:id/credits", require("./controllers/movie/credits.js"));
|
||||
router.get(
|
||||
"/v2/movie/:id/release_dates",
|
||||
require("./controllers/movie/releaseDates.js")
|
||||
);
|
||||
router.get("/v2/movie/:id", require("./controllers/movie/info.js"));
|
||||
|
||||
router.get("/v2/show/now_playing", listController.nowPlayingShows);
|
||||
router.get("/v2/show/popular", listController.popularShows);
|
||||
router.get("/v2/show/top_rated", listController.topRatedShows);
|
||||
router.get("/v2/show/:id/credits", require("./controllers/show/credits.js"));
|
||||
router.get("/v2/show/:id", require("./controllers/show/info.js"));
|
||||
|
||||
router.get(
|
||||
"/v2/person/:id/credits",
|
||||
require("./controllers/person/credits.js")
|
||||
);
|
||||
router.get("/v2/person/:id", require("./controllers/person/info.js"));
|
||||
|
||||
/**
|
||||
* Plex
|
||||
*/
|
||||
router.get("/v2/plex/search", require("./controllers/plex/search"));
|
||||
|
||||
/**
|
||||
* List
|
||||
*/
|
||||
router.get("/v1/plex/search", require("./controllers/plex/searchMedia.js"));
|
||||
router.get("/v1/plex/playing", require("./controllers/plex/plexPlaying.js"));
|
||||
router.get("/v1/plex/request", require("./controllers/plex/searchRequest.js"));
|
||||
router.get(
|
||||
"/v1/plex/request/:mediaId",
|
||||
require("./controllers/plex/readRequest.js")
|
||||
);
|
||||
router.post(
|
||||
"/v1/plex/request/:mediaId",
|
||||
require("./controllers/plex/submitRequest.js")
|
||||
);
|
||||
router.post("/v1/plex/hook", require("./controllers/plex/hookDump.js"));
|
||||
|
||||
router.get(
|
||||
"/v1/plex/watch-link",
|
||||
mustBeAuthenticated,
|
||||
require("./controllers/plex/watchDirectLink.js")
|
||||
);
|
||||
|
||||
/**
|
||||
* Requests
|
||||
*/
|
||||
|
||||
router.get("/v2/request", require("./controllers/request/fetchAllRequests.js"));
|
||||
router.get("/v2/request/:id", require("./controllers/request/getRequest.js"));
|
||||
router.post("/v2/request", require("./controllers/request/requestTmdbId.js"));
|
||||
router.get(
|
||||
"/v1/plex/requests/all",
|
||||
require("./controllers/plex/fetchRequested.js")
|
||||
);
|
||||
router.put(
|
||||
"/v1/plex/request/:requestId",
|
||||
mustBeAuthenticated,
|
||||
require("./controllers/plex/updateRequested.js")
|
||||
);
|
||||
|
||||
/**
|
||||
* Pirate
|
||||
*/
|
||||
router.get(
|
||||
"/v1/pirate/search",
|
||||
mustBeAuthenticated,
|
||||
require("./controllers/pirate/searchTheBay.js")
|
||||
);
|
||||
router.post(
|
||||
"/v1/pirate/add",
|
||||
mustBeAuthenticated,
|
||||
require("./controllers/pirate/addMagnet.js")
|
||||
);
|
||||
|
||||
/**
|
||||
* git
|
||||
*/
|
||||
router.post("/v1/git/dump", require("./controllers/git/dumpHook.js"));
|
||||
|
||||
/**
|
||||
* misc
|
||||
*/
|
||||
router.get("/v1/emoji", require("./controllers/misc/emoji.js"));
|
||||
|
||||
// REGISTER OUR ROUTES -------------------------------
|
||||
// all of our routes will be prefixed with /api
|
||||
app.use("/api", router);
|
||||
|
||||
module.exports = app;
|
||||
@@ -1,16 +0,0 @@
|
||||
const GitRepository = require("../../../git/gitRepository");
|
||||
|
||||
const gitRepository = new GitRepository();
|
||||
|
||||
function dumpHookController(req, res) {
|
||||
gitRepository
|
||||
.dumpHook(req.body)
|
||||
.then(() => {
|
||||
res.status(200);
|
||||
})
|
||||
.catch(error => {
|
||||
res.status(500);
|
||||
});
|
||||
}
|
||||
|
||||
module.exports = dumpHookController;
|
||||
@@ -1,81 +0,0 @@
|
||||
const configuration = require("../../../config/configuration").getInstance();
|
||||
const TMDB = require("../../../tmdb/tmdb");
|
||||
const tmdb = new TMDB(configuration.get("tmdb", "apiKey"));
|
||||
|
||||
// there should be a translate function from query params to
|
||||
// tmdb list that is valid. Should it be a helper function or does it
|
||||
// belong in tmdb.
|
||||
// + could also have default value that are sent to the client.
|
||||
// * have the same class create a getListNames() and a fetchList()
|
||||
// * dicover list might be overkill_https://tinyurl.com/y7f8ragw
|
||||
// + trending! https://tinyurl.com/ydywrqox
|
||||
// by all, mediatype, or person. Can also define time periode to
|
||||
// get more trending view of what people are checking out.
|
||||
// + newly created (tv/latest).
|
||||
// + movie/latest
|
||||
//
|
||||
function handleError(error, res) {
|
||||
const { status, message } = error;
|
||||
|
||||
if (status && message) {
|
||||
res.status(status).send({ success: false, message });
|
||||
} else {
|
||||
console.log("caught list controller error", error);
|
||||
res
|
||||
.status(500)
|
||||
.send({ message: "An unexpected error occured while requesting list" });
|
||||
}
|
||||
}
|
||||
|
||||
function handleListResponse(response, res) {
|
||||
return res.send(response).catch(error => handleError(error, res));
|
||||
}
|
||||
|
||||
function fetchTmdbList(req, res, listname, type) {
|
||||
const { page } = req.query;
|
||||
|
||||
if (type === "movie") {
|
||||
return tmdb
|
||||
.movieList(listname, page)
|
||||
.then(listResponse => res.send(listResponse))
|
||||
.catch(error => handleError(error, res));
|
||||
} else if (type === "show") {
|
||||
return tmdb
|
||||
.showList(listname, page)
|
||||
.then(listResponse => res.send(listResponse))
|
||||
.catch(error => handleError(error, res));
|
||||
}
|
||||
|
||||
handleError(
|
||||
{
|
||||
status: 400,
|
||||
message: `'${type}' is not a valid list type.`
|
||||
},
|
||||
res
|
||||
);
|
||||
}
|
||||
|
||||
const nowPlayingMovies = (req, res) =>
|
||||
fetchTmdbList(req, res, "miscNowPlayingMovies", "movie");
|
||||
const popularMovies = (req, res) =>
|
||||
fetchTmdbList(req, res, "miscPopularMovies", "movie");
|
||||
const topRatedMovies = (req, res) =>
|
||||
fetchTmdbList(req, res, "miscTopRatedMovies", "movie");
|
||||
const upcomingMovies = (req, res) =>
|
||||
fetchTmdbList(req, res, "miscUpcomingMovies", "movie");
|
||||
const nowPlayingShows = (req, res) =>
|
||||
fetchTmdbList(req, res, "tvOnTheAir", "show");
|
||||
const popularShows = (req, res) =>
|
||||
fetchTmdbList(req, res, "miscPopularTvs", "show");
|
||||
const topRatedShows = (req, res) =>
|
||||
fetchTmdbList(req, res, "miscTopRatedTvs", "show");
|
||||
|
||||
module.exports = {
|
||||
nowPlayingMovies,
|
||||
popularMovies,
|
||||
topRatedMovies,
|
||||
upcomingMovies,
|
||||
nowPlayingShows,
|
||||
popularShows,
|
||||
topRatedShows
|
||||
};
|
||||
File diff suppressed because one or more lines are too long
@@ -1,30 +0,0 @@
|
||||
const configuration = require("../../../config/configuration").getInstance();
|
||||
const TMDB = require("../../../tmdb/tmdb");
|
||||
|
||||
const tmdb = new TMDB(configuration.get("tmdb", "apiKey"));
|
||||
|
||||
const movieCreditsController = (req, res) => {
|
||||
const movieId = req.params.id;
|
||||
|
||||
tmdb
|
||||
.movieCredits(movieId)
|
||||
.then(credits => res.send(credits.createJsonResponse()))
|
||||
.catch(error => {
|
||||
const { status, message } = error;
|
||||
|
||||
if (status && message) {
|
||||
res.status(status).send({ success: false, message });
|
||||
} else {
|
||||
// TODO log unhandled errors
|
||||
console.log("caugth movie credits controller error", error);
|
||||
res
|
||||
.status(500)
|
||||
.send({
|
||||
message:
|
||||
"An unexpected error occured while requesting movie credits"
|
||||
});
|
||||
}
|
||||
});
|
||||
};
|
||||
|
||||
module.exports = movieCreditsController;
|
||||
@@ -1,71 +0,0 @@
|
||||
const configuration = require("../../../config/configuration").getInstance();
|
||||
const TMDB = require("../../../tmdb/tmdb");
|
||||
const Plex = require("../../../plex/plex");
|
||||
const tmdb = new TMDB(configuration.get("tmdb", "apiKey"));
|
||||
const plex = new Plex(configuration.get("plex", "ip"));
|
||||
|
||||
function handleError(error, res) {
|
||||
const { status, message } = error;
|
||||
|
||||
if (status && message) {
|
||||
res.status(status).send({ success: false, message });
|
||||
} else {
|
||||
console.log("caught movieinfo controller error", error);
|
||||
res.status(500).send({
|
||||
message: "An unexpected error occured while requesting movie info"
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Controller: Retrieve information for a movie
|
||||
* @param {Request} req http request variable
|
||||
* @param {Response} res
|
||||
* @returns {Callback}
|
||||
*/
|
||||
async function movieInfoController(req, res) {
|
||||
const movieId = req.params.id;
|
||||
let { credits, release_dates, check_existance } = req.query;
|
||||
|
||||
credits && credits.toLowerCase() === "true"
|
||||
? (credits = true)
|
||||
: (credits = false);
|
||||
release_dates && release_dates.toLowerCase() === "true"
|
||||
? (release_dates = true)
|
||||
: (release_dates = false);
|
||||
check_existance && check_existance.toLowerCase() === "true"
|
||||
? (check_existance = true)
|
||||
: (check_existance = false);
|
||||
|
||||
let tmdbQueue = [tmdb.movieInfo(movieId)];
|
||||
if (credits) tmdbQueue.push(tmdb.movieCredits(movieId));
|
||||
if (release_dates) tmdbQueue.push(tmdb.movieReleaseDates(movieId));
|
||||
|
||||
try {
|
||||
const [Movie, Credits, ReleaseDates] = await Promise.all(tmdbQueue);
|
||||
|
||||
const movie = Movie.createJsonResponse();
|
||||
if (Credits) movie.credits = Credits.createJsonResponse();
|
||||
if (ReleaseDates)
|
||||
movie.release_dates = ReleaseDates.createJsonResponse().results;
|
||||
|
||||
if (check_existance) {
|
||||
try {
|
||||
movie.exists_in_plex = await plex.existsInPlex(movie);
|
||||
} catch (error) {
|
||||
if (error.status === 401) {
|
||||
console.log("Unathorized request, check plex server LAN settings");
|
||||
} else {
|
||||
console.log("Unkown error from plex!");
|
||||
}
|
||||
console.log(error?.message);
|
||||
}
|
||||
}
|
||||
|
||||
res.send(movie);
|
||||
} catch (error) {
|
||||
handleError(error, res);
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = movieInfoController;
|
||||
@@ -1,30 +0,0 @@
|
||||
const configuration = require("../../../config/configuration").getInstance();
|
||||
const TMDB = require("../../../tmdb/tmdb");
|
||||
|
||||
const tmdb = new TMDB(configuration.get("tmdb", "apiKey"));
|
||||
|
||||
const movieReleaseDatesController = (req, res) => {
|
||||
const movieId = req.params.id;
|
||||
|
||||
tmdb
|
||||
.movieReleaseDates(movieId)
|
||||
.then(releaseDates => res.send(releaseDates.createJsonResponse()))
|
||||
.catch(error => {
|
||||
const { status, message } = error;
|
||||
|
||||
if (status && message) {
|
||||
res.status(status).send({ success: false, message });
|
||||
} else {
|
||||
// TODO log unhandled errors : here our at tmdbReleaseError ?
|
||||
console.log("caugth release dates controller error", error);
|
||||
res
|
||||
.status(500)
|
||||
.send({
|
||||
message:
|
||||
"An unexpected error occured while requesting movie credits"
|
||||
});
|
||||
}
|
||||
});
|
||||
};
|
||||
|
||||
module.exports = movieReleaseDatesController;
|
||||
@@ -1,26 +0,0 @@
|
||||
const configuration = require("../../../config/configuration").getInstance();
|
||||
const TMDB = require("../../../tmdb/tmdb");
|
||||
const tmdb = new TMDB(configuration.get("tmdb", "apiKey"));
|
||||
|
||||
const personCreditsController = (req, res) => {
|
||||
const personId = req.params.id;
|
||||
|
||||
return tmdb
|
||||
.personCredits(personId)
|
||||
.then(credits => res.send(credits))
|
||||
.catch(error => {
|
||||
const { status, message } = error;
|
||||
|
||||
if (status && message) {
|
||||
res.status(status).send({ success: false, message });
|
||||
} else {
|
||||
// TODO log unhandled errors
|
||||
console.log("caugth show credits controller error", error);
|
||||
res.status(500).send({
|
||||
message: "An unexpected error occured while requesting person credits"
|
||||
});
|
||||
}
|
||||
});
|
||||
};
|
||||
|
||||
module.exports = personCreditsController;
|
||||
@@ -1,49 +0,0 @@
|
||||
const configuration = require("../../../config/configuration").getInstance();
|
||||
const TMDB = require("../../../tmdb/tmdb");
|
||||
const tmdb = new TMDB(configuration.get("tmdb", "apiKey"));
|
||||
|
||||
function handleError(error, res) {
|
||||
const { status, message } = error;
|
||||
|
||||
if (status && message) {
|
||||
res.status(status).send({ success: false, message });
|
||||
} else {
|
||||
console.log("caught personinfo controller error", error);
|
||||
res.status(500).send({
|
||||
message: "An unexpected error occured while requesting person info."
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Controller: Retrieve information for a person
|
||||
* @param {Request} req http request variable
|
||||
* @param {Response} res
|
||||
* @returns {Callback}
|
||||
*/
|
||||
|
||||
async function personInfoController(req, res) {
|
||||
const personId = req.params.id;
|
||||
let { credits } = req.query;
|
||||
arguments;
|
||||
|
||||
credits && credits.toLowerCase() === "true"
|
||||
? (credits = true)
|
||||
: (credits = false);
|
||||
|
||||
let tmdbQueue = [tmdb.personInfo(personId)];
|
||||
if (credits) tmdbQueue.push(tmdb.personCredits(personId));
|
||||
|
||||
try {
|
||||
const [Person, Credits] = await Promise.all(tmdbQueue);
|
||||
|
||||
const person = Person.createJsonResponse();
|
||||
if (credits) person.credits = Credits.createJsonResponse();
|
||||
|
||||
return res.send(person);
|
||||
} catch (error) {
|
||||
handleError(error, res);
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = personInfoController;
|
||||
@@ -1,24 +0,0 @@
|
||||
/*
|
||||
* @Author: KevinMidboe
|
||||
* @Date: 2017-10-21 09:54:31
|
||||
* @Last Modified by: KevinMidboe
|
||||
* @Last Modified time: 2017-10-21 15:32:43
|
||||
*/
|
||||
|
||||
const PirateRepository = require("../../../pirate/pirateRepository");
|
||||
|
||||
function addMagnet(req, res) {
|
||||
const magnet = req.body.magnet;
|
||||
const name = req.body.name;
|
||||
const tmdb_id = req.body.tmdb_id;
|
||||
|
||||
PirateRepository.AddMagnet(magnet, name, tmdb_id)
|
||||
.then(result => {
|
||||
res.send(result);
|
||||
})
|
||||
.catch(error => {
|
||||
res.status(500).send({ success: false, message: error.message });
|
||||
});
|
||||
}
|
||||
|
||||
module.exports = addMagnet;
|
||||
@@ -1,29 +0,0 @@
|
||||
/*
|
||||
* @Author: KevinMidboe
|
||||
* @Date: 2017-10-21 09:54:31
|
||||
* @Last Modified by: KevinMidboe
|
||||
* @Last Modified time: 2018-02-26 19:56:32
|
||||
*/
|
||||
|
||||
const PirateRepository = require("../../../pirate/pirateRepository");
|
||||
// const pirateRepository = new PirateRepository();
|
||||
|
||||
/**
|
||||
* Controller: Retrieves search history of a logged in user
|
||||
* @param {Request} req http request variable
|
||||
* @param {Response} res
|
||||
* @returns {Callback}
|
||||
*/
|
||||
function updateRequested(req, res) {
|
||||
const { query, page, type } = req.query;
|
||||
|
||||
PirateRepository.SearchPiratebay(query, page, type)
|
||||
.then(result => {
|
||||
res.send({ success: true, results: result });
|
||||
})
|
||||
.catch(error => {
|
||||
res.status(401).send({ success: false, message: error.message });
|
||||
});
|
||||
}
|
||||
|
||||
module.exports = updateRequested;
|
||||
@@ -1,29 +0,0 @@
|
||||
const RequestRepository = require("../../../plex/requestRepository.js");
|
||||
|
||||
const requestRepository = new RequestRepository();
|
||||
|
||||
/**
|
||||
* Controller: Retrieves search history of a logged in user
|
||||
* @param {Request} req http request variable
|
||||
* @param {Response} res
|
||||
* @returns {Callback}
|
||||
*/
|
||||
function fetchRequestedController(req, res) {
|
||||
// const user = req.loggedInUser;
|
||||
const { status, page } = req.query;
|
||||
|
||||
requestRepository
|
||||
.fetchRequested(status, page)
|
||||
.then(requestedItems => {
|
||||
res.send({
|
||||
success: true,
|
||||
results: requestedItems,
|
||||
total_results: requestedItems.length
|
||||
});
|
||||
})
|
||||
.catch(error => {
|
||||
res.status(401).send({ success: false, message: error.message });
|
||||
});
|
||||
}
|
||||
|
||||
module.exports = fetchRequestedController;
|
||||
@@ -1,12 +0,0 @@
|
||||
/*
|
||||
* @Author: KevinMidboe
|
||||
* @Date: 2017-05-03 23:26:46
|
||||
* @Last Modified by: KevinMidboe
|
||||
* @Last Modified time: 2018-02-06 20:54:22
|
||||
*/
|
||||
|
||||
function hookDumpController(req, res) {
|
||||
console.log(req);
|
||||
}
|
||||
|
||||
module.exports = hookDumpController;
|
||||
@@ -1,17 +0,0 @@
|
||||
const PlexRepository = require("../../../plex/plexRepository");
|
||||
const configuration = require("../../../config/configuration").getInstance();
|
||||
|
||||
const plexRepository = new PlexRepository(configuration.get("plex", "ip"));
|
||||
|
||||
function playingController(req, res) {
|
||||
plexRepository
|
||||
.nowPlaying()
|
||||
.then(movies => {
|
||||
res.send(movies);
|
||||
})
|
||||
.catch(error => {
|
||||
res.status(500).send({ success: false, message: error.message });
|
||||
});
|
||||
}
|
||||
|
||||
module.exports = playingController;
|
||||
@@ -1,24 +0,0 @@
|
||||
const RequestRepository = require("../../../plex/requestRepository");
|
||||
|
||||
const requestRepository = new RequestRepository();
|
||||
|
||||
/**
|
||||
* Controller: Retrieve information for a movie
|
||||
* @param {Request} req http request variable
|
||||
* @param {Response} res
|
||||
* @returns {Callback}
|
||||
*/
|
||||
function readRequestController(req, res) {
|
||||
const mediaId = req.params.mediaId;
|
||||
const { type } = req.query;
|
||||
requestRepository
|
||||
.lookup(mediaId, type)
|
||||
.then(movies => {
|
||||
res.send(movies);
|
||||
})
|
||||
.catch(error => {
|
||||
res.status(404).send({ success: false, message: error.message });
|
||||
});
|
||||
}
|
||||
|
||||
module.exports = readRequestController;
|
||||
@@ -1,32 +0,0 @@
|
||||
const configuration = require("../../../config/configuration").getInstance();
|
||||
const Plex = require("../../../plex/plex");
|
||||
const plex = new Plex(configuration.get("plex", "ip"));
|
||||
|
||||
/**
|
||||
* Controller: Search plex for movies, shows and episodes by query
|
||||
* @param {Request} req http request variable
|
||||
* @param {Response} res
|
||||
* @returns {Callback}
|
||||
*/
|
||||
function searchPlexController(req, res) {
|
||||
const { query, type } = req.query;
|
||||
plex
|
||||
.search(query, type)
|
||||
.then(movies => {
|
||||
if (movies.length > 0) {
|
||||
res.send(movies);
|
||||
} else {
|
||||
res
|
||||
.status(404)
|
||||
.send({
|
||||
success: false,
|
||||
message: "Search query did not give any results from plex."
|
||||
});
|
||||
}
|
||||
})
|
||||
.catch(error => {
|
||||
res.status(500).send({ success: false, message: error.message });
|
||||
});
|
||||
}
|
||||
|
||||
module.exports = searchPlexController;
|
||||
@@ -1,35 +0,0 @@
|
||||
const PlexRepository = require("../../../plex/plexRepository");
|
||||
const configuration = require("../../../config/configuration").getInstance();
|
||||
|
||||
const plexRepository = new PlexRepository(configuration.get("plex", "ip"));
|
||||
|
||||
/**
|
||||
* Controller: Search for media and check existence
|
||||
* in plex by query and page
|
||||
* @param {Request} req http request variable
|
||||
* @param {Response} res
|
||||
* @returns {Callback}
|
||||
*/
|
||||
function searchMediaController(req, res) {
|
||||
const { query } = req.query;
|
||||
|
||||
plexRepository
|
||||
.search(query)
|
||||
.then(media => {
|
||||
if (media !== undefined || media.length > 0) {
|
||||
res.send(media);
|
||||
} else {
|
||||
res
|
||||
.status(404)
|
||||
.send({
|
||||
success: false,
|
||||
message: "Search query did not return any results."
|
||||
});
|
||||
}
|
||||
})
|
||||
.catch(error => {
|
||||
res.status(500).send({ success: false, message: error.message });
|
||||
});
|
||||
}
|
||||
|
||||
module.exports = searchMediaController;
|
||||
@@ -1,24 +0,0 @@
|
||||
const SearchHistory = require("../../../searchHistory/searchHistory");
|
||||
const Cache = require("../../../tmdb/cache");
|
||||
const RequestRepository = require("../../../plex/requestRepository.js");
|
||||
|
||||
const cache = new Cache();
|
||||
const requestRepository = new RequestRepository(cache);
|
||||
const searchHistory = new SearchHistory();
|
||||
|
||||
function searchRequestController(req, res) {
|
||||
const { query, page, type } = req.query;
|
||||
const username = req.loggedInUser ? req.loggedInUser.username : null;
|
||||
|
||||
Promise.resolve()
|
||||
.then(() => searchHistory.create(username, query))
|
||||
.then(() => requestRepository.search(query, page, type))
|
||||
.then(searchResult => {
|
||||
res.send(searchResult);
|
||||
})
|
||||
.catch(error => {
|
||||
res.status(500).send({ success: false, message: error.message });
|
||||
});
|
||||
}
|
||||
|
||||
module.exports = searchRequestController;
|
||||
@@ -1,61 +0,0 @@
|
||||
const configuration = require("../../../config/configuration").getInstance();
|
||||
const RequestRepository = require("../../../request/request");
|
||||
const TMDB = require("../../../tmdb/tmdb");
|
||||
const tmdb = new TMDB(configuration.get("tmdb", "apiKey"));
|
||||
const request = new RequestRepository();
|
||||
|
||||
const tmdbMovieInfo = id => {
|
||||
return tmdb.movieInfo(id);
|
||||
};
|
||||
|
||||
const tmdbShowInfo = id => {
|
||||
return tmdb.showInfo(id);
|
||||
};
|
||||
|
||||
/**
|
||||
* Controller: POST a media id to be donwloaded
|
||||
* @param {Request} req http request variable
|
||||
* @param {Response} res
|
||||
* @returns {Callback}
|
||||
*/
|
||||
function submitRequestController(req, res) {
|
||||
// This is the id that is the param of the url
|
||||
const id = req.params.mediaId;
|
||||
const type = req.query.type ? req.query.type.toLowerCase() : undefined;
|
||||
const ip = req.headers["x-forwarded-for"] || req.connection.remoteAddress;
|
||||
const user_agent = req.headers["user-agent"];
|
||||
const username = req.loggedInUser ? req.loggedInUser.username : null;
|
||||
|
||||
let mediaFunction = undefined;
|
||||
|
||||
if (type === "movie") {
|
||||
console.log("movie");
|
||||
mediaFunction = tmdbMovieInfo;
|
||||
} else if (type === "show") {
|
||||
console.log("show");
|
||||
mediaFunction = tmdbShowInfo;
|
||||
} else {
|
||||
res.status(422).send({
|
||||
success: false,
|
||||
message: 'Incorrect type. Allowed types: "movie" or "show"'
|
||||
});
|
||||
}
|
||||
|
||||
if (mediaFunction === undefined) {
|
||||
res.status(200);
|
||||
return;
|
||||
}
|
||||
|
||||
mediaFunction(id)
|
||||
.then(tmdbMedia =>
|
||||
request.requestFromTmdb(tmdbMedia, ip, user_agent, username)
|
||||
)
|
||||
.then(() =>
|
||||
res.send({ success: true, message: "Media item successfully requested" })
|
||||
)
|
||||
.catch(err =>
|
||||
res.status(500).send({ success: false, message: err.message })
|
||||
);
|
||||
}
|
||||
|
||||
module.exports = submitRequestController;
|
||||
@@ -1,26 +0,0 @@
|
||||
const RequestRepository = require("../../../plex/requestRepository");
|
||||
|
||||
const requestRepository = new RequestRepository();
|
||||
|
||||
/**
|
||||
* Controller: Retrieves search history of a logged in user
|
||||
* @param {Request} req http request variable
|
||||
* @param {Response} res
|
||||
* @returns {Callback}
|
||||
*/
|
||||
function updateRequested(req, res) {
|
||||
const id = req.params.requestId;
|
||||
const type = req.body.type;
|
||||
const status = req.body.status;
|
||||
|
||||
requestRepository
|
||||
.updateRequestedById(id, type, status)
|
||||
.then(() => {
|
||||
res.send({ success: true });
|
||||
})
|
||||
.catch(error => {
|
||||
res.status(401).send({ success: false, message: error.message });
|
||||
});
|
||||
}
|
||||
|
||||
module.exports = updateRequested;
|
||||
@@ -1,27 +0,0 @@
|
||||
const configuration = require("../../../config/configuration").getInstance();
|
||||
const Plex = require("../../../plex/plex");
|
||||
const plex = new Plex(configuration.get("plex", "ip"));
|
||||
|
||||
/**
|
||||
* Controller: Search plex for movies, shows and episodes by query
|
||||
* @param {Request} req http request variable
|
||||
* @param {Response} res
|
||||
* @returns {Callback}
|
||||
*/
|
||||
|
||||
function watchDirectLink(req, res) {
|
||||
const { title, year } = req.query;
|
||||
|
||||
plex
|
||||
.getDirectLinkByTitleAndYear(title, year)
|
||||
.then(plexDirectLink => {
|
||||
if (plexDirectLink == false)
|
||||
res.status(404).send({ success: true, link: null });
|
||||
else res.status(200).send({ success: true, link: plexDirectLink });
|
||||
})
|
||||
.catch(error => {
|
||||
res.status(500).send({ success: false, message: error.message });
|
||||
});
|
||||
}
|
||||
|
||||
module.exports = watchDirectLink;
|
||||
@@ -1,27 +0,0 @@
|
||||
const RequestRepository = require("../../../request/request");
|
||||
const request = new RequestRepository();
|
||||
|
||||
/**
|
||||
* Controller: Fetch all requested items
|
||||
* @param {Request} req http request variable
|
||||
* @param {Response} res
|
||||
* @returns {Callback}
|
||||
*/
|
||||
function fetchAllRequests(req, res) {
|
||||
let { page, filter, sort, query } = req.query;
|
||||
let sort_by = sort;
|
||||
let sort_direction = undefined;
|
||||
|
||||
if (sort !== undefined && sort.includes(":")) {
|
||||
[sort_by, sort_direction] = sort.split(":");
|
||||
}
|
||||
|
||||
Promise.resolve()
|
||||
.then(() => request.fetchAll(page, sort_by, sort_direction, filter, query))
|
||||
.then(result => res.send(result))
|
||||
.catch(error => {
|
||||
res.status(404).send({ success: false, message: error.message });
|
||||
});
|
||||
}
|
||||
|
||||
module.exports = fetchAllRequests;
|
||||
@@ -1,22 +0,0 @@
|
||||
const RequestRepository = require("../../../request/request");
|
||||
const request = new RequestRepository();
|
||||
|
||||
/**
|
||||
* Controller: Get requested item by tmdb id and type
|
||||
* @param {Request} req http request variable
|
||||
* @param {Response} res
|
||||
* @returns {Callback}
|
||||
*/
|
||||
function fetchAllRequests(req, res) {
|
||||
const id = req.params.id;
|
||||
const { type } = req.query;
|
||||
|
||||
request
|
||||
.getRequestByIdAndType(id, type)
|
||||
.then(result => res.send(result))
|
||||
.catch(error => {
|
||||
res.status(404).send({ success: false, message: error.message });
|
||||
});
|
||||
}
|
||||
|
||||
module.exports = fetchAllRequests;
|
||||
@@ -1,67 +0,0 @@
|
||||
const configuration = require("../../../config/configuration").getInstance();
|
||||
const TMDB = require("../../../tmdb/tmdb");
|
||||
const RequestRepository = require("../../../request/request");
|
||||
const tmdb = new TMDB(configuration.get("tmdb", "apiKey"));
|
||||
const request = new RequestRepository();
|
||||
// const { sendSMS } = require("src/notifications/sms");
|
||||
|
||||
const tmdbMovieInfo = id => {
|
||||
return tmdb.movieInfo(id);
|
||||
};
|
||||
|
||||
const tmdbShowInfo = id => {
|
||||
return tmdb.showInfo(id);
|
||||
};
|
||||
|
||||
/**
|
||||
* Controller: Request by id with type param
|
||||
* @param {Request} req http request variable
|
||||
* @param {Response} res
|
||||
* @returns {Callback}
|
||||
*/
|
||||
function requestTmdbIdController(req, res) {
|
||||
const { id, type } = req.body;
|
||||
|
||||
const ip = req.headers["x-forwarded-for"] || req.connection.remoteAddress;
|
||||
const user_agent = req.headers["user-agent"];
|
||||
const username = req.loggedInUser ? req.loggedInUser.username : null;
|
||||
|
||||
let mediaFunction = undefined;
|
||||
|
||||
if (id === undefined || type === undefined) {
|
||||
res.status(422).send({
|
||||
success: false,
|
||||
message: "'Missing parameteres: 'id' and/or 'type'"
|
||||
});
|
||||
}
|
||||
|
||||
if (type === "movie") {
|
||||
mediaFunction = tmdbMovieInfo;
|
||||
} else if (type === "show") {
|
||||
mediaFunction = tmdbShowInfo;
|
||||
} else {
|
||||
res.status(422).send({
|
||||
success: false,
|
||||
message: 'Incorrect type. Allowed types: "movie" or "show"'
|
||||
});
|
||||
}
|
||||
|
||||
mediaFunction(id)
|
||||
// .catch((error) => { console.error(error); res.status(404).send({ success: false, error: 'Id not found' }) })
|
||||
.then(tmdbMedia => {
|
||||
request.requestFromTmdb(tmdbMedia, ip, user_agent, username);
|
||||
|
||||
// TODO enable SMS
|
||||
// const url = `https://request.movie?${tmdbMedia.type}=${tmdbMedia.id}`;
|
||||
// const message = `${tmdbMedia.title} (${tmdbMedia.year}) requested!\n${url}`;
|
||||
// sendSMS(message);
|
||||
})
|
||||
.then(() =>
|
||||
res.send({ success: true, message: "Request has been submitted." })
|
||||
)
|
||||
.catch(error => {
|
||||
res.send({ success: false, message: error.message });
|
||||
});
|
||||
}
|
||||
|
||||
module.exports = requestTmdbIdController;
|
||||
@@ -1,40 +0,0 @@
|
||||
const configuration = require("../../../config/configuration").getInstance();
|
||||
const TMDB = require("../../../tmdb/tmdb");
|
||||
const SearchHistory = require("../../../searchHistory/searchHistory");
|
||||
const tmdb = new TMDB(configuration.get("tmdb", "apiKey"));
|
||||
const searchHistory = new SearchHistory();
|
||||
|
||||
/**
|
||||
* Controller: Search for movies by query and pagey
|
||||
* @param {Request} req http request variable
|
||||
* @param {Response} res
|
||||
* @returns {Callback}
|
||||
*/
|
||||
function movieSearchController(req, res) {
|
||||
const { query, page, adult } = req.query;
|
||||
const username = req.loggedInUser ? req.loggedInUser.username : null;
|
||||
const includeAdult = adult == "true" ? true : false;
|
||||
|
||||
if (username) {
|
||||
searchHistory.create(username, query);
|
||||
}
|
||||
|
||||
return tmdb
|
||||
.movieSearch(query, page, includeAdult)
|
||||
.then(movieSearchResults => res.send(movieSearchResults))
|
||||
.catch(error => {
|
||||
const { status, message } = error;
|
||||
|
||||
if (status && message) {
|
||||
res.status(status).send({ success: false, message });
|
||||
} else {
|
||||
// TODO log unhandled errors
|
||||
console.log("caugth movie search controller error", error);
|
||||
res.status(500).send({
|
||||
message: `An unexpected error occured while searching movies with query: ${query}`
|
||||
});
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
module.exports = movieSearchController;
|
||||
@@ -1,46 +0,0 @@
|
||||
const configuration = require("../../..//config/configuration").getInstance();
|
||||
const TMDB = require("../../../tmdb/tmdb");
|
||||
const SearchHistory = require("../../../searchHistory/searchHistory");
|
||||
const tmdb = new TMDB(configuration.get("tmdb", "apiKey"));
|
||||
const searchHistory = new SearchHistory();
|
||||
|
||||
function checkAndCreateJsonResponse(result) {
|
||||
if (typeof result["createJsonResponse"] === "function") {
|
||||
return result.createJsonResponse();
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Controller: Search for multi (movies, shows and people by query and pagey
|
||||
* @param {Request} req http request variable
|
||||
* @param {Response} res
|
||||
* @returns {Callback}
|
||||
*/
|
||||
function multiSearchController(req, res) {
|
||||
const { query, page, adult } = req.query;
|
||||
const username = req.loggedInUser ? req.loggedInUser.username : null;
|
||||
|
||||
if (username) {
|
||||
searchHistory.create(username, query);
|
||||
}
|
||||
|
||||
return tmdb
|
||||
.multiSearch(query, page, adult)
|
||||
.then(multiSearchResults => res.send(multiSearchResults))
|
||||
.catch(error => {
|
||||
const { status, message } = error;
|
||||
|
||||
if (status && message) {
|
||||
res.status(status).send({ success: false, message });
|
||||
} else {
|
||||
// TODO log unhandled errors
|
||||
console.log("caugth multi search controller error", error);
|
||||
res.status(500).send({
|
||||
message: `An unexpected error occured while searching with query: ${query}`
|
||||
});
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
module.exports = multiSearchController;
|
||||
@@ -1,40 +0,0 @@
|
||||
const configuration = require("../../../config/configuration").getInstance();
|
||||
const TMDB = require("../../../tmdb/tmdb");
|
||||
const SearchHistory = require("../../../searchHistory/searchHistory");
|
||||
const tmdb = new TMDB(configuration.get("tmdb", "apiKey"));
|
||||
const searchHistory = new SearchHistory();
|
||||
|
||||
/**
|
||||
* Controller: Search for person by query and pagey
|
||||
* @param {Request} req http request variable
|
||||
* @param {Response} res
|
||||
* @returns {Callback}
|
||||
*/
|
||||
function personSearchController(req, res) {
|
||||
const { query, page, adult } = req.query;
|
||||
const username = req.loggedInUser ? req.loggedInUser.username : null;
|
||||
const includeAdult = adult == "true" ? true : false;
|
||||
|
||||
if (username) {
|
||||
searchHistory.create(username, query);
|
||||
}
|
||||
|
||||
return tmdb
|
||||
.personSearch(query, page, includeAdult)
|
||||
.then(persons => res.send(persons))
|
||||
.catch(error => {
|
||||
const { status, message } = error;
|
||||
|
||||
if (status && message) {
|
||||
res.status(status).send({ success: false, message });
|
||||
} else {
|
||||
// TODO log unhandled errors
|
||||
console.log("caugth person search controller error", error);
|
||||
res.status(500).send({
|
||||
message: `An unexpected error occured while searching people with query: ${query}`
|
||||
});
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
module.exports = personSearchController;
|
||||
@@ -1,32 +0,0 @@
|
||||
const SearchHistory = require("../../../searchHistory/searchHistory");
|
||||
const configuration = require("../../../config/configuration").getInstance();
|
||||
const TMDB = require("../../../tmdb/tmdb");
|
||||
const tmdb = new TMDB(configuration.get("tmdb", "apiKey"));
|
||||
const searchHistory = new SearchHistory();
|
||||
|
||||
/**
|
||||
* Controller: Search for shows by query and pagey
|
||||
* @param {Request} req http request variable
|
||||
* @param {Response} res
|
||||
* @returns {Callback}
|
||||
*/
|
||||
function showSearchController(req, res) {
|
||||
const { query, page, adult } = req.query;
|
||||
const username = req.loggedInUser ? req.loggedInUser.username : null;
|
||||
const includeAdult = adult == "true" ? true : false;
|
||||
|
||||
if (username) {
|
||||
searchHistory.create(username, query);
|
||||
}
|
||||
|
||||
return tmdb
|
||||
.showSearch(query, page, includeAdult)
|
||||
.then(shows => {
|
||||
res.send(shows);
|
||||
})
|
||||
.catch(error => {
|
||||
res.status(500).send({ success: false, message: error.message });
|
||||
});
|
||||
}
|
||||
|
||||
module.exports = showSearchController;
|
||||
@@ -1,17 +0,0 @@
|
||||
const StrayRepository = require("../../../seasoned/strayRepository");
|
||||
|
||||
const strayRepository = new StrayRepository();
|
||||
|
||||
function readStraysController(req, res) {
|
||||
const { verified, page } = req.query;
|
||||
strayRepository
|
||||
.readAll(verified, page)
|
||||
.then(strays => {
|
||||
res.send(strays);
|
||||
})
|
||||
.catch(error => {
|
||||
res.status(500).send({ success: false, message: error.message });
|
||||
});
|
||||
}
|
||||
|
||||
module.exports = readStraysController;
|
||||
@@ -1,18 +0,0 @@
|
||||
const StrayRepository = require("../../../seasoned/strayRepository");
|
||||
|
||||
const strayRepository = new StrayRepository();
|
||||
|
||||
function strayByIdController(req, res) {
|
||||
const id = req.params.strayId;
|
||||
|
||||
strayRepository
|
||||
.read(id)
|
||||
.then(stray => {
|
||||
res.send(stray);
|
||||
})
|
||||
.catch(error => {
|
||||
res.status(500).send({ success: false, message: error.message });
|
||||
});
|
||||
}
|
||||
|
||||
module.exports = strayByIdController;
|
||||
@@ -1,18 +0,0 @@
|
||||
const StrayRepository = require("../../../seasoned/strayRepository");
|
||||
|
||||
const strayRepository = new StrayRepository();
|
||||
|
||||
function verifyStrayController(req, res) {
|
||||
const id = req.params.strayId;
|
||||
|
||||
strayRepository
|
||||
.verifyStray(id)
|
||||
.then(() => {
|
||||
res.send({ success: true, message: "Episode verified" });
|
||||
})
|
||||
.catch(error => {
|
||||
res.status(500).send({ success: false, message: error.message });
|
||||
});
|
||||
}
|
||||
|
||||
module.exports = verifyStrayController;
|
||||
@@ -1,28 +0,0 @@
|
||||
const configuration = require("../../../config/configuration").getInstance();
|
||||
const TMDB = require("../../../tmdb/tmdb");
|
||||
const tmdb = new TMDB(configuration.get("tmdb", "apiKey"));
|
||||
|
||||
const showCreditsController = (req, res) => {
|
||||
const showId = req.params.id;
|
||||
|
||||
tmdb
|
||||
.showCredits(showId)
|
||||
.then(credits => res.send(credits.createJsonResponse()))
|
||||
.catch(error => {
|
||||
const { status, message } = error;
|
||||
|
||||
if (status && message) {
|
||||
res.status(status).send({ success: false, message });
|
||||
} else {
|
||||
// TODO log unhandled errors
|
||||
console.log("caugth show credits controller error", error);
|
||||
res
|
||||
.status(500)
|
||||
.send({
|
||||
message: "An unexpected error occured while requesting show credits"
|
||||
});
|
||||
}
|
||||
});
|
||||
};
|
||||
|
||||
module.exports = showCreditsController;
|
||||
@@ -1,55 +0,0 @@
|
||||
const configuration = require("../../../config/configuration").getInstance();
|
||||
const TMDB = require("../../../tmdb/tmdb");
|
||||
const Plex = require("../../../plex/plex");
|
||||
const tmdb = new TMDB(configuration.get("tmdb", "apiKey"));
|
||||
const plex = new Plex(configuration.get("plex", "ip"));
|
||||
|
||||
function handleError(error, res) {
|
||||
const { status, message } = error;
|
||||
|
||||
if (status && message) {
|
||||
res.status(status).send({ success: false, message });
|
||||
} else {
|
||||
console.log("caught showinfo controller error", error);
|
||||
res.status(500).send({
|
||||
message: "An unexpected error occured while requesting show info."
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Controller: Retrieve information for a show
|
||||
* @param {Request} req http request variable
|
||||
* @param {Response} res
|
||||
* @returns {Callback}
|
||||
*/
|
||||
|
||||
async function showInfoController(req, res) {
|
||||
const showId = req.params.id;
|
||||
let { credits, check_existance } = req.query;
|
||||
|
||||
credits && credits.toLowerCase() === "true"
|
||||
? (credits = true)
|
||||
: (credits = false);
|
||||
check_existance && check_existance.toLowerCase() === "true"
|
||||
? (check_existance = true)
|
||||
: (check_existance = false);
|
||||
|
||||
let tmdbQueue = [tmdb.showInfo(showId)];
|
||||
if (credits) tmdbQueue.push(tmdb.showCredits(showId));
|
||||
|
||||
try {
|
||||
const [Show, Credits] = await Promise.all(tmdbQueue);
|
||||
|
||||
const show = Show.createJsonResponse();
|
||||
if (credits) show.credits = Credits.createJsonResponse();
|
||||
|
||||
if (check_existance) show.exists_in_plex = await plex.existsInPlex(show);
|
||||
|
||||
res.send(show);
|
||||
} catch (error) {
|
||||
handleError(error, res);
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = showInfoController;
|
||||
@@ -1,93 +0,0 @@
|
||||
const UserRepository = require("../../../user/userRepository");
|
||||
const userRepository = new UserRepository();
|
||||
const fetch = require("node-fetch");
|
||||
const FormData = require("form-data");
|
||||
|
||||
function handleError(error, res) {
|
||||
let { status, message, source } = error;
|
||||
|
||||
if (status && message) {
|
||||
if (status === 401) {
|
||||
(message = "Unauthorized. Please check plex credentials."),
|
||||
(source = "plex");
|
||||
}
|
||||
|
||||
res.status(status).send({ success: false, message, source });
|
||||
} else {
|
||||
console.log("caught authenticate plex account controller error", error);
|
||||
res.status(500).send({
|
||||
message:
|
||||
"An unexpected error occured while authenticating your account with plex",
|
||||
source
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
function handleResponse(response) {
|
||||
if (!response.ok) {
|
||||
throw {
|
||||
success: false,
|
||||
status: response.status,
|
||||
message: response.statusText
|
||||
};
|
||||
}
|
||||
|
||||
return response.json();
|
||||
}
|
||||
|
||||
function plexAuthenticate(username, password) {
|
||||
const url = "https://plex.tv/api/v2/users/signin";
|
||||
|
||||
const form = new FormData();
|
||||
form.append("login", username);
|
||||
form.append("password", password);
|
||||
form.append("rememberMe", "false");
|
||||
|
||||
const headers = {
|
||||
Accept: "application/json, text/plain, */*",
|
||||
"Content-Type": form.getHeaders()["content-type"],
|
||||
"X-Plex-Client-Identifier": "seasonedRequest"
|
||||
};
|
||||
const options = {
|
||||
method: "POST",
|
||||
headers,
|
||||
body: form
|
||||
};
|
||||
|
||||
return fetch(url, options).then(resp => handleResponse(resp));
|
||||
}
|
||||
|
||||
function link(req, res) {
|
||||
const user = req.loggedInUser;
|
||||
const { username, password } = req.body;
|
||||
|
||||
return plexAuthenticate(username, password)
|
||||
.then(plexUser => userRepository.linkPlexUserId(user.username, plexUser.id))
|
||||
.then(response =>
|
||||
res.send({
|
||||
success: true,
|
||||
message:
|
||||
"Successfully authenticated and linked plex account with seasoned request."
|
||||
})
|
||||
)
|
||||
.catch(error => handleError(error, res));
|
||||
}
|
||||
|
||||
function unlink(req, res) {
|
||||
const username = req.loggedInUser ? req.loggedInUser.username : null;
|
||||
|
||||
return userRepository
|
||||
.unlinkPlexUserId(username)
|
||||
.then(response =>
|
||||
res.send({
|
||||
success: true,
|
||||
message: "Successfully unlinked plex account from seasoned request."
|
||||
})
|
||||
)
|
||||
.catch(error => handleError(error, res));
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
link,
|
||||
unlink
|
||||
};
|
||||
@@ -1,61 +0,0 @@
|
||||
const User = require("../../../user/user");
|
||||
const Token = require("../../../user/token");
|
||||
const UserSecurity = require("../../../user/userSecurity");
|
||||
const UserRepository = require("../../../user/userRepository");
|
||||
const configuration = require("../../../config/configuration").getInstance();
|
||||
|
||||
const secret = configuration.get("authentication", "secret");
|
||||
const userSecurity = new UserSecurity();
|
||||
const userRepository = new UserRepository();
|
||||
|
||||
// TODO look to move some of the token generation out of the reach of the final "catch-all"
|
||||
// catch including the, maybe sensitive, error message.
|
||||
|
||||
const isProduction = process.env.NODE_ENV === "production";
|
||||
const cookieOptions = {
|
||||
httpOnly: false,
|
||||
secure: isProduction,
|
||||
maxAge: 90 * 24 * 3600000, // 90 days
|
||||
sameSite: isProduction ? "Strict" : "Lax"
|
||||
};
|
||||
|
||||
/**
|
||||
* Controller: Log in a user provided correct credentials.
|
||||
* @param {Request} req http request variable
|
||||
* @param {Response} res
|
||||
* @returns {Callback}
|
||||
*/
|
||||
async function loginController(req, res) {
|
||||
const user = new User(req.body.username);
|
||||
const password = req.body.password;
|
||||
|
||||
try {
|
||||
const [loggedIn, isAdmin, settings] = await Promise.all([
|
||||
userSecurity.login(user, password),
|
||||
userRepository.checkAdmin(user),
|
||||
userRepository.getSettings(user.username)
|
||||
]);
|
||||
|
||||
if (!loggedIn) {
|
||||
return res.status(503).send({
|
||||
success: false,
|
||||
message: "Unexpected error! Unable to create user."
|
||||
});
|
||||
}
|
||||
|
||||
const token = new Token(
|
||||
user,
|
||||
isAdmin === 1 ? true : false,
|
||||
settings
|
||||
).toString(secret);
|
||||
|
||||
return res.cookie("authorization", token, cookieOptions).status(200).send({
|
||||
success: true,
|
||||
message: "Welcome to request.movie!"
|
||||
});
|
||||
} catch (error) {
|
||||
return res.status(401).send({ success: false, message: error.message });
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = loginController;
|
||||
@@ -1,16 +0,0 @@
|
||||
/**
|
||||
* Controller: Log out a user (destroy authorization token)
|
||||
* @param {Request} req http request variable
|
||||
* @param {Response} res
|
||||
* @returns {Callback}
|
||||
*/
|
||||
async function logoutController(req, res) {
|
||||
res.clearCookie("authorization");
|
||||
|
||||
return res.status(200).send({
|
||||
success: true,
|
||||
message: "Logged out, see you later!"
|
||||
});
|
||||
}
|
||||
|
||||
module.exports = logoutController;
|
||||
@@ -1,47 +0,0 @@
|
||||
const User = require("../../../user/user");
|
||||
const Token = require("../../../user/token");
|
||||
const UserSecurity = require("../../../user/userSecurity");
|
||||
const UserRepository = require("../../../user/userRepository");
|
||||
const configuration = require("../../../config/configuration").getInstance();
|
||||
|
||||
const secret = configuration.get("authentication", "secret");
|
||||
const userSecurity = new UserSecurity();
|
||||
const userRepository = new UserRepository();
|
||||
|
||||
const isProduction = process.env.NODE_ENV === "production";
|
||||
const cookieOptions = {
|
||||
httpOnly: false,
|
||||
secure: isProduction,
|
||||
maxAge: 90 * 24 * 3600000, // 90 days
|
||||
sameSite: isProduction ? "Strict" : "Lax"
|
||||
};
|
||||
|
||||
/**
|
||||
* Controller: Register a new user
|
||||
* @param {Request} req http request variable
|
||||
* @param {Response} res
|
||||
* @returns {Callback}
|
||||
*/
|
||||
function registerController(req, res) {
|
||||
const user = new User(req.body.username, req.body.email);
|
||||
const password = req.body.password;
|
||||
|
||||
userSecurity
|
||||
.createNewUser(user, password)
|
||||
.then(() => {
|
||||
const token = new Token(user, false).toString(secret);
|
||||
|
||||
return res
|
||||
.cookie("authorization", token, cookieOptions)
|
||||
.status(200)
|
||||
.send({
|
||||
success: true,
|
||||
message: "Welcome to Seasoned!"
|
||||
});
|
||||
})
|
||||
.catch(error => {
|
||||
res.status(401).send({ success: false, message: error.message });
|
||||
});
|
||||
}
|
||||
|
||||
module.exports = registerController;
|
||||
@@ -1,28 +0,0 @@
|
||||
const RequestRepository = require("../../../plex/requestRepository.js");
|
||||
|
||||
const requestRepository = new RequestRepository();
|
||||
|
||||
/**
|
||||
* Controller: Retrieves requested items of a logged in user
|
||||
* @param {Request} req http request variable
|
||||
* @param {Response} res
|
||||
* @returns {Callback}
|
||||
*/
|
||||
function requestsController(req, res) {
|
||||
const username = req.loggedInUser ? req.loggedInUser.username : null;
|
||||
|
||||
requestRepository
|
||||
.userRequests(username)
|
||||
.then(requests => {
|
||||
res.send({
|
||||
success: true,
|
||||
results: requests,
|
||||
total_results: requests.length
|
||||
});
|
||||
})
|
||||
.catch(error => {
|
||||
res.status(500).send({ success: false, message: error.message });
|
||||
});
|
||||
}
|
||||
|
||||
module.exports = requestsController;
|
||||
@@ -1,24 +0,0 @@
|
||||
const SearchHistory = require("../../../searchHistory/searchHistory");
|
||||
|
||||
const searchHistory = new SearchHistory();
|
||||
|
||||
/**
|
||||
* Controller: Retrieves search history of a logged in user
|
||||
* @param {Request} req http request variable
|
||||
* @param {Response} res
|
||||
* @returns {Callback}
|
||||
*/
|
||||
function historyController(req, res) {
|
||||
const username = req.loggedInUser ? req.loggedInUser.username : null;
|
||||
|
||||
searchHistory
|
||||
.read(username)
|
||||
.then(searchQueries => {
|
||||
res.send({ success: true, searchQueries });
|
||||
})
|
||||
.catch(error => {
|
||||
res.status(404).send({ success: false, message: error.message });
|
||||
});
|
||||
}
|
||||
|
||||
module.exports = historyController;
|
||||
@@ -1,41 +0,0 @@
|
||||
const UserRepository = require("../../../user/userRepository");
|
||||
const userRepository = new UserRepository();
|
||||
/**
|
||||
* Controller: Retrieves settings of a logged in user
|
||||
* @param {Request} req http request variable
|
||||
* @param {Response} res
|
||||
* @returns {Callback}
|
||||
*/
|
||||
const getSettingsController = (req, res) => {
|
||||
const username = req.loggedInUser ? req.loggedInUser.username : null;
|
||||
|
||||
userRepository
|
||||
.getSettings(username)
|
||||
.then(settings => {
|
||||
res.send({ success: true, settings });
|
||||
})
|
||||
.catch(error => {
|
||||
res.status(404).send({ success: false, message: error.message });
|
||||
});
|
||||
};
|
||||
|
||||
const updateSettingsController = (req, res) => {
|
||||
const username = req.loggedInUser ? req.loggedInUser.username : null;
|
||||
|
||||
const idempotencyKey = req.headers("Idempotency-Key"); // TODO implement better transactions
|
||||
const { dark_mode, emoji } = req.body;
|
||||
|
||||
userRepository
|
||||
.updateSettings(username, dark_mode, emoji)
|
||||
.then(settings => {
|
||||
res.send({ success: true, settings });
|
||||
})
|
||||
.catch(error => {
|
||||
res.status(404).send({ success: false, message: error.message });
|
||||
});
|
||||
};
|
||||
|
||||
module.exports = {
|
||||
getSettingsController,
|
||||
updateSettingsController
|
||||
};
|
||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user