diff --git a/client/src/components/plex/PlexTokenForm.js b/client/src/components/plex/PlexTokenForm.js index e0737b0..304f098 100644 --- a/client/src/components/plex/PlexTokenForm.js +++ b/client/src/components/plex/PlexTokenForm.js @@ -10,7 +10,7 @@ import TextHeader from '../helpers/Header'; import styles from '../../css/materialize.css'; class PlexTokenForm extends React.Component { - state = {email: '', password: '', plexUrl: ''}; + state = {email: '', password: ''}; onFormSubmit = event => { event.preventDefault(); @@ -62,20 +62,7 @@ class PlexTokenForm extends React.Component { /> -
-
-

Plex Server URL

- - this.setState({plexUrl: e.target.value}) - } - /> -
-
+

Plex Password

diff --git a/server/services/plex/auth.js b/server/services/plex/auth.js index d3b6023..144e86a 100644 --- a/server/services/plex/auth.js +++ b/server/services/plex/auth.js @@ -1,10 +1,11 @@ +import parser from 'xml2json'; import uuid from 'uuid'; import btoa from 'btoa'; import request from 'request-promise'; const rxAuthToken = /authenticationToken="([^"]+)"/; -const urlParams = (username, password) => ({ +const tokenUrlParams = (username, password) => ({ url: 'https://plex.tv/users/sign_in.xml', headers: { 'X-Plex-Client-Identifier': uuid(), @@ -19,7 +20,7 @@ const encryptUserCreds = (username, password) => { const fetchToken = async (username, password) => { try { - const res = await request.post(urlParams(username, password)); + const res = await request.post(tokenUrlParams(username, password)); const token = res.match(rxAuthToken)[1]; return token; } catch (error) { @@ -27,4 +28,27 @@ const fetchToken = async (username, password) => { } }; -export default fetchToken; +const plexUrlParams = plexToken => ({ + url: 'https://plex.tv/pms/servers.xml', + headers: { + 'X-Plex-Client-Identifier': uuid(), + 'X-Plex-Token': plexToken, + }, +}); + +const plexUrl = async plexToken => { + try { + const res = await request.get(plexUrlParams(plexToken)); + const formattedResponse = JSON.parse(parser.toJson(res)); + const server = formattedResponse.MediaContainer.Server.filter( + server => server.port === '32400', + ); + + console.log(server); + return `http://${server[0].address}:${server[0].port}`; + } catch (error) { + return error.message; + } +}; + +export default {fetchToken, plexUrl}; diff --git a/server/services/plex/importData.js b/server/services/plex/importData.js index 6f0db68..2bc9371 100644 --- a/server/services/plex/importData.js +++ b/server/services/plex/importData.js @@ -7,7 +7,7 @@ import {Op} from 'sequelize'; const mdb = new MovieDb(config.server.movieApiKey); const importSections = async user => { - const sections = await plexApi.getSections(); + const sections = await plexApi.getSections(user); const dbSections = await createSections(sections, user); return dbSections; }; @@ -58,35 +58,37 @@ const createSections = (sections, user) => { }; const importLibraries = async user => { - const sections = await plexApi.getSections(); + const sections = await plexApi.getSections(user); return Promise.map(sections, section => { return importLibrary(section.key, user); }); }; -const importMostWatched = async () => { +const importMostWatched = async user => { // const sectionKeys = await models.PlexSection.findAll().then(sections => { // return sections.map(section => section.key.toString()); // }); const sectionKeys = [1, 2]; return Promise.map(sectionKeys, sectionKey => { - return importMostWatchedData(sectionKey); + return importMostWatchedData(sectionKey, user); }).catch(err => { console.log(err); }); }; -const importMostWatchedData = async sectionKey => { - const mostWatchedData = await plexApi.getMostWatched({sectionKey}); +const importMostWatchedData = async (sectionKey, user) => { + const mostWatchedData = await plexApi.getMostWatched({sectionKey}, user); const mostWatchedDbData = await updateLibrary(mostWatchedData); return mostWatchedDbData; }; const importLibrary = async (sectionKey, user) => { - console.log('user--', user); - const libraryData = await plexApi.getLibraryDataBySection({ - sectionKey, - }); + const libraryData = await plexApi.getLibraryDataBySection( + { + sectionKey, + }, + user, + ); const dbLibraryData = await createLibrary(libraryData, user); return dbLibraryData; }; diff --git a/server/services/plex/index.js b/server/services/plex/index.js index 4494921..1f5b64a 100644 --- a/server/services/plex/index.js +++ b/server/services/plex/index.js @@ -6,8 +6,9 @@ import helpers from '../helpers'; const getAuthToken = async (req, res) => { try { - const {email, password, plexUrl} = req.query; - const plexToken = await auth(email, password); + const {email, password} = req.query; + const plexToken = await auth.fetchToken(email, password); + const plexUrl = await auth.plexUrl(plexToken); const [rowsUpdate, updatedUser] = await models.User.update( {plexUrl, plexToken}, {returning: true, where: {googleId: req.user.googleId}}, @@ -21,7 +22,7 @@ const getAuthToken = async (req, res) => { const getUsers = (req, res) => { plexApi - .getUsers() + .getUsers(req.user) .then(users => { res.json(users); }) @@ -31,7 +32,7 @@ const getUsers = (req, res) => { const getMostWatched = async (req, res) => { try { const options = req.query; - const mostWatched = await plexApi.getMostWatched(options); + const mostWatched = await plexApi.getMostWatched(options, req.user); res.json(mostWatched); } catch (error) { res.json(error); @@ -40,7 +41,7 @@ const getMostWatched = async (req, res) => { const getSections = async (req, res) => { try { - const sections = await plexApi.getSections(); + const sections = await plexApi.getSections(req.user); res.json(sections); } catch (error) { res.json(error); @@ -50,7 +51,7 @@ const getSections = async (req, res) => { const getLibraryDataBySection = async (req, res) => { try { const options = {sectionId: req.params.id}; - const sections = await plexApi.getLibraryDataBySection(options); + const sections = await plexApi.getLibraryDataBySection(options, req.user); res.json(sections); } catch (error) { res.json(error); @@ -58,22 +59,23 @@ const getLibraryDataBySection = async (req, res) => { }; const importSections = async (req, res) => { - const sections = await importData.importSections(); + const sections = await importData.importSections(req.user); res.json(sections); }; const importLibraries = async (req, res) => { - const libraries = await importData.importLibraries(); + const libraries = await importData.importLibraries(req.user); res.json(libraries); }; const importMostWatched = async (req, res) => { - const libraries = await importData.importMostWatched(); + const libraries = await importData.importMostWatched(req.user); res.json(libraries); }; const importAll = async (req, res) => { try { + console.log('user', req.user); await importData.importSections(req.user); await importData.importLibraries(req.user); await importData.importMostWatched(req.user); diff --git a/server/services/plex/plexApi.js b/server/services/plex/plexApi.js index 93b1f84..31196a1 100644 --- a/server/services/plex/plexApi.js +++ b/server/services/plex/plexApi.js @@ -1,52 +1,52 @@ import config from '../../../config'; import helpers from '../helpers'; -const getUsersUrlParams = function(token) { +const getUsersUrlParams = function(user) { return { - host: config.plex.plexApiUrl, + host: user.plexUrl, path: '/users', queryParams: { - 'X-Plex-Token': config.plex.token || token, + 'X-Plex-Token': user.plexToken, }, }; }; -const getSectionsUrlParams = function() { +const getSectionsUrlParams = function(user) { return { - host: config.plex.plexUrl, + host: user.plexUrl, path: '/library/sections', queryParams: { - 'X-Plex-Token': config.plex.token, + 'X-Plex-Token': user.plexToken, }, }; }; -const mostWatchedUrlParams = function(accountId, sectionKey, limit = 10) { +const mostWatchedUrlParams = function(accountId, sectionKey, limit = 10, user) { return { - host: config.plex.plexUrl, + host: user.plexUrl, path: '/library/all/top', queryParams: { ...(accountId && {accountId}), ...(sectionKey && {type: sectionKey}), ...(limit && {limit}), - 'X-Plex-Token': config.plex.token, + 'X-Plex-Token': user.plexToken, }, }; }; -const getLibraryDataBySectionUrlParams = function(sectionId) { +const getLibraryDataBySectionUrlParams = function(sectionId, user) { return { - host: config.plex.plexUrl, + host: user.plexUrl, path: `/library/sections/${sectionId}/all`, queryParams: { - 'X-Plex-Token': config.plex.token, + 'X-Plex-Token': user.plexToken, }, }; }; -const getUsers = async function(token) { +const getUsers = async function(user) { try { - const urlParams = getUsersUrlParams(token); + const urlParams = getUsersUrlParams(user); const getUsersUrl = helpers.buildUrl(urlParams); const response = await helpers.request(getUsersUrl); return response.MediaContainer.User; @@ -55,9 +55,12 @@ const getUsers = async function(token) { } }; -const getMostWatched = async function({accountId, sectionKey, limit = 10}) { +const getMostWatched = async function( + {accountId, sectionKey, limit = 10}, + user, +) { try { - const urlParams = mostWatchedUrlParams(accountId, sectionKey, limit); + const urlParams = mostWatchedUrlParams(accountId, sectionKey, limit, user); const mostWatchedUrl = helpers.buildUrl(urlParams); const response = await helpers.request(mostWatchedUrl); return response.MediaContainer.Metadata; @@ -71,13 +74,12 @@ const getMostWatched = async function({accountId, sectionKey, limit = 10}) { } }; -const getSections = async function() { +const getSections = async function(user) { try { - const urlParams = getSectionsUrlParams(); + const urlParams = getSectionsUrlParams(user); const getSectionsUrl = helpers.buildUrl(urlParams); - console.log('sectionsUrl---', getSectionsUrl); const response = await helpers.request(getSectionsUrl); - console.log('mike', response); + console.log('mike-', response); return response.MediaContainer.Directory; } catch (error) { return { @@ -88,10 +90,10 @@ const getSections = async function() { } }; -const getLibraryDataBySection = async function({sectionKey}) { +const getLibraryDataBySection = async function({sectionKey}, user) { try { console.log('sectionId--', sectionKey); - const urlParams = getLibraryDataBySectionUrlParams(sectionKey); + const urlParams = getLibraryDataBySectionUrlParams(sectionKey, user); const getLibraryDataBySectionUrl = helpers.buildUrl(urlParams); const response = await helpers.request(getLibraryDataBySectionUrl); return response.MediaContainer.Metadata;