Added timeout to plex requests and include error in error message when unable to search
This commit is contained in:
@@ -1,24 +1,26 @@
|
|||||||
const axios = require('axios');
|
const axios = require('axios')
|
||||||
const convertPlexToMovie = require('src/plex/convertPlexToMovie');
|
const convertPlexToMovie = require('src/plex/convertPlexToMovie')
|
||||||
const convertPlexToShow = require('src/plex/convertPlexToShow');
|
const convertPlexToShow = require('src/plex/convertPlexToShow')
|
||||||
const convertPlexToEpisode = require('src/plex/convertPlexToEpisode');
|
const convertPlexToEpisode = require('src/plex/convertPlexToEpisode')
|
||||||
|
|
||||||
class Plex {
|
class Plex {
|
||||||
constructor(ip) {
|
constructor(ip) {
|
||||||
this.plexIP = ip;
|
this.plexIP = ip
|
||||||
this.plexPort = 32400;
|
this.plexPort = 32400
|
||||||
}
|
}
|
||||||
|
|
||||||
existsInPlex(tmdbMovie) {
|
existsInPlex(tmdbMovie) {
|
||||||
return Promise.resolve()
|
return Promise.resolve()
|
||||||
.then(() => this.search(tmdbMovie.title))
|
.then(() => this.search(tmdbMovie.title))
|
||||||
|
// TODO handle this when whitelist of local ip is not set in plex
|
||||||
|
.catch((error) => { console.error('Unable to search plex')})
|
||||||
.then((plexMovies) => {
|
.then((plexMovies) => {
|
||||||
const matches = plexMovies.some((plexMovie) => {
|
const matches = plexMovies.some((plexMovie) => {
|
||||||
return tmdbMovie.title === plexMovie.title && tmdbMovie.type === plexMovie.type;
|
return tmdbMovie.title === plexMovie.title && tmdbMovie.type === plexMovie.type
|
||||||
})
|
})
|
||||||
|
|
||||||
tmdbMovie.existsInPlex = matches;
|
tmdbMovie.existsInPlex = matches
|
||||||
return tmdbMovie;
|
return tmdbMovie
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -27,26 +29,27 @@ class Plex {
|
|||||||
baseURL: `http://${this.plexIP}:${this.plexPort}`,
|
baseURL: `http://${this.plexIP}:${this.plexPort}`,
|
||||||
url: '/hubs/search',
|
url: '/hubs/search',
|
||||||
params: { query: query },
|
params: { query: query },
|
||||||
responseType: 'json'
|
responseType: 'json',
|
||||||
|
timeout: 3000
|
||||||
}
|
}
|
||||||
|
|
||||||
return Promise.resolve()
|
return Promise.resolve()
|
||||||
.then(() => axios.request(options))
|
.then(() => axios.request(options))
|
||||||
.catch((error) => { throw new Error(`Unable to search plex library`); })
|
.catch((error) => { throw new Error(`Unable to search plex library`, error) })
|
||||||
.then(response => this.mapResults(response));
|
.then(response => this.mapResults(response))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
mapResults(response) {
|
mapResults(response) {
|
||||||
return response.data.MediaContainer.Hub.reduce((result, hub) => {
|
return response.data.MediaContainer.Hub.reduce((result, hub) => {
|
||||||
if (hub.type === 'movie' && hub.Metadata !== undefined) {
|
if (hub.type === 'movie' && hub.Metadata !== undefined) {
|
||||||
return [...result, ...hub.Metadata.map(convertPlexToMovie)];
|
return [...result, ...hub.Metadata.map(convertPlexToMovie)]
|
||||||
}
|
}
|
||||||
else if (hub.type === 'show' && hub.Metadata !== undefined) {
|
else if (hub.type === 'show' && hub.Metadata !== undefined) {
|
||||||
return [...result, ...hub.Metadata.map(convertPlexToShow)];
|
return [...result, ...hub.Metadata.map(convertPlexToShow)]
|
||||||
}
|
}
|
||||||
else if (hub.type === 'episode' && hub.Metadata !== undefined) {
|
else if (hub.type === 'episode' && hub.Metadata !== undefined) {
|
||||||
return [...result, ...hub.Metadata.map(convertPlexToEpisode)];
|
return [...result, ...hub.Metadata.map(convertPlexToEpisode)]
|
||||||
}
|
}
|
||||||
|
|
||||||
return result
|
return result
|
||||||
|
|||||||
Reference in New Issue
Block a user