Merge pull request #75 from KevinMidboe/api_fixes

Api fixes
This commit is contained in:
2018-02-05 15:02:10 +01:00
committed by GitHub
9 changed files with 84 additions and 32 deletions

View File

@@ -23,7 +23,7 @@ CREATE TABLE IF NOT EXISTS search_history (
CREATE TABLE IF NOT EXISTS requests(
id TEXT,
name TEXT,
title TEXT,
year NUMBER,
poster_path TEXT DEFAULT NULL,
background_path TEXT DEFAULT NULL,
@@ -54,4 +54,4 @@ CREATE TABLE IF NOT EXISTS shows(
show_names TEXT,
date_added DATE,
date_modified DATE DEFUALT CURRENT_DATE NOT NULL
);
);

View File

@@ -7,7 +7,7 @@ class Movie {
this.release_date = undefined;
this.summary = undefined;
this.rating = undefined;
this.poster = undefined;
this.poster_path = undefined;
this.background = undefined;
this.genre = undefined;
this.date_added = undefined;
@@ -18,4 +18,4 @@ class Movie {
}
}
module.exports = Movie;
module.exports = Movie;

View File

@@ -17,6 +17,15 @@ function convertPlexToSeasoned(plexObject) {
movie.mediaInfo = plexObject.Media;
// Don't need a for-loop when we have it in json format
file_sizes = []
for (let movie_info of plexObject.Media) {
for (let file_data of movie_info.Part) {
file_sizes.push(file_data.size)
}
}
movie.size = file_sizes;
return movie;
}
else if (mediaType === 'show') {
@@ -36,4 +45,4 @@ function convertPlexToSeasoned(plexObject) {
}
}
module.exports = convertPlexToSeasoned;
module.exports = convertPlexToSeasoned;

View File

@@ -7,7 +7,7 @@ class PlexRepository {
searchMedia(query) {
var options = {
uri: 'http://10.0.0.42:32400/search?query=' + query,
uri: 'http://10.0.0.44:32400/search?query=' + query,
headers: {
'Accept': 'application/json'
},
@@ -31,7 +31,7 @@ class PlexRepository {
nowPlaying() {
var options = {
uri: 'http://10.0.0.42:32400/status/sessions',
uri: 'http://10.0.0.44:32400/status/sessions',
headers: {
'Accept': 'application/json'
},

View File

@@ -25,6 +25,7 @@ class RequestRepository {
'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 ?",
}
}
@@ -43,6 +44,7 @@ class RequestRepository {
// runnning through the entire plex loop. Many loops, but safe.
let checkIfMatchesPlexObjects = function(title, year, plexarray) {
// Iterate all elements in plexarray
console.log(plexArray)
for (let plexItem of plexarray) {
// If matches with our title and year return true
if (plexItem.title === title && plexItem.year === year)
@@ -84,25 +86,68 @@ class RequestRepository {
}
lookup(identifier, type = 'movie') {
if (type === 'movie') { type = 'movieInfo'}
else if (type === 'tv') { type = 'tvInfo'}
// console.log('Lookup: ', identifier + ' : ' + type)
// 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;
// });
let tmdbType = undefined;
if (type === 'movie') { tmdbType = 'movieInfo'}
else if (type === 'tv') { tmdbType = 'tvInfo'}
return Promise.resolve()
.then(() => tmdb.lookup(identifier, type))
.then(() => tmdb.lookup(identifier, tmdbType))
.then((tmdbMovie) => this.checkID(tmdbMovie))
.then((tmdbMovie) => {
return Promise.resolve(plexRepository.searchMedia(tmdbMovie.title))
.then((plexMovies) => {
console.log('plexMovies lookup: ', plexMovies)
for (var i = 0; i < plexMovies.length; i++) {
if (tmdbMovie.title === plexMovies[i].title && tmdbMovie.year === plexMovies[i].year) {
console.log('matched')
tmdbMovie.matchedInPlex = true;
return tmdbMovie;
}
}
return tmdbMovie;
})
// Add it here
.catch((error) => {
return error;
});
console.log('there was a error:', error)
return 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) => {
let already_requested = false;
if (result)
already_requested = true
tmdbMovie.requested = already_requested;
return tmdbMovie;
});
})
}
/**
@@ -117,7 +162,7 @@ class RequestRepository {
user = 'NULL';
console.log(user)
// Add request to database
this.database.run(this.queries.insertRequest, [movie.id, movie.title, movie.year, movie.poster, movie.background, 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
@@ -171,4 +216,4 @@ class RequestRepository {
}
module.exports = RequestRepository;
module.exports = RequestRepository;

View File

@@ -25,8 +25,8 @@ function convertTmdbToSeasoned(tmdbObject, strictType=undefined) {
movie.id = tmdbObject.id;
movie.summary = tmdbObject.overview;
movie.rating = tmdbObject.vote_average;
movie.poster = tmdbObject.poster_path;
movie.background = tmdbObject.backdrop_path;
movie.poster_path = tmdbObject.poster_path;
movie.background_path = tmdbObject.backdrop_path;
movie.genre = tmdbObject.genre_ids;
movie.popularity = tmdbObject.popularity;
@@ -42,8 +42,8 @@ function convertTmdbToSeasoned(tmdbObject, strictType=undefined) {
show.id = tmdbObject.id;
show.summary = tmdbObject.overview;
show.rating = tmdbObject.vote_average;
show.poster = tmdbObject.poster_path;
show.background = tmdbObject.backdrop_path;
show.poster_path = tmdbObject.poster_path;
show.background_path = tmdbObject.backdrop_path;
show.genre = tmdbObject.genre_ids;
show.popularity = tmdbObject.popularity;

View File

@@ -76,9 +76,9 @@ class TMDB {
* @returns {Promise} succeeds if movie was found
*/
lookup(identifier, queryType = 'movie') {
var type;
if (queryType === 'movie') { type = 'movieInfo'}
else if (queryType === 'show') { type = 'tvInfo'}
var type, tmdbType;
if (queryType === 'movie' || queryType === 'movieInfo') { type = 'movie', tmdbType = 'movieInfo' }
else if (queryType === 'show' || queryType === 'tvInfo') { type = 'show', tmdbType = 'tvInfo' }
else {
return Promise.resolve()
.then(() => {
@@ -89,14 +89,12 @@ class TMDB {
const cacheKey = `${this.cacheTags.lookup}:${type}:${identifier}`;
return Promise.resolve()
.then(() => this.cache.get(cacheKey))
.catch(() => this.tmdb(type, query))
.catch(() => this.tmdb(tmdbType, query))
.catch(() => { throw new Error('Could not find a movie with that id.'); })
.then((response) => this.cache.set(cacheKey, response))
.then((response) => {
try {
var car = convertTmdbToSeasoned(response, queryType);
console.log(car);
return car;
return convertTmdbToSeasoned(response, type);
} catch (parseError) {
throw new Error('Could not parse movie.');
}
@@ -142,7 +140,6 @@ class TMDB {
const mappedResults = response.results.map((result) => {
return convertTmdbToSeasoned(result, type)
})
return [mappedResults, response.page, response.total_pages]
})
.catch((error) => { throw new Error(error)})

View File

@@ -7,6 +7,8 @@ const configuration = require('src/config/configuration').getInstance();
// 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());
// this will let us get the data from a POST
@@ -18,7 +20,6 @@ app.use(bodyParser.json());
/* Decode the Authorization header if provided */
// router.use(tokenToUser);
const port = 31459; // set our port
const router = express.Router();
const allowedOrigins = ['https://kevinmidboe.com', 'http://localhost:8080'];
@@ -28,7 +29,7 @@ app.use(bodyParser.urlencoded({ extended: true }));
// This is probably a correct middleware/router setup
/* Decode the Authorization header if provided */
/* Translate the user token to a user name */
router.use(tokenToUser);
// TODO: Should have a separate middleware/router for handling headers.
@@ -61,7 +62,7 @@ app.use(function onError(err, req, res, next) {
*/
router.post('/v1/user', require('./controllers/user/register.js'));
router.post('/v1/user/login', require('./controllers/user/login.js'));
router.get('/v1/user/history', mustBeAuthenticated, require('./controllers/user/history.js'));
router.get('/v1/user/history', require('./controllers/user/history.js'));
/**
* Seasoned
@@ -83,7 +84,7 @@ router.get('/v1/plex/hook', require('./controllers/plex/hookDump.js'));
/**
* Requests
*/
router.get('/v1/plex/requests/all', mustBeAuthenticated, require('./controllers/plex/fetchRequested.js'));
router.get('/v1/plex/requests/all', require('./controllers/plex/fetchRequested.js'));
router.put('/v1/plex/request/:requestId', mustBeAuthenticated, require('./controllers/plex/updateRequested.js'));
/**

View File

@@ -13,7 +13,7 @@ function historyController(req, res) {
requestRepository.fetchRequested()
.then((requestedItems) => {
res.send({ success: true, requestedItems });
res.send({ success: true, results: requestedItems, total_results: requestedItems.length });
})
.catch((error) => {
res.status(401).send({ success: false, error: error.message });