mirror of
https://github.com/mjrode/WhatToWatch.git
synced 2026-05-07 21:19:08 -05:00
Extracted plex service functions to a helper file.
This commit is contained in:
@@ -27,7 +27,6 @@ export default () => {
|
||||
|
||||
const port = server.get('port');
|
||||
|
||||
console.log('port', port);
|
||||
sequelize.sync().then(() => {
|
||||
server.listen(port, () => {
|
||||
console.log(`Express server listening on - http://${hostname}:${port}`);
|
||||
|
||||
@@ -4,7 +4,6 @@ const express = require('express');
|
||||
|
||||
const router = express.Router();
|
||||
|
||||
console.log('Made it here');
|
||||
router.use(plexController);
|
||||
|
||||
export default router;
|
||||
|
||||
@@ -1,4 +1,6 @@
|
||||
import axios from 'axios';
|
||||
import parser from 'xml2json';
|
||||
import buildUrlLibrary from 'build-url';
|
||||
|
||||
const formatResponse = response => {
|
||||
const xmlResponse = response.headers['content-type'].includes('xml');
|
||||
@@ -8,4 +10,43 @@ const formatResponse = response => {
|
||||
return response.data;
|
||||
};
|
||||
|
||||
export default formatResponse;
|
||||
const buildUrl = function(urlParams) {
|
||||
try {
|
||||
const params = urlParams;
|
||||
const {host} = params;
|
||||
delete params.host;
|
||||
const urlHash = params;
|
||||
|
||||
return buildUrlLibrary(host, urlHash);
|
||||
} catch (error) {
|
||||
console.log(error);
|
||||
return error;
|
||||
}
|
||||
};
|
||||
|
||||
const request = async function(url) {
|
||||
console.log('Request URL', url);
|
||||
return new Promise((resolve, reject) => {
|
||||
const httpClient = axios;
|
||||
httpClient
|
||||
.get(url)
|
||||
.then(response => {
|
||||
return resolve(formatResponse(response));
|
||||
})
|
||||
.catch(error => {
|
||||
if (error.response) {
|
||||
console.log('status', error.response.status);
|
||||
console.log('headers', error.response.headers);
|
||||
return reject(error.response);
|
||||
}
|
||||
if (error.request) {
|
||||
console.log('request', error.request);
|
||||
} else {
|
||||
console.log('Error', error.message);
|
||||
}
|
||||
return reject(error);
|
||||
});
|
||||
});
|
||||
};
|
||||
|
||||
export default {formatResponse, buildUrl, request};
|
||||
|
||||
@@ -12,7 +12,6 @@ const getAuthToken = async (req, res) => {
|
||||
};
|
||||
|
||||
const getUsers = async (req, res) => {
|
||||
console.log('getUsers-mike-');
|
||||
const users = await plexApi.getUsers();
|
||||
res.json(users);
|
||||
};
|
||||
@@ -33,6 +32,7 @@ const getLibraryDataBySection = async (req, res) => {
|
||||
res.json(sections);
|
||||
} catch (error) {
|
||||
console.log(error);
|
||||
res.json(error);
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
@@ -1,7 +1,5 @@
|
||||
import axios from 'axios';
|
||||
import buildUrlPack from 'build-url';
|
||||
import config from '../../../config';
|
||||
import formatResponse from './helpers';
|
||||
import helpers from './helpers';
|
||||
|
||||
const getUsersUrlParams = function() {
|
||||
return {
|
||||
@@ -49,71 +47,32 @@ const getLibraryDataBySectionUrlParams = function(req) {
|
||||
};
|
||||
};
|
||||
|
||||
const buildUrl = function(urlParams) {
|
||||
try {
|
||||
const params = urlParams;
|
||||
const {host} = params;
|
||||
delete params.host;
|
||||
const urlHash = params;
|
||||
|
||||
return buildUrlPack(host, urlHash);
|
||||
} catch (error) {
|
||||
console.log(error);
|
||||
return error;
|
||||
}
|
||||
};
|
||||
|
||||
const request = async function(url) {
|
||||
console.log('Request URL', url);
|
||||
return new Promise((resolve, reject) => {
|
||||
const httpClient = axios;
|
||||
httpClient
|
||||
.get(url)
|
||||
.then(response => {
|
||||
return resolve(formatResponse(response));
|
||||
})
|
||||
.catch(error => {
|
||||
if (error.response) {
|
||||
console.log('status', error.response.status);
|
||||
console.log('headers', error.response.headers);
|
||||
return reject(error.response);
|
||||
}
|
||||
if (error.request) {
|
||||
console.log('request', error.request);
|
||||
} else {
|
||||
console.log('Error', error.message);
|
||||
}
|
||||
return reject(error);
|
||||
});
|
||||
});
|
||||
};
|
||||
|
||||
const getUsers = async function() {
|
||||
const urlParams = getUsersUrlParams();
|
||||
const getUsersUrl = buildUrl(urlParams);
|
||||
const response = await request(getUsersUrl);
|
||||
const getUsersUrl = helpers.buildUrl(urlParams);
|
||||
const response = await helpers.request(getUsersUrl);
|
||||
return response.MediaContainer.User;
|
||||
};
|
||||
|
||||
const getMostWatched = async function(req) {
|
||||
const urlParams = mostWatchedUrlParams(req);
|
||||
const mostWatchedUrl = buildUrl(urlParams);
|
||||
const response = await request(mostWatchedUrl);
|
||||
const mostWatchedUrl = helpers.buildUrl(urlParams);
|
||||
const response = await helpers.request(mostWatchedUrl);
|
||||
return response.MediaContainer.Metadata;
|
||||
};
|
||||
|
||||
const getSections = async function() {
|
||||
const urlParams = getSectionsUrlParams();
|
||||
const getSectionsUrl = buildUrl(urlParams);
|
||||
const response = await request(getSectionsUrl);
|
||||
const getSectionsUrl = helpers.buildUrl(urlParams);
|
||||
const response = await helpers.request(getSectionsUrl);
|
||||
return response.MediaContainer.Directory;
|
||||
};
|
||||
|
||||
const getLibraryDataBySection = async function(req) {
|
||||
try {
|
||||
const urlParams = getLibraryDataBySectionUrlParams(req);
|
||||
const getLibraryDataBySectionUrl = buildUrl(urlParams);
|
||||
const response = await request(getLibraryDataBySectionUrl);
|
||||
const getLibraryDataBySectionUrl = helpers.buildUrl(urlParams);
|
||||
const response = await helpers.request(getLibraryDataBySectionUrl);
|
||||
return response.MediaContainer.Metadata;
|
||||
} catch (error) {
|
||||
return error;
|
||||
@@ -128,6 +87,4 @@ export default {
|
||||
getUsersUrlParams,
|
||||
getLibraryDataBySectionUrlParams,
|
||||
getSectionsUrlParams,
|
||||
buildUrl,
|
||||
request,
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user