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');
function convertPlexToStream(plexStream) {
const stream = convertPlexToSeasoned(plexStream)
const plexStreamMedia = plexStream.Media[0]
const stream = convertPlexToSeasoned(plexStream);
const plexStreamMedia = plexStream.Media[0];
stream.mediaInfo = convertStreamToMediaInfo(plexStreamMedia);
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();
function hookDumpController(req, res) {

View File

@@ -1,25 +1,24 @@
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
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
}
year: `(${this.mediaItem.year})`,
poster: this.posterURL + this.mediaItem.poster,
};
return `
<h1>${info.name} ${info.year}</h1>
<img src="${info.poster}">
`
`;
}
}

View File

@@ -1,79 +1,72 @@
const assert = require('assert');
const convertPlexToSeasoned = require('src/plex/convertPlexToSeasoned');
const convertPlexToStream = require('src/plex/convertPlexToStream');
var rp = require('request-promise');
const PLEX_METHODS = ['lookup', 'playing']
const rp = require('request-promise');
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) {
return Promise.resolve()
.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()
.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) => {
return (element.type === 'movie' || element.type === 'show')
}).map((element) => convertPlexToSeasoned(element))
return [mappedResults, mappedResults.length]
return (element.type === 'movie' || element.type === 'show');
}).map((element) => convertPlexToSeasoned(element));
return [mappedResults, mappedResults.length];
})
.catch((error) => {throw new Error(error)})
.catch((error) => { throw new Error(error); });
}
nowPlaying() {
var options = {
const options = {
uri: 'http://10.0.0.44:32400/status/sessions',
headers: {
'Accept': 'application/json'
Accept: 'application/json',
},
json: true
}
json: true,
};
return rp(options)
.then((result) => {
if (result.MediaContainer.size > 0) {
var playing = result.MediaContainer.Video.map(convertPlexToStream);
return {'size': Object.keys(playing).length, 'video': playing };
} else {
return { 'size': 0, 'video': [] };
const playing = result.MediaContainer.Video.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);
})
throw new Error(`Error handling plex playing. Error: ${err}`);
});
}
// multipleInPlex(tmdbResults) {

View File

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

View File

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