Linted all plex scripts.

This commit is contained in:
2018-02-07 13:50:05 +01:00
parent 97217a2826
commit dda1db6c5f
9 changed files with 220 additions and 251 deletions

View File

@@ -5,8 +5,8 @@ const convertStreamToUser = require('src/plex/stream/convertStreamToUser');
const ConvertStreamToPlayback = require('src/plex/stream/convertStreamToPlayback'); const ConvertStreamToPlayback = require('src/plex/stream/convertStreamToPlayback');
function convertPlexToStream(plexStream) { function convertPlexToStream(plexStream) {
const stream = convertPlexToSeasoned(plexStream) const stream = convertPlexToSeasoned(plexStream);
const plexStreamMedia = plexStream.Media[0] const plexStreamMedia = plexStream.Media[0];
stream.mediaInfo = convertStreamToMediaInfo(plexStreamMedia); stream.mediaInfo = convertStreamToMediaInfo(plexStreamMedia);
stream.player = convertStreamToPlayer(plexStream.Player); stream.player = convertStreamToPlayer(plexStream.Player);

View File

@@ -1,10 +1,3 @@
/*
* @Author: KevinMidboe
* @Date: 2017-05-03 23:26:46
* @Last Modified by: KevinMidboe
* @Last Modified time: 2017-05-03 23:27:59
*/
const configuration = require('src/config/configuration').getInstance(); const configuration = require('src/config/configuration').getInstance();
function hookDumpController(req, res) { function hookDumpController(req, res) {

View File

@@ -1,25 +1,24 @@
class mailTemplate { class mailTemplate {
constructor(mediaItem) { constructor(mediaItem) {
this.mediaItem = mediaItem; this.mediaItem = mediaItem;
this.posterURL = 'https://image.tmdb.org/t/p/w600'; this.posterURL = 'https://image.tmdb.org/t/p/w600';
} }
toText() { toText() {
return this.mediaItem.title + ' (' + this.mediaItem.year + ')'; // plain text body return `${this.mediaItem.title} (${this.mediaItem.year})`; // plain text body
} }
toHTML() { toHTML() {
const info = { const info = {
name: this.mediaItem.title, name: this.mediaItem.title,
year: '(' + this.mediaItem.year + ')', year: `(${this.mediaItem.year})`,
poster: this.posterURL + this.mediaItem.poster poster: this.posterURL + this.mediaItem.poster,
} };
return ` return `
<h1>${info.name} ${info.year}</h1> <h1>${info.name} ${info.year}</h1>
<img src="${info.poster}"> <img src="${info.poster}">
` `;
} }
} }

View File

@@ -1,79 +1,72 @@
const assert = require('assert');
const convertPlexToSeasoned = require('src/plex/convertPlexToSeasoned'); const convertPlexToSeasoned = require('src/plex/convertPlexToSeasoned');
const convertPlexToStream = require('src/plex/convertPlexToStream'); const convertPlexToStream = require('src/plex/convertPlexToStream');
var rp = require('request-promise'); const rp = require('request-promise');
const PLEX_METHODS = ['lookup', 'playing']
class PlexRepository { class PlexRepository {
search(query, callback) {
var options = {
uri: 'http://10.0.0.44:32400/search?query=' + query,
headers: {
'Accept': 'application/json'
},
json: true
}
return rp(options)
.then((result) => this.mapResults(result))
.then(([mappedResults, resultCount]) => {
return { 'results': mappedResults, 'total_results': resultCount }
})
}
compareTmdbToPlex(tmdb, plexResult) {
return Promise.resolve()
.then(() => {
plexResult.results.map((plexItem) => {
if (tmdb.title === plexItem.title && tmdb.year === plexItem.year)
tmdb.matchedInPlex = true;
})
return tmdb
})
}
inPlex(tmdbResult) { inPlex(tmdbResult) {
return Promise.resolve() return Promise.resolve()
.then(() => this.search(tmdbResult.title)) .then(() => this.search(tmdbResult.title))
.then((plexResult) => this.compareTmdbToPlex(tmdbResult, plexResult)) .then(plexResult => this.compareTmdbToPlex(tmdbResult, plexResult));
} }
mapResults(response) { search(query) {
const options = {
uri: `http://10.0.0.44:32400/search?query=${query}`,
headers: {
Accept: 'application/json',
},
json: true,
};
return rp(options)
.then(result => this.mapResults(result))
.then(([mappedResults, resultCount]) => ({ results: mappedResults, total_results: resultCount }));
}
static compareTmdbToPlex(tmdb, plexResult) {
return Promise.resolve() return Promise.resolve()
.then(() => { .then(() => {
if (! response.MediaContainer.hasOwnProperty('Metadata')) return [[], 0] plexResult.results.map((plexItem) => {
if (tmdb.title === plexItem.title && tmdb.year === plexItem.year) { tmdb.matchedInPlex = true; }
return tmdb;
});
return tmdb;
});
}
static mapResults(response) {
return Promise.resolve()
.then(() => {
if (!response.MediaContainer.hasOwnProperty('Metadata')) return [[], 0];
const mappedResults = response.MediaContainer.Metadata.filter((element) => { const mappedResults = response.MediaContainer.Metadata.filter((element) => {
return (element.type === 'movie' || element.type === 'show') return (element.type === 'movie' || element.type === 'show');
}).map((element) => convertPlexToSeasoned(element)) }).map((element) => convertPlexToSeasoned(element));
return [mappedResults, mappedResults.length] return [mappedResults, mappedResults.length];
}) })
.catch((error) => {throw new Error(error)}) .catch((error) => { throw new Error(error); });
} }
nowPlaying() { nowPlaying() {
var options = { const options = {
uri: 'http://10.0.0.44:32400/status/sessions', uri: 'http://10.0.0.44:32400/status/sessions',
headers: { headers: {
'Accept': 'application/json' Accept: 'application/json',
}, },
json: true json: true,
} };
return rp(options) return rp(options)
.then((result) => { .then((result) => {
if (result.MediaContainer.size > 0) { if (result.MediaContainer.size > 0) {
var playing = result.MediaContainer.Video.map(convertPlexToStream); const playing = result.MediaContainer.Video.map(convertPlexToStream);
return {'size': Object.keys(playing).length, 'video': playing }; return { size: Object.keys(playing).length, video: playing };
} else {
return { 'size': 0, 'video': [] };
} }
return { size: 0, video: [] };
}) })
.catch((err) => { .catch((err) => {
throw new Error('Error handling plex playing. Error: ' + err); throw new Error(`Error handling plex playing. Error: ${err}`);
}) });
} }
// multipleInPlex(tmdbResults) { // multipleInPlex(tmdbResults) {

View File

@@ -1,66 +1,57 @@
const assert = require('assert');
const PlexRepository = require('src/plex/plexRepository'); const PlexRepository = require('src/plex/plexRepository');
const plexRepository = new PlexRepository();
const configuration = require('src/config/configuration').getInstance();
const Cache = require('src/tmdb/cache'); const Cache = require('src/tmdb/cache');
const configuration = require('src/config/configuration').getInstance();
const TMDB = require('src/tmdb/tmdb'); const TMDB = require('src/tmdb/tmdb');
const cache = new Cache();
const tmdb = new TMDB(cache, configuration.get('tmdb', 'apiKey'));
var Promise = require('bluebird');
var rp = require('request-promise');
const establishedDatabase = require('src/database/database'); const establishedDatabase = require('src/database/database');
const MailTemplate = require('src/plex/mailTemplate') const plexRepository = new PlexRepository();
const cache = new Cache();
const tmdb = new TMDB(cache, configuration.get('tmdb', 'apiKey'));
var pythonShell = require('python-shell'); const MailTemplate = require('src/plex/mailTemplate');
const nodemailer = require('nodemailer'); const nodemailer = require('nodemailer');
class RequestRepository { class RequestRepository {
constructor(cache, database) { constructor(cache, database) {
this.database = database || establishedDatabase; this.database = database || establishedDatabase;
this.queries = { this.queries = {
'insertRequest': "INSERT INTO requests VALUES (?, ?, ?, ?, ?, ?, ?, CURRENT_DATE, 'requested', ?, ?)", insertRequest: "INSERT INTO requests VALUES (?, ?, ?, ?, ?, ?, ?, CURRENT_DATE, 'requested', ?, ?)",
'fetchRequstedItems': "SELECT * FROM requests", fetchRequstedItems: 'SELECT * FROM requests',
'updateRequestedById': "UPDATE requests SET status = ? WHERE id is ? AND type is ?", updateRequestedById: 'UPDATE requests SET status = ? WHERE id is ? AND type is ?',
'checkIfIdRequested': "SELECT * FROM requests WHERE id IS ? AND type IS ?", checkIfIdRequested: 'SELECT * FROM requests WHERE id IS ? AND type IS ?',
} };
} }
search(query, type, page) { search(query, type, page) {
return Promise.resolve() return Promise.resolve()
.then(() => tmdb.search(query, type, page)) .then(() => tmdb.search(query, type, page))
// .then((tmdbResult) => plexRepository.multipleInPlex(tmdbResult)) // .then((tmdbResult) => plexRepository.multipleInPlex(tmdbResult))
.then((result) => { .then(result => result)
return result .catch(error => `error in the house${error}`);
})
.catch((error) => {return 'error in the house' + error})
} }
lookup(identifier, type = 'movie') { lookup(identifier, type = 'movie') {
return Promise.resolve() return Promise.resolve()
.then(() => tmdb.lookup(identifier, type)) .then(() => tmdb.lookup(identifier, type))
.then((tmdbMovie) => this.checkID(tmdbMovie)) .then(tmdbMovie => this.checkID(tmdbMovie))
.then((tmdbMovie) => plexRepository.inPlex(tmdbMovie)) .then(tmdbMovie => plexRepository.inPlex(tmdbMovie))
.catch((error) => { .catch((error) => {
console.log(error) throw new Error(error);
}) });
} }
checkID(tmdbMovie) { checkID(tmdbMovie) {
return Promise.resolve() return Promise.resolve()
.then(() => this.database.get(this.queries.checkIfIdRequested, [tmdbMovie.id, tmdbMovie.type])) .then(() => this.database.get(this.queries.checkIfIdRequested, [tmdbMovie.id, tmdbMovie.type]))
.then((result, error) => { .then((result, error) => {
if (error) { throw new Error(error); }
let already_requested = false; let already_requested = false;
if (result) if (result) { already_requested = true; }
already_requested = true
tmdbMovie.requested = already_requested; tmdbMovie.requested = already_requested;
return tmdbMovie; return tmdbMovie;
}) });
} }
/** /**
@@ -69,22 +60,19 @@ class RequestRepository {
* @returns {Promise} If nothing has gone wrong. * @returns {Promise} If nothing has gone wrong.
*/ */
sendRequest(identifier, type, ip, user_agent, user) { sendRequest(identifier, type, ip, user_agent, user) {
tmdb.lookup(identifier, type).then(movie => { tmdb.lookup(identifier, type).then((movie) => {
if (user === 'false') { user = 'NULL'; }
if (user === 'false')
user = 'NULL';
console.log(user)
// Add request to database // Add request to database
this.database.run(this.queries.insertRequest, [movie.id, movie.title, movie.year, movie.poster_path, movie.background_path, user, ip, user_agent, movie.type]) this.database.run(this.queries.insertRequest, [movie.id, movie.title, movie.year, movie.poster_path, movie.background_path, user, ip, user_agent, movie.type]);
// create reusable transporter object using the default SMTP transport // create reusable transporter object using the default SMTP transport
let transporter = nodemailer.createTransport({ const transporter = nodemailer.createTransport({
service: 'gmail', service: 'gmail',
auth: { auth: {
user: configuration.get('mail', 'user_pi'), user: configuration.get('mail', 'user_pi'),
pass: configuration.get('mail', 'password_pi') pass: configuration.get('mail', 'password_pi'),
} },
// host: configuration.get('mail', 'host'), // host: configuration.get('mail', 'host'),
// port: 26, // port: 26,
// ignoreTLS: true, // ignoreTLS: true,
@@ -92,16 +80,16 @@ class RequestRepository {
// secure: false, // secure:true for port 465, secure:false for port 587 // secure: false, // secure:true for port 465, secure:false for port 587
}); });
const mailTemplate = new MailTemplate(movie) const mailTemplate = new MailTemplate(movie);
// setup email data with unicode symbols // setup email data with unicode symbols
let mailOptions = { const mailOptions = {
// TODO get the mail adr from global location (easy to add) // TODO get the mail adr from global location (easy to add)
from: 'MovieRequester <pi.midboe@gmail.com>', // sender address from: 'MovieRequester <pi.midboe@gmail.com>', // sender address
to: 'kevin.midboe@gmail.com', // list of receivers to: 'kevin.midboe@gmail.com', // list of receivers
subject: 'Download request', // Subject line subject: 'Download request', // Subject line
text: mailTemplate.toText(), text: mailTemplate.toText(),
html: mailTemplate.toHTML() html: mailTemplate.toHTML(),
}; };
// send mail with defined transport object // send mail with defined transport object
@@ -111,12 +99,10 @@ class RequestRepository {
} }
console.log('Message %s sent: %s', info.messageId, info.response); console.log('Message %s sent: %s', info.messageId, info.response);
}); });
});
})
// TODO add better response when done. // TODO add better response when done.
return Promise.resolve(); return Promise.resolve();
} }
fetchRequested() { fetchRequested() {
@@ -126,7 +112,6 @@ class RequestRepository {
updateRequestedById(id, type, status) { updateRequestedById(id, type, status) {
return this.database.run(this.queries.updateRequestedById, [status, id, type]); return this.database.run(this.queries.updateRequestedById, [status, id, type]);
} }
} }
module.exports = RequestRepository; module.exports = RequestRepository;

View File

@@ -8,7 +8,6 @@ class convertStreamToPlayback {
this.videoProfile = plexStream.videoProfile; this.videoProfile = plexStream.videoProfile;
this.duration = plexStream.duration; this.duration = plexStream.duration;
this.container = plexStream.container; this.container = plexStream.container;
} }
} }