-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #111 from KevinMidboe/api/v2
Api/v2
- Loading branch information
Showing
89 changed files
with
6,270 additions
and
1,687 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,6 @@ | ||
.DS_Store | ||
|
||
development.json | ||
env | ||
shows.db | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
language: node_js | ||
node_js: '8.7.0' | ||
node_js: '11.9.0' | ||
git: | ||
submodules: true | ||
script: | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,6 +8,9 @@ | |
"tmdb": { | ||
"apiKey": "bogus-api-key" | ||
}, | ||
"plex": { | ||
"ip": "0.0.0.0" | ||
}, | ||
"raven": { | ||
"DSN": "" | ||
}, | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
DROP TABLE IF EXISTS user; | ||
DROP TABLE IF EXISTS search_history; | ||
DROP TABLE IF EXISTS requests; | ||
DROP TABLE IF EXISTS request; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
const Episode = require('src/plex/types/episode'); | ||
|
||
function convertPlexToEpisode(plexEpisode) { | ||
const episode = new Episode(plexEpisode.title, plexEpisode.grandparentTitle, plexEpisode.year); | ||
episode.season = plexEpisode.parentIndex; | ||
episode.episode = plexEpisode.index; | ||
episode.summary = plexEpisode.summary; | ||
episode.rating = plexEpisode.rating; | ||
|
||
if (plexEpisode.viewCount !== undefined) { | ||
episode.views = plexEpisode.viewCount; | ||
} | ||
|
||
if (plexEpisode.originallyAvailableAt !== undefined) { | ||
episode.airdate = new Date(plexEpisode.originallyAvailableAt) | ||
} | ||
|
||
return episode; | ||
} | ||
module.exports = convertPlexToEpisode; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
const Movie = require('src/plex/types/movie'); | ||
|
||
function convertPlexToMovie(plexMovie) { | ||
const movie = new Movie(plexMovie.title, plexMovie.year); | ||
movie.rating = plexMovie.rating; | ||
movie.tagline = plexMovie.tagline; | ||
|
||
if (plexMovie.summary !== undefined) { | ||
movie.summary = plexMovie.summary; | ||
} | ||
|
||
return movie; | ||
} | ||
|
||
module.exports = convertPlexToMovie; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
const Show = require('src/plex/types/show'); | ||
|
||
function convertPlexToShow(plexShow) { | ||
const show = new Show(plexShow.title, plexShow.year); | ||
show.summary = plexShow.summary; | ||
show.rating = plexShow.rating; | ||
show.seasons = plexShow.childCount; | ||
show.episodes = plexShow.leafCount; | ||
|
||
return show; | ||
} | ||
|
||
module.exports = convertPlexToShow; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
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, port=32400) { | ||
this.plexIP = ip | ||
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 this.search(tmdbMovie.title) | ||
.then(plexMovies => plexMovies.some(plex => this.matchTmdbAndPlexMedia(plex, 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 = { | ||
timeout: 2000, | ||
headers: { 'Accept': 'application/json' } | ||
} | ||
|
||
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) { | ||
if (response === undefined || response.MediaContainer === undefined) { | ||
console.log('response was not valid to map', response) | ||
return [] | ||
} | ||
|
||
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() | ||
} | ||
} | ||
|
||
module.exports = Plex; |
Oops, something went wrong.