Moved, renamed, re-did and added a lot of stuff. Getting ready for the v2 upgrade
This commit is contained in:
@@ -1,59 +1,89 @@
|
||||
const axios = require('axios')
|
||||
const fetch = require('node-fetch')
|
||||
const convertPlexToMovie = require('src/plex/convertPlexToMovie')
|
||||
const convertPlexToShow = require('src/plex/convertPlexToShow')
|
||||
const convertPlexToEpisode = require('src/plex/convertPlexToEpisode')
|
||||
|
||||
|
||||
const { Movie, Show, Person } = require('src/tmdb/types');
|
||||
|
||||
// const { Movie, }
|
||||
// TODO? import class definitions to compare types ?
|
||||
// what would typescript do?
|
||||
|
||||
class Plex {
|
||||
constructor(ip) {
|
||||
constructor(ip, port=32400) {
|
||||
this.plexIP = ip
|
||||
this.plexPort = 32400
|
||||
this.plexPort = port
|
||||
}
|
||||
|
||||
matchTmdbAndPlexMedia(plex, tmdb) {
|
||||
if (plex === undefined || tmdb === undefined)
|
||||
return false
|
||||
|
||||
const sanitize = (string) => string.toLowerCase()
|
||||
|
||||
const matchTitle = sanitize(plex.title) === sanitize(tmdb.title)
|
||||
const matchYear = plex.year === tmdb.year
|
||||
|
||||
return matchTitle && matchYear
|
||||
}
|
||||
|
||||
existsInPlex(tmdbMovie) {
|
||||
return Promise.resolve()
|
||||
.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) => {
|
||||
const matches = plexMovies.some((plexMovie) => {
|
||||
return tmdbMovie.title === plexMovie.title && tmdbMovie.type === plexMovie.type
|
||||
})
|
||||
return this.search(tmdbMovie.title)
|
||||
.then(plexMovies => plexMovies.some(plex => this.matchTmdbAndPlexMedia(plex, tmdbMovie)))
|
||||
}
|
||||
|
||||
tmdbMovie.existsInPlex = matches
|
||||
return tmdbMovie
|
||||
})
|
||||
successfullResponse(response) {
|
||||
const { status, statusText } = response
|
||||
|
||||
if (status === 200) {
|
||||
return response.json()
|
||||
} else {
|
||||
throw { message: statusText, status: status }
|
||||
}
|
||||
}
|
||||
|
||||
search(query) {
|
||||
const url = `http://${this.plexIP}:${this.plexPort}/hubs/search?query=${query}`
|
||||
const options = {
|
||||
baseURL: `http://${this.plexIP}:${this.plexPort}`,
|
||||
url: '/hubs/search',
|
||||
params: { query: query },
|
||||
responseType: 'json',
|
||||
timeout: 3000
|
||||
timeout: 2000,
|
||||
headers: { 'Accept': 'application/json' }
|
||||
}
|
||||
|
||||
return Promise.resolve()
|
||||
.then(() => axios.request(options))
|
||||
.catch((error) => { throw new Error(`Unable to search plex library`, error) })
|
||||
.then(response => this.mapResults(response))
|
||||
return fetch(url, options)
|
||||
.then(this.successfullResponse)
|
||||
.then(this.mapResults)
|
||||
.catch(error => {
|
||||
if (error.type === 'request-timeout') {
|
||||
throw { message: 'Plex did not respond', status: 408, success: false }
|
||||
}
|
||||
|
||||
throw error
|
||||
})
|
||||
}
|
||||
|
||||
|
||||
mapResults(response) {
|
||||
return response.data.MediaContainer.Hub.reduce((result, hub) => {
|
||||
if (hub.type === 'movie' && hub.Metadata !== undefined) {
|
||||
return [...result, ...hub.Metadata.map(convertPlexToMovie)]
|
||||
}
|
||||
else if (hub.type === 'show' && hub.Metadata !== undefined) {
|
||||
return [...result, ...hub.Metadata.map(convertPlexToShow)]
|
||||
}
|
||||
else if (hub.type === 'episode' && hub.Metadata !== undefined) {
|
||||
return [...result, ...hub.Metadata.map(convertPlexToEpisode)]
|
||||
}
|
||||
if (response === undefined || response.MediaContainer === undefined) {
|
||||
console.log('response was not valid to map', response)
|
||||
return []
|
||||
}
|
||||
|
||||
return result
|
||||
}, [])
|
||||
return response.MediaContainer.Hub
|
||||
.filter(category => category.size > 0)
|
||||
.map(category => {
|
||||
if (category.type === 'movie') {
|
||||
return category.Metadata.map(movie => {
|
||||
const ovie = Movie.convertFromPlexResponse(movie)
|
||||
return ovie.createJsonResponse()
|
||||
})
|
||||
} else if (category.type === 'show') {
|
||||
return category.Metadata.map(convertPlexToShow)
|
||||
} else if (category.type === 'episode') {
|
||||
return category.Metadata.map(convertPlexToEpisode)
|
||||
}
|
||||
})
|
||||
.filter(result => result !== undefined)
|
||||
.flat()
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user