diff --git a/seasoned_api/src/plex/convertPlexToStream.js b/seasoned_api/src/plex/convertPlexToStream.js index 1e82173..f6ba3ef 100644 --- a/seasoned_api/src/plex/convertPlexToStream.js +++ b/seasoned_api/src/plex/convertPlexToStream.js @@ -5,15 +5,15 @@ 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] - stream.mediaInfo = convertStreamToMediaInfo(plexStreamMedia); - stream.player = convertStreamToPlayer(plexStream.Player); + 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]); + stream.user = convertStreamToUser(plexStream.User); + stream.playback = new ConvertStreamToPlayback(plexStreamMedia.Part[0]); - return stream; + return stream; } -module.exports = convertPlexToStream; \ No newline at end of file +module.exports = convertPlexToStream; diff --git a/seasoned_api/src/plex/convertStreamToMediaInfo.js b/seasoned_api/src/plex/convertStreamToMediaInfo.js index 60d6c1d..ca75776 100644 --- a/seasoned_api/src/plex/convertStreamToMediaInfo.js +++ b/seasoned_api/src/plex/convertStreamToMediaInfo.js @@ -1,22 +1,22 @@ const MediaInfo = require('src/media_classes/mediaInfo'); function convertStreamToMediaInfo(plexStream) { - const mediaInfo = new MediaInfo(); - - mediaInfo.duration = plexStream.duration; - mediaInfo.height = plexStream.height; - mediaInfo.width = plexStream.width; + const mediaInfo = new MediaInfo(); - 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; + mediaInfo.duration = plexStream.duration; + mediaInfo.height = plexStream.height; + mediaInfo.width = plexStream.width; - return mediaInfo; + 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; \ No newline at end of file +module.exports = convertStreamToMediaInfo; diff --git a/seasoned_api/src/plex/hookDump.js b/seasoned_api/src/plex/hookDump.js index 1761757..23cecf4 100644 --- a/seasoned_api/src/plex/hookDump.js +++ b/seasoned_api/src/plex/hookDump.js @@ -1,14 +1,7 @@ -/* -* @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) { - console.log(req); + console.log(req); } -module.exports = hookDumpController; \ No newline at end of file +module.exports = hookDumpController; diff --git a/seasoned_api/src/plex/mailTemplate.js b/seasoned_api/src/plex/mailTemplate.js index c07e34f..64d38a9 100644 --- a/seasoned_api/src/plex/mailTemplate.js +++ b/seasoned_api/src/plex/mailTemplate.js @@ -1,26 +1,25 @@ class mailTemplate { - - constructor(mediaItem) { - this.mediaItem = mediaItem; - this.posterURL = 'https://image.tmdb.org/t/p/w600'; - } + 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 - } + 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 - } + toHTML() { + const info = { + name: this.mediaItem.title, + year: `(${this.mediaItem.year})`, + poster: this.posterURL + this.mediaItem.poster, + }; - return ` -

${info.name} ${info.year}

- - ` - } + return ` +

${info.name} ${info.year}

+ + `; + } } -module.exports = mailTemplate; \ No newline at end of file +module.exports = mailTemplate; diff --git a/seasoned_api/src/plex/plexRepository.js b/seasoned_api/src/plex/plexRepository.js index 2a3b975..bde5c01 100644 --- a/seasoned_api/src/plex/plexRepository.js +++ b/seasoned_api/src/plex/plexRepository.js @@ -1,87 +1,80 @@ -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 { + inPlex(tmdbResult) { + return Promise.resolve() + .then(() => this.search(tmdbResult.title)) + .then(plexResult => this.compareTmdbToPlex(tmdbResult, plexResult)); + } - search(query, callback) { - var options = { - uri: 'http://10.0.0.44:32400/search?query=' + query, - headers: { - 'Accept': 'application/json' - }, - json: true - } + 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]) => { - return { 'results': mappedResults, 'total_results': resultCount } - }) - } + return rp(options) + .then(result => this.mapResults(result)) + .then(([mappedResults, resultCount]) => ({ 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 - }) - } + static 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; + }); + return tmdb; + }); + } - inPlex(tmdbResult) { - return Promise.resolve() - .then(() => this.search(tmdbResult.title)) - .then((plexResult) => this.compareTmdbToPlex(tmdbResult, plexResult)) - } + static mapResults(response) { + return Promise.resolve() + .then(() => { + if (!response.MediaContainer.hasOwnProperty('Metadata')) return [[], 0]; - 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); }); + } - 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://10.0.0.44:32400/status/sessions', + headers: { + Accept: 'application/json', + }, + json: true, + }; - nowPlaying() { - var options = { - uri: 'http://10.0.0.44:32400/status/sessions', - headers: { - 'Accept': 'application/json' - }, - json: true - } + return rp(options) + .then((result) => { + if (result.MediaContainer.size > 0) { + 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}`); + }); + } - 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': [] }; - } - }) - .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) - // } + // multipleInPlex(tmdbResults) { + // const results = tmdbResults.results.map(async (tmdb) => { + // return this.inPlex(tmdb) + // }) + // return Promise.all(results) + // } } module.exports = PlexRepository; diff --git a/seasoned_api/src/plex/requestRepository.js b/seasoned_api/src/plex/requestRepository.js index b24e7f8..3cfb60b 100644 --- a/seasoned_api/src/plex/requestRepository.js +++ b/seasoned_api/src/plex/requestRepository.js @@ -1,132 +1,117 @@ -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 ?', + }; + } - 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 ?", - } - } + search(query, type, page) { + return Promise.resolve() + .then(() => tmdb.search(query, type, page)) + // .then((tmdbResult) => plexRepository.multipleInPlex(tmdbResult)) + .then(result => result) + .catch(error => `error in the house${error}`); + } - 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}) - } + 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); + }); + } - lookup(identifier, type = 'movie') { - return Promise.resolve() - .then(() => tmdb.lookup(identifier, type)) - .then((tmdbMovie) => this.checkID(tmdbMovie)) - .then((tmdbMovie) => plexRepository.inPlex(tmdbMovie)) - .catch((error) => { - console.log(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; } - checkID(tmdbMovie) { - return Promise.resolve() - .then(() => this.database.get(this.queries.checkIfIdRequested, [tmdbMovie.id, tmdbMovie.type])) - .then((result, error) => { - let already_requested = false; - if (result) - already_requested = true + tmdbMovie.requested = already_requested; + return tmdbMovie; + }); + } - tmdbMovie.requested = already_requested; - 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) { - tmdb.lookup(identifier, type).then(movie => { - - if (user === 'false') - user = 'NULL'; - console.log(user) - // 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]) + /** + * 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) { + 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]); - // create reusable transporter object using the default SMTP transport - let transporter = nodemailer.createTransport({ - service: 'gmail', - auth: { - user: configuration.get('mail', 'user_pi'), - pass: configuration.get('mail', 'password_pi') - } - // host: configuration.get('mail', 'host'), - // port: 26, - // ignoreTLS: true, - // tls :{rejectUnauthorized: false}, - // secure: false, // secure:true for port 465, secure:false for port 587 - }); + // create reusable transporter object using the default SMTP transport + const transporter = nodemailer.createTransport({ + service: 'gmail', + auth: { + user: configuration.get('mail', 'user_pi'), + pass: configuration.get('mail', 'password_pi'), + }, + // host: configuration.get('mail', 'host'), + // port: 26, + // ignoreTLS: true, + // tls :{rejectUnauthorized: false}, + // 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 = { - // TODO get the mail adr from global location (easy to add) - from: 'MovieRequester ', // sender address - to: 'kevin.midboe@gmail.com', // list of receivers - subject: 'Download request', // Subject line - text: mailTemplate.toText(), - html: mailTemplate.toHTML() - }; + // setup email data with unicode symbols + const mailOptions = { + // TODO get the mail adr from global location (easy to add) + from: 'MovieRequester ', // sender address + to: 'kevin.midboe@gmail.com', // list of receivers + subject: 'Download request', // Subject line + text: mailTemplate.toText(), + html: mailTemplate.toHTML(), + }; - // send mail with defined transport object - transporter.sendMail(mailOptions, (error, info) => { - if (error) { - return console.log(error); - } - console.log('Message %s sent: %s', info.messageId, info.response); - }); + // send mail with defined transport object + transporter.sendMail(mailOptions, (error, info) => { + if (error) { + return console.log(error); + } + console.log('Message %s sent: %s', info.messageId, info.response); + }); + }); - }) + // TODO add better response when done. + return Promise.resolve(); + } - // TODO add better response when done. - return Promise.resolve(); - - } - - fetchRequested() { - return this.database.all(this.queries.fetchRequstedItems); - } - - updateRequestedById(id, type, status) { - return this.database.run(this.queries.updateRequestedById, [status, id, type]); - } + fetchRequested() { + return this.database.all(this.queries.fetchRequstedItems); + } + updateRequestedById(id, type, status) { + return this.database.run(this.queries.updateRequestedById, [status, id, type]); + } } module.exports = RequestRepository; diff --git a/seasoned_api/src/plex/stream/convertStreamToPlayback.js b/seasoned_api/src/plex/stream/convertStreamToPlayback.js index f4fae2d..cdabee4 100644 --- a/seasoned_api/src/plex/stream/convertStreamToPlayback.js +++ b/seasoned_api/src/plex/stream/convertStreamToPlayback.js @@ -1,15 +1,14 @@ 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; - - } + 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; \ No newline at end of file +module.exports = convertStreamToPlayback; diff --git a/seasoned_api/src/plex/stream/convertStreamToPlayer.js b/seasoned_api/src/plex/stream/convertStreamToPlayer.js index bb64e2f..8d4abc5 100644 --- a/seasoned_api/src/plex/stream/convertStreamToPlayer.js +++ b/seasoned_api/src/plex/stream/convertStreamToPlayer.js @@ -1,13 +1,13 @@ const Player = require('src/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; + 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; + return player; } -module.exports = convertStreamToPlayer; \ No newline at end of file +module.exports = convertStreamToPlayer; diff --git a/seasoned_api/src/plex/stream/convertStreamToUser.js b/seasoned_api/src/plex/stream/convertStreamToUser.js index bce1c2a..6b356d2 100644 --- a/seasoned_api/src/plex/stream/convertStreamToUser.js +++ b/seasoned_api/src/plex/stream/convertStreamToUser.js @@ -1,7 +1,7 @@ const User = require('src/media_classes/user'); function convertStreamToUser(plexStream) { - return new User(plexStream.id, plexStream.title); + return new User(plexStream.id, plexStream.title); } -module.exports = convertStreamToUser; \ No newline at end of file +module.exports = convertStreamToUser;