Moved eveything related to the api to a seperate folder seasoned_api.
This commit is contained in:
22
seasoned_api/src/plex/convertPlexToMovie.js
Normal file
22
seasoned_api/src/plex/convertPlexToMovie.js
Normal file
@@ -0,0 +1,22 @@
|
||||
const Movie = require('src/media_classes/movie');
|
||||
|
||||
function convertPlexToMovie(plexMovie) {
|
||||
const movie = new Movie();
|
||||
movie.title = plexMovie.title;
|
||||
|
||||
if (plexMovie.type === 'episode') {
|
||||
movie.title = plexMovie.grandparentTitle;
|
||||
movie.childTitle = plexMovie.title;
|
||||
movie.season = plexMovie.parentIndex;
|
||||
movie.episode = plexMovie.index;
|
||||
}
|
||||
movie.year = plexMovie.year;
|
||||
movie.library = plexMovie.librarySectionTitle;
|
||||
movie.type = plexMovie.type;
|
||||
movie.poster = plexMovie.thumb;
|
||||
movie.background = plexMovie.art;
|
||||
|
||||
return movie;
|
||||
}
|
||||
|
||||
module.exports = convertPlexToMovie;
|
||||
17
seasoned_api/src/plex/convertPlexToStream.js
Normal file
17
seasoned_api/src/plex/convertPlexToStream.js
Normal file
@@ -0,0 +1,17 @@
|
||||
const convertPlexToMovie = require('src/plex/convertPlexToMovie');
|
||||
const convertStreamToMediaInfo = require('src/plex/convertStreamToMediaInfo');
|
||||
const convertStreamToPlayer = require('src/plex/stream/convertStreamToPlayer');
|
||||
const convertStreamToUser = require('src/plex/stream/convertStreamToUser');
|
||||
const ConvertStreamToPlayback = require('src/plex/stream/convertStreamToPlayback');
|
||||
|
||||
function convertPlexToStream(plexStream) {
|
||||
const stream = convertPlexToMovie(plexStream);
|
||||
stream.mediaInfo = convertStreamToMediaInfo(plexStream.Media);
|
||||
stream.player = convertStreamToPlayer(plexStream.Player);
|
||||
stream.user = convertStreamToUser(plexStream.User);
|
||||
stream.playback = new ConvertStreamToPlayback(plexStream.Media.Part);
|
||||
|
||||
return stream;
|
||||
}
|
||||
|
||||
module.exports = convertPlexToStream;
|
||||
21
seasoned_api/src/plex/convertStreamToMediaInfo.js
Normal file
21
seasoned_api/src/plex/convertStreamToMediaInfo.js
Normal file
@@ -0,0 +1,21 @@
|
||||
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;
|
||||
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;
|
||||
14
seasoned_api/src/plex/hookDump.js
Normal file
14
seasoned_api/src/plex/hookDump.js
Normal file
@@ -0,0 +1,14 @@
|
||||
/*
|
||||
* @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);
|
||||
}
|
||||
|
||||
module.exports = hookDumpController;
|
||||
26
seasoned_api/src/plex/mailTemplate.js
Normal file
26
seasoned_api/src/plex/mailTemplate.js
Normal file
@@ -0,0 +1,26 @@
|
||||
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;
|
||||
53
seasoned_api/src/plex/plexRepository.js
Normal file
53
seasoned_api/src/plex/plexRepository.js
Normal file
@@ -0,0 +1,53 @@
|
||||
const assert = require('assert');
|
||||
const convertPlexToMovie = require('src/plex/convertPlexToMovie');
|
||||
const convertPlexToStream = require('src/plex/convertPlexToStream');
|
||||
const configuration = require('src/config/configuration').getInstance();
|
||||
const TMDB = require('src/tmdb/tmdb');
|
||||
const tmdb = new TMDB(configuration.get('tmdb', 'apiKey'));
|
||||
var rp = require('request-promise');
|
||||
|
||||
class PlexRepository {
|
||||
|
||||
searchMedia(query) {
|
||||
var options = {
|
||||
uri: 'http://10.0.0.41:32400/search?query=' + query,
|
||||
headers: {
|
||||
'Accept': 'application/json'
|
||||
},
|
||||
json: true
|
||||
}
|
||||
|
||||
return rp(options)
|
||||
.then((result) => {
|
||||
return result.MediaContainer.Metadata.map(convertPlexToMovie);
|
||||
})
|
||||
.catch((err) => {
|
||||
throw new Error(err);
|
||||
})
|
||||
}
|
||||
|
||||
nowPlaying() {
|
||||
var options = {
|
||||
uri: 'http://10.0.0.41:32400/status/sessions',
|
||||
headers: {
|
||||
'Accept': 'application/json'
|
||||
},
|
||||
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': [] };
|
||||
}
|
||||
})
|
||||
.catch((err) => {
|
||||
throw new Error(err);
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = PlexRepository;
|
||||
123
seasoned_api/src/plex/requestRepository.js
Normal file
123
seasoned_api/src/plex/requestRepository.js
Normal file
@@ -0,0 +1,123 @@
|
||||
const assert = require('assert');
|
||||
const PlexRepository = require('src/plex/plexRepository');
|
||||
const plexRepository = new PlexRepository();
|
||||
const convertPlexToMovie = require('src/plex/convertPlexToMovie');
|
||||
const configuration = require('src/config/configuration').getInstance();
|
||||
const TMDB = require('src/tmdb/tmdb');
|
||||
const tmdb = new TMDB(configuration.get('tmdb', 'apiKey'));
|
||||
var Promise = require('bluebird');
|
||||
var rp = require('request-promise');
|
||||
|
||||
const MailTemplate = require('src/plex/mailTemplate')
|
||||
|
||||
var pythonShell = require('python-shell');
|
||||
const nodemailer = require('nodemailer');
|
||||
|
||||
|
||||
class RequestRepository {
|
||||
|
||||
searchRequest(query, page, type) {
|
||||
return Promise.resolve()
|
||||
.then(() => tmdb.search(query, page, type))
|
||||
.then((tmdbMovies) => {
|
||||
return Promise.resolve()
|
||||
.then(() => plexRepository.searchMedia(query))
|
||||
.then((plexMedia) => {
|
||||
return Promise.each(tmdbMovies, function(tmdbMovie) {
|
||||
return Promise.each(plexMedia, function(plexMovie) {
|
||||
if (tmdbMovie.title == plexMovie.title && tmdbMovie.year == plexMovie.year) {
|
||||
tmdbMovie.matchedInPlex = true;
|
||||
console.log(tmdbMovie.title + ' : ' + tmdbMovie.year);
|
||||
}
|
||||
return tmdbMovie;
|
||||
})
|
||||
})
|
||||
})
|
||||
})
|
||||
.catch((error) => {
|
||||
return error;
|
||||
});
|
||||
}
|
||||
|
||||
lookup(identifier, type = 'movie') {
|
||||
if (type === 'movie') { type = 'movieInfo'}
|
||||
else if (type === 'tv') { type = 'tvInfo'}
|
||||
return Promise.resolve()
|
||||
.then(() => tmdb.lookup(identifier, type))
|
||||
.then((tmdbMovie) => {
|
||||
return Promise.resolve(plexRepository.searchMedia(tmdbMovie.title))
|
||||
.then((plexMovies) => {
|
||||
for (var i = 0; i < plexMovies.length; i++) {
|
||||
if (tmdbMovie.title === plexMovies[i].title && tmdbMovie.year === plexMovies[i].year) {
|
||||
tmdbMovie.matchedInPlex = true;
|
||||
return tmdbMovie;
|
||||
}
|
||||
}
|
||||
})
|
||||
.catch((error) => {
|
||||
return error;
|
||||
});
|
||||
return tmdbMovie;
|
||||
});
|
||||
}
|
||||
|
||||
sendRequest(identifier) {
|
||||
// TODO try a cache hit on the movie item
|
||||
|
||||
tmdb.lookup(identifier).then(movie => {
|
||||
console.log(movie.title)
|
||||
|
||||
|
||||
// create reusable transporter object using the default SMTP transport
|
||||
let transporter = nodemailer.createTransport({
|
||||
host: configuration.get('mail', 'host'),
|
||||
port: 26,
|
||||
ignoreTLS: true,
|
||||
tls :{rejectUnauthorized: false},
|
||||
secure: false, // secure:true for port 465, secure:false for port 587
|
||||
auth: {
|
||||
user: configuration.get('mail', 'user'),
|
||||
pass: configuration.get('mail', 'password')
|
||||
}
|
||||
});
|
||||
|
||||
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 <support@kevinmidboe.com>', // 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);
|
||||
});
|
||||
|
||||
// var options = {
|
||||
// args: [movie.title, movie.year, movie.poster]
|
||||
// }
|
||||
|
||||
// pythonShell.run('sendRequest.py', options, function (err, results) {
|
||||
// if (err) throw err;
|
||||
// // TODO Add error handling!! RequestRepository.ERROR
|
||||
// // results is an array consisting of messages collected during execution
|
||||
|
||||
// console.log('results: %j', results)
|
||||
// })
|
||||
})
|
||||
|
||||
return Promise.resolve();
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
module.exports = RequestRepository;
|
||||
10
seasoned_api/src/plex/stream/convertStreamToPlayback.js
Normal file
10
seasoned_api/src/plex/stream/convertStreamToPlayback.js
Normal file
@@ -0,0 +1,10 @@
|
||||
class convertStreamToPlayback {
|
||||
constructor(plexStream) {
|
||||
this.bitrate = plexStream.bitrate;
|
||||
this.width = plexStream.width;
|
||||
this.height = plexStream.height;
|
||||
this.decision = plexStream.decision;
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = convertStreamToPlayback;
|
||||
13
seasoned_api/src/plex/stream/convertStreamToPlayer.js
Normal file
13
seasoned_api/src/plex/stream/convertStreamToPlayer.js
Normal file
@@ -0,0 +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;
|
||||
|
||||
return player;
|
||||
}
|
||||
|
||||
module.exports = convertStreamToPlayer;
|
||||
7
seasoned_api/src/plex/stream/convertStreamToUser.js
Normal file
7
seasoned_api/src/plex/stream/convertStreamToUser.js
Normal file
@@ -0,0 +1,7 @@
|
||||
const User = require('src/media_classes/user');
|
||||
|
||||
function convertStreamToUser(plexStream) {
|
||||
return new User(plexStream.id, plexStream.title);
|
||||
}
|
||||
|
||||
module.exports = convertStreamToUser;
|
||||
Reference in New Issue
Block a user