diff --git a/.DS_Store b/.DS_Store new file mode 100644 index 0000000..1ad899d Binary files /dev/null and b/.DS_Store differ diff --git a/.gitignore b/.gitignore index 68d0f48..d5d6790 100644 --- a/.gitignore +++ b/.gitignore @@ -4,3 +4,4 @@ node_modules .nyc_output dist npm-debug.log +coverage \ No newline at end of file diff --git a/coverage/config/index.html b/coverage/config/index.html new file mode 100644 index 0000000..5764621 --- /dev/null +++ b/coverage/config/index.html @@ -0,0 +1,123 @@ + + +
++ Press n or j to go to the next uncovered block, b, p or k for the previous block. +
++ Press n or j to go to the next uncovered block, b, p or k for the previous block. +
+| 1 +2 +3 +4 +5 +6 +7 +8 +9 +10 +11 +12 +13 +14 +15 | +1x +1x +1x + +1x + +1x +1x + +1x + + + + | /* eslint-disable no-undef */
+require('custom-env').env();
+require('dotenv').config();
+const _ = require('lodash');
+
+const env = process.env.NODE_ENV || 'local';
+
+const envConfig = require(`./${env}`).default;
+const plexConfig = require('./plex').default;
+
+const defaultConfig = {
+ env,
+};
+export default {server: _.merge(defaultConfig, envConfig), plex: plexConfig};
+ |
+ Press n or j to go to the next uncovered block, b, p or k for the previous block. +
+| 1 +2 +3 +4 +5 +6 +7 +8 | 1x + + + + + + + | const plexConfig = {
+ ip: 'http://192.168.0.44',
+ plexServerUrl: 'https://plex.mjrflix.com',
+ plexApiUrl: 'https://plex.tv/api',
+ token: process.env.PLEX_API_TOKEN,
+};
+export default plexConfig;
+ |
+ Press n or j to go to the next uncovered block, b, p or k for the previous block. +
+| 1 +2 +3 +4 +5 +6 +7 | 1x + + + + + + | const testConfig = {
+ hostname: 'localhost',
+ port: 8080,
+};
+
+export default testConfig;
+ |
@@ -89,19 +89,19 @@
+ Press n or j to go to the next uncovered block, b, p or k for the previous block. +
+| File | ++ | Statements | ++ | Branches | ++ | Functions | ++ | Lines | ++ |
|---|---|---|---|---|---|---|---|---|---|
| plex.controller.js | +100% | +9/9 | +100% | +0/0 | +100% | +0/0 | +100% | +9/9 | +
+ Press n or j to go to the next uncovered block, b, p or k for the previous block. +
+| 1 +2 +3 +4 +5 +6 +7 +8 +9 +10 +11 +12 +13 +14 +15 +16 +17 +18 +19 +20 +21 | + + +1x + +1x + + +1x + +1x +1x + +1x +1x + +1x +1x + + + | import {Router} from 'express';
+import plexService from '../services/plex';
+
+const router = Router();
+
+router.get('/auth', plexService.getAuthToken);
+
+// users
+router.get('/users', plexService.getUsers);
+
+router.get('/most-watched', plexService.getMostWatched);
+router.get('/import/most-watched', plexService.importMostWatched);
+
+router.get('/sections', plexService.getSections);
+router.get('/import/sections', plexService.importSections);
+
+router.get('/library/:id', plexService.getLibraryDataBySection);
+router.get('/import/libraries', plexService.importLibraries);
+
+export default router;
+ |
@@ -63,15 +63,15 @@
@@ -89,14 +89,10 @@ 41 42 43 -44 -45 -46 -47
import express from 'express';
import {json, urlencoded} from 'body-parser';
import {sequelize} from './models';
-
-const routes = require('./routes').default;
+import plex from './routes/plex.route';
export default () => {
const server = express();
@@ -158,7 +151,8 @@ export default () => {
server.use(urlencoded({extended: true}));
// Set up routes
- routes.init(server);
+ server.use('/plex', plex);
+
return server;
};
@@ -167,14 +161,11 @@ export default () => {
const port = server.get('port');
+ console.log('port', port);
sequelize.sync().then(() => {
- if (!module.parent) {
- server.listen(port, () => {
- console.log(
- `Express server listening on - http://${hostname}:${port}`,
- );
- });
- }
+ server.listen(port, () => {
+ console.log(`Express server listening on - http://${hostname}:${port}`);
+ });
});
};
@@ -190,7 +181,7 @@ export default () => {
diff --git a/coverage/recommend/server/models/index.html b/coverage/recommend/server/models/index.html
index a965ccd..bcce8b9 100644
--- a/coverage/recommend/server/models/index.html
+++ b/coverage/recommend/server/models/index.html
@@ -119,7 +119,7 @@
diff --git a/coverage/recommend/server/models/index.js.html b/coverage/recommend/server/models/index.js.html
index 60c9702..3fc192a 100644
--- a/coverage/recommend/server/models/index.js.html
+++ b/coverage/recommend/server/models/index.js.html
@@ -136,7 +136,7 @@ export default models;
diff --git a/coverage/recommend/server/models/plexLibrary.js.html b/coverage/recommend/server/models/plexLibrary.js.html
index 592809b..845df04 100644
--- a/coverage/recommend/server/models/plexLibrary.js.html
+++ b/coverage/recommend/server/models/plexLibrary.js.html
@@ -112,7 +112,7 @@ export default plexLibrary;
diff --git a/coverage/recommend/server/models/plexSection.js.html b/coverage/recommend/server/models/plexSection.js.html
index 1c3703f..f944a44 100644
--- a/coverage/recommend/server/models/plexSection.js.html
+++ b/coverage/recommend/server/models/plexSection.js.html
@@ -97,7 +97,7 @@ export default plexSection;
diff --git a/coverage/recommend/server/models/user.js.html b/coverage/recommend/server/models/user.js.html
index ebfbef5..914aae5 100644
--- a/coverage/recommend/server/models/user.js.html
+++ b/coverage/recommend/server/models/user.js.html
@@ -142,7 +142,7 @@ export default user;
diff --git a/coverage/recommend/server/routes/index.html b/coverage/recommend/server/routes/index.html
index d6b34a6..f1a22f1 100644
--- a/coverage/recommend/server/routes/index.html
+++ b/coverage/recommend/server/routes/index.html
@@ -20,31 +20,31 @@
- 75%
+ 100%
Statements
- 6/8
+ 4/4
- 0%
+ 100%
Branches
- 0/2
+ 0/0
- 66.67%
+ 100%
Functions
- 2/3
+ 0/0
- 75%
+ 100%
Lines
- 6/8
+ 4/4
Press n or j to go to the next uncovered block, b, p or k for the previous block.
-
+
@@ -62,16 +62,16 @@
- index.js
-
- 75%
- 6/8
- 0%
- 0/2
- 66.67%
- 2/3
- 75%
- 6/8
+ plex.route.js
+
+ 100%
+ 4/4
+ 100%
+ 0/0
+ 100%
+ 0/0
+ 100%
+ 4/4
@@ -80,7 +80,7 @@
diff --git a/coverage/recommend/server/routes/plex.route.js.html b/coverage/recommend/server/routes/plex.route.js.html
new file mode 100644
index 0000000..5de0769
--- /dev/null
+++ b/coverage/recommend/server/routes/plex.route.js.html
@@ -0,0 +1,99 @@
+
+
+
+ Code coverage report for recommend/server/routes/plex.route.js
+
+
+
+
+
+
+
+
+
+
+ All files / recommend/server/routes plex.route.js
+
+
+
+ 100%
+ Statements
+ 4/4
+
+
+ 100%
+ Branches
+ 0/0
+
+
+ 100%
+ Functions
+ 0/0
+
+
+ 100%
+ Lines
+ 4/4
+
+
+
+ Press n or j to go to the next uncovered block, b, p or k for the previous block.
+
+
+
+
+1
+2
+3
+4
+5
+6
+7
+8
+9
+10
+11
+
+1x
+
+1x
+
+1x
+1x
+
+
+ import plexController from '../controllers/plex.controller';
+
+const express = require('express');
+
+const router = express.Router();
+
+console.log('Made it here');
+router.use(plexController);
+
+export default router;
+
+
+
+
+
+
+
+
+
+
+
+
diff --git a/coverage/recommend/server/services/plex/auth.js.html b/coverage/recommend/server/services/plex/auth.js.html
new file mode 100644
index 0000000..a80bfb1
--- /dev/null
+++ b/coverage/recommend/server/services/plex/auth.js.html
@@ -0,0 +1,147 @@
+
+
+
+ Code coverage report for recommend/server/services/plex/auth.js
+
+
+
+
+
+
+
+
+
+
+ All files / recommend/server/services/plex auth.js
+
+
+
+ 40%
+ Statements
+ 4/10
+
+
+ 100%
+ Branches
+ 0/0
+
+
+ 0%
+ Functions
+ 0/3
+
+
+ 44.44%
+ Lines
+ 4/9
+
+
+
+ Press n or j to go to the next uncovered block, b, p or k for the previous block.
+
+
+
+
+1
+2
+3
+4
+5
+6
+7
+8
+9
+10
+11
+12
+13
+14
+15
+16
+17
+18
+19
+20
+21
+22
+23
+24
+25
+26
+27
+
+
+
+1x
+
+1x
+
+
+
+
+
+
+
+1x
+
+
+
+
+1x
+
+
+
+
+
+
+ import uuid from 'uuid';
+import btoa from 'btoa';
+import request from 'request-promise';
+
+const rxAuthToken = /authenticationToken="([^"]+)"/;
+
+const urlParams = (username, password) => ({
+ url: 'https://plex.tv/users/sign_in.xml',
+ headers: {
+ 'X-Plex-Client-Identifier': uuid(),
+ Authorization: `Basic ${encryptUserCreds(username, password)}`,
+ },
+});
+
+const encryptUserCreds = (username, password) => {
+ const creds = `${username}:${password}`;
+ return btoa(creds);
+};
+
+const fetchToken = async (username, password) => {
+ const res = await request.post(urlParams(username, password));
+ const token = res.match(rxAuthToken)[1];
+ return token;
+};
+
+export default fetchToken;
+
+
+
+
+
+
+
+
+
+
+
+
diff --git a/coverage/recommend/server/services/plex/helpers.js.html b/coverage/recommend/server/services/plex/helpers.js.html
new file mode 100644
index 0000000..676e62b
--- /dev/null
+++ b/coverage/recommend/server/services/plex/helpers.js.html
@@ -0,0 +1,102 @@
+
+
+
+ Code coverage report for recommend/server/services/plex/helpers.js
+
+
+
+
+
+
+
+
+
+
+ All files / recommend/server/services/plex helpers.js
+
+
+
+ 100%
+ Statements
+ 5/5
+
+
+ 100%
+ Branches
+ 2/2
+
+
+ 100%
+ Functions
+ 1/1
+
+
+ 100%
+ Lines
+ 5/5
+
+
+
+ Press n or j to go to the next uncovered block, b, p or k for the previous block.
+
+
+
+
+1
+2
+3
+4
+5
+6
+7
+8
+9
+10
+11
+12
+
+1x
+7x
+7x
+3x
+
+4x
+
+
+
+ import parser from 'xml2json';
+
+const formatResponse = response => {
+ const xmlResponse = response.headers['content-type'].includes('xml');
+ if (xmlResponse) {
+ return JSON.parse(parser.toJson(response.data));
+ }
+ return response.data;
+};
+
+export default formatResponse;
+
+
+
+
+
+
+
+
+
+
+
+
diff --git a/coverage/recommend/server/services/plex/importData.js.html b/coverage/recommend/server/services/plex/importData.js.html
new file mode 100644
index 0000000..3cc76fc
--- /dev/null
+++ b/coverage/recommend/server/services/plex/importData.js.html
@@ -0,0 +1,354 @@
+
+
+
+ Code coverage report for recommend/server/services/plex/importData.js
+
+
+
+
+
+
+
+
+
+
+ All files / recommend/server/services/plex importData.js
+
+
+
+ 24.14%
+ Statements
+ 7/29
+
+
+ 100%
+ Branches
+ 0/0
+
+
+ 0%
+ Functions
+ 0/12
+
+
+ 24.14%
+ Lines
+ 7/29
+
+
+
+ Press n or j to go to the next uncovered block, b, p or k for the previous block.
+
+
+
+
+1
+2
+3
+4
+5
+6
+7
+8
+9
+10
+11
+12
+13
+14
+15
+16
+17
+18
+19
+20
+21
+22
+23
+24
+25
+26
+27
+28
+29
+30
+31
+32
+33
+34
+35
+36
+37
+38
+39
+40
+41
+42
+43
+44
+45
+46
+47
+48
+49
+50
+51
+52
+53
+54
+55
+56
+57
+58
+59
+60
+61
+62
+63
+64
+65
+66
+67
+68
+69
+70
+71
+72
+73
+74
+75
+76
+77
+78
+79
+80
+81
+82
+83
+84
+85
+86
+87
+88
+89
+90
+91
+92
+93
+94
+95
+96
+
+
+1x
+
+
+
+
+
+
+1x
+
+
+
+
+
+
+
+
+
+
+1x
+
+
+
+
+
+
+
+1x
+
+
+
+
+
+
+
+1x
+
+
+
+
+
+
+
+
+1x
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+1x
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+ import plexApiClient from './plexApi';
+import models from '../../models';
+
+const importSections = async () => {
+ const plexApi = plexApiClient();
+ const sections = await plexApi.getSections();
+ createSections(sections);
+ return sections;
+};
+
+const createSections = sections => {
+ sections.forEach(async section => {
+ await models.PlexSection.create({
+ userId: 1,
+ title: section.title,
+ type: section.type,
+ key: section.key,
+ });
+ });
+};
+
+const importLibraries = async () => {
+ const plexApi = plexApiClient();
+ const sections = await plexApi.getSections();
+ sections.forEach(async section => {
+ await importLibrary(section.key);
+ });
+};
+
+const importMostWatched = async req => {
+ const plexApi = plexApiClient();
+ const mostWatched = await plexApi.getMostWatched(req);
+ mostWatched.forEach(async libraryData => {
+ await updateLibrary([libraryData]);
+ });
+};
+
+const importLibrary = async sectionId => {
+ const plexApi = plexApiClient();
+ const libraryData = await plexApi.getLibraryDataBySection({
+ sectionId,
+ });
+ createLibrary(libraryData);
+ return libraryData;
+};
+
+const updateLibrary = libraryData => {
+ libraryData.forEach(async data => {
+ await models.PlexLibrary.update(
+ {
+ userId: 1,
+ title: data.title,
+ type: data.type,
+ views: data.globalViewCount,
+ rating_key: data.ratingKey,
+ metadata_path: data.key,
+ summary: data.summary,
+ rating: data.rating,
+ year: data.year,
+ genre: JSON.stringify(data.Genre),
+ },
+ {
+ where: {
+ title: data.title,
+ },
+ },
+ );
+ });
+};
+
+const createLibrary = libraryData => {
+ libraryData.forEach(async data => {
+ await models.PlexLibrary.upsert(
+ {
+ userId: 1,
+ title: data.title,
+ type: data.type,
+ views: data.views,
+ rating_key: data.ratingKey,
+ metadata_path: data.key,
+ summary: data.summary,
+ rating: data.rating,
+ year: data.year,
+ genre: JSON.stringify(data.Genre),
+ },
+ {
+ where: {
+ title: data.title,
+ },
+ },
+ );
+ });
+};
+
+export default {importSections, importLibraries, importMostWatched};
+
+
+
+
+
+
+
+
+
+
+
+
diff --git a/coverage/recommend/server/services/plex/index.html b/coverage/recommend/server/services/plex/index.html
new file mode 100644
index 0000000..67e096d
--- /dev/null
+++ b/coverage/recommend/server/services/plex/index.html
@@ -0,0 +1,149 @@
+
+
+
+ Code coverage report for recommend/server/services/plex
+
+
+
+
+
+
+
+
+
+
+ All files recommend/server/services/plex
+
+
+
+ 60.16%
+ Statements
+ 77/128
+
+
+ 72.22%
+ Branches
+ 13/18
+
+
+ 44.74%
+ Functions
+ 17/38
+
+
+ 60.63%
+ Lines
+ 77/127
+
+
+
+ Press n or j to go to the next uncovered block, b, p or k for the previous block.
+
+
+
+
+
+
+
+ File
+
+ Statements
+
+ Branches
+
+ Functions
+
+ Lines
+
+
+
+
+ auth.js
+
+ 40%
+ 4/10
+ 100%
+ 0/0
+ 0%
+ 0/3
+ 44.44%
+ 4/9
+
+
+
+ helpers.js
+
+ 100%
+ 5/5
+ 100%
+ 2/2
+ 100%
+ 1/1
+ 100%
+ 5/5
+
+
+
+ importData.js
+
+ 24.14%
+ 7/29
+ 100%
+ 0/0
+ 0%
+ 0/12
+ 24.14%
+ 7/29
+
+
+
+ index.js
+
+ 60%
+ 18/30
+ 100%
+ 0/0
+ 44.44%
+ 4/9
+ 60%
+ 18/30
+
+
+
+ plexApi.js
+
+ 79.63%
+ 43/54
+ 68.75%
+ 11/16
+ 92.31%
+ 12/13
+ 79.63%
+ 43/54
+
+
+
+
+
+
+
+
+
+
+
+
+
+
diff --git a/coverage/recommend/server/services/plex/index.js.html b/coverage/recommend/server/services/plex/index.js.html
new file mode 100644
index 0000000..8429ff0
--- /dev/null
+++ b/coverage/recommend/server/services/plex/index.js.html
@@ -0,0 +1,261 @@
+
+
+
+ Code coverage report for recommend/server/services/plex/index.js
+
+
+
+
+
+
+
+
+
+
+ All files / recommend/server/services/plex index.js
+
+
+
+ 60%
+ Statements
+ 18/30
+
+
+ 100%
+ Branches
+ 0/0
+
+
+ 44.44%
+ Functions
+ 4/9
+
+
+ 60%
+ Lines
+ 18/30
+
+
+
+ Press n or j to go to the next uncovered block, b, p or k for the previous block.
+
+
+
+
+1
+2
+3
+4
+5
+6
+7
+8
+9
+10
+11
+12
+13
+14
+15
+16
+17
+18
+19
+20
+21
+22
+23
+24
+25
+26
+27
+28
+29
+30
+31
+32
+33
+34
+35
+36
+37
+38
+39
+40
+41
+42
+43
+44
+45
+46
+47
+48
+49
+50
+51
+52
+53
+54
+55
+56
+57
+58
+59
+60
+61
+62
+63
+64
+65
+
+
+
+1x
+
+
+
+
+
+
+
+
+1x
+1x
+1x
+1x
+
+
+1x
+2x
+2x
+
+
+1x
+1x
+1x
+
+
+1x
+1x
+1x
+1x
+
+
+
+
+
+1x
+
+
+
+
+1x
+
+
+
+
+1x
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+ import plexApi from './plexApi';
+import importData from './importData';
+import auth from './auth';
+
+const getAuthToken = async (req, res) => {
+ const {username} = req.query;
+ const {password} = req.query;
+
+ auth(username, password).then(data => {
+ return res.json(data);
+ });
+};
+
+const getUsers = async (req, res) => {
+ console.log('getUsers-mike-');
+ const users = await plexApi.getUsers();
+ res.json(users);
+};
+
+const getMostWatched = async (req, res) => {
+ const mostWatched = await plexApi.getMostWatched(req);
+ res.json(mostWatched);
+};
+
+const getSections = async (req, res) => {
+ const sections = await plexApi.getSections();
+ res.json(sections);
+};
+
+const getLibraryDataBySection = async (req, res) => {
+ try {
+ const sections = await plexApi.getLibraryDataBySection(req);
+ res.json(sections);
+ } catch (error) {
+ console.log(error);
+ }
+};
+
+const importSections = async (req, res) => {
+ const sections = await importData.importSections();
+ res.json(sections);
+};
+
+const importLibraries = async (req, res) => {
+ const libraries = await importData.importLibraries();
+ res.json(libraries);
+};
+
+const importMostWatched = async (req, res) => {
+ req.query.type = 2;
+ const libraries = await importData.importMostWatched(req);
+ res.json(libraries);
+};
+
+export default {
+ getUsers,
+ getMostWatched,
+ getSections,
+ getLibraryDataBySection,
+ importSections,
+ importLibraries,
+ importMostWatched,
+ getAuthToken,
+};
+
+
+
+
+
+
+
+
+
+
+
+
diff --git a/coverage/recommend/server/services/plex/plexApi.js.html b/coverage/recommend/server/services/plex/plexApi.js.html
new file mode 100644
index 0000000..8ba1b56
--- /dev/null
+++ b/coverage/recommend/server/services/plex/plexApi.js.html
@@ -0,0 +1,468 @@
+
+
+
+ Code coverage report for recommend/server/services/plex/plexApi.js
+
+
+
+
+
+
+
+
+
+
+ All files / recommend/server/services/plex plexApi.js
+
+
+
+ 79.63%
+ Statements
+ 43/54
+
+
+ 68.75%
+ Branches
+ 11/16
+
+
+ 92.31%
+ Functions
+ 12/13
+
+
+ 79.63%
+ Lines
+ 43/54
+
+
+
+ Press n or j to go to the next uncovered block, b, p or k for the previous block.
+
+
+
+
+1
+2
+3
+4
+5
+6
+7
+8
+9
+10
+11
+12
+13
+14
+15
+16
+17
+18
+19
+20
+21
+22
+23
+24
+25
+26
+27
+28
+29
+30
+31
+32
+33
+34
+35
+36
+37
+38
+39
+40
+41
+42
+43
+44
+45
+46
+47
+48
+49
+50
+51
+52
+53
+54
+55
+56
+57
+58
+59
+60
+61
+62
+63
+64
+65
+66
+67
+68
+69
+70
+71
+72
+73
+74
+75
+76
+77
+78
+79
+80
+81
+82
+83
+84
+85
+86
+87
+88
+89
+90
+91
+92
+93
+94
+95
+96
+97
+98
+99
+100
+101
+102
+103
+104
+105
+106
+107
+108
+109
+110
+111
+112
+113
+114
+115
+116
+117
+118
+119
+120
+121
+122
+123
+124
+125
+126
+127
+128
+129
+130
+131
+132
+133
+134
+
+
+
+
+1x
+5x
+
+
+
+
+
+
+
+
+1x
+1x
+
+
+
+
+
+
+
+
+1x
+2x
+
+
+
+
+
+
+
+
+
+
+
+
+
+1x
+1x
+1x
+
+
+
+
+
+
+
+
+1x
+8x
+8x
+8x
+8x
+8x
+
+8x
+
+
+
+
+
+
+1x
+7x
+7x
+7x
+7x
+
+
+7x
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+1x
+2x
+2x
+2x
+2x
+
+
+1x
+2x
+2x
+2x
+2x
+
+
+1x
+1x
+1x
+1x
+1x
+
+
+1x
+1x
+1x
+1x
+1x
+1x
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+ import axios from 'axios';
+import buildUrlPack from 'build-url';
+import config from '../../../config';
+import formatResponse from './helpers';
+
+const getUsersUrlParams = function() {
+ return {
+ host: config.plex.plexApiUrl,
+ path: '/users',
+ queryParams: {
+ 'X-Plex-Token': config.plex.token,
+ },
+ };
+};
+
+const getSectionsUrlParams = function() {
+ return {
+ host: config.plex.plexServerUrl,
+ path: '/library/sections',
+ queryParams: {
+ 'X-Plex-Token': config.plex.token,
+ },
+ };
+};
+
+const mostWatchedUrlParams = function(req) {
+ return {
+ host: config.plex.plexServerUrl,
+ path: '/library/all/top',
+ queryParams: {
+ ...(req && req.query.accountID && {accountID: req.query.accountID}),
+ ...(req && req.query.type && {type: req.query.type}),
+ ...((req && (req.query.limit && {limit: req.query.limit})) || {
+ limit: 10,
+ }),
+ 'X-Plex-Token': config.plex.token,
+ },
+ };
+};
+
+const getLibraryDataBySectionUrlParams = function(req) {
+ const sectionId = req.sectionId || req.params.id;
+ return {
+ host: config.plex.plexServerUrl,
+ path: `/library/sections/${sectionId}/all`,
+ queryParams: {
+ 'X-Plex-Token': config.plex.token,
+ },
+ };
+};
+
+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);
+ return response.MediaContainer.User;
+};
+
+const getMostWatched = async function(req) {
+ const urlParams = mostWatchedUrlParams(req);
+ const mostWatchedUrl = buildUrl(urlParams);
+ const response = await request(mostWatchedUrl);
+ return response.MediaContainer.Metadata;
+};
+
+const getSections = async function() {
+ const urlParams = getSectionsUrlParams();
+ const getSectionsUrl = buildUrl(urlParams);
+ const response = await 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);
+ return response.MediaContainer.Metadata;
+ } catch (error) {
+ return error;
+ }
+};
+
+export default {
+ getUsers,
+ getMostWatched,
+ getSections,
+ getLibraryDataBySection,
+ getUsersUrlParams,
+ getLibraryDataBySectionUrlParams,
+ getSectionsUrlParams,
+ buildUrl,
+ request,
+};
+
+
+
+
+
+
+
+
+
+
+
+
diff --git a/coverage/server/services/plex/helpers.js.html b/coverage/server/services/plex/helpers.js.html
new file mode 100644
index 0000000..b6d7082
--- /dev/null
+++ b/coverage/server/services/plex/helpers.js.html
@@ -0,0 +1,102 @@
+
+
+
+ Code coverage report for server/services/plex/helpers.js
+
+
+
+
+
+
+
+
+
+
+ All files / server/services/plex helpers.js
+
+
+
+ 80%
+ Statements
+ 4/5
+
+
+ 50%
+ Branches
+ 1/2
+
+
+ 100%
+ Functions
+ 1/1
+
+
+ 80%
+ Lines
+ 4/5
+
+
+
+ Press n or j to go to the next uncovered block, b, p or k for the previous block.
+
+
+
+
+1
+2
+3
+4
+5
+6
+7
+8
+9
+10
+11
+12
+
+1x
+2x
+2x
+2x
+
+
+
+
+
+ import parser from 'xml2json';
+
+const formatResponse = response => {
+ const xmlResponse = response.headers['content-type'].includes('xml');
+ Eif (xmlResponse) {
+ return JSON.parse(parser.toJson(response.data));
+ }
+ return response.data;
+};
+
+export default formatResponse;
+
+
+
+
+
+
+
+
+
+
+
+
diff --git a/coverage/server/services/plex/index.html b/coverage/server/services/plex/index.html
new file mode 100644
index 0000000..c75bed4
--- /dev/null
+++ b/coverage/server/services/plex/index.html
@@ -0,0 +1,110 @@
+
+
+
+ Code coverage report for server/services/plex
+
+
+
+
+
+
+
+
+
+
+ All files server/services/plex
+
+
+
+ 50.85%
+ Statements
+ 30/59
+
+
+ 5.56%
+ Branches
+ 1/18
+
+
+ 50%
+ Functions
+ 7/14
+
+
+ 50.85%
+ Lines
+ 30/59
+
+
+
+ Press n or j to go to the next uncovered block, b, p or k for the previous block.
+
+
+
+
+
+
+
+ File
+
+ Statements
+
+ Branches
+
+ Functions
+
+ Lines
+
+
+
+
+ helpers.js
+
+ 80%
+ 4/5
+ 50%
+ 1/2
+ 100%
+ 1/1
+ 80%
+ 4/5
+
+
+
+ plexApi.js
+
+ 48.15%
+ 26/54
+ 0%
+ 0/16
+ 46.15%
+ 6/13
+ 48.15%
+ 26/54
+
+
+
+
+
+
+
+
+
+
+
+
+
+
diff --git a/coverage/server/services/plex/plexApi.js.html b/coverage/server/services/plex/plexApi.js.html
new file mode 100644
index 0000000..4dc3751
--- /dev/null
+++ b/coverage/server/services/plex/plexApi.js.html
@@ -0,0 +1,468 @@
+
+
+
+ Code coverage report for server/services/plex/plexApi.js
+
+
+
+
+
+
+
+
+
+
+ All files / server/services/plex plexApi.js
+
+
+
+ 48.15%
+ Statements
+ 26/54
+
+
+ 0%
+ Branches
+ 0/16
+
+
+ 46.15%
+ Functions
+ 6/13
+
+
+ 48.15%
+ Lines
+ 26/54
+
+
+
+ Press n or j to go to the next uncovered block, b, p or k for the previous block.
+
+
+
+
+1
+2
+3
+4
+5
+6
+7
+8
+9
+10
+11
+12
+13
+14
+15
+16
+17
+18
+19
+20
+21
+22
+23
+24
+25
+26
+27
+28
+29
+30
+31
+32
+33
+34
+35
+36
+37
+38
+39
+40
+41
+42
+43
+44
+45
+46
+47
+48
+49
+50
+51
+52
+53
+54
+55
+56
+57
+58
+59
+60
+61
+62
+63
+64
+65
+66
+67
+68
+69
+70
+71
+72
+73
+74
+75
+76
+77
+78
+79
+80
+81
+82
+83
+84
+85
+86
+87
+88
+89
+90
+91
+92
+93
+94
+95
+96
+97
+98
+99
+100
+101
+102
+103
+104
+105
+106
+107
+108
+109
+110
+111
+112
+113
+114
+115
+116
+117
+118
+119
+120
+121
+122
+123
+124
+125
+126
+127
+128
+129
+130
+131
+132
+133
+134
+
+
+
+
+1x
+4x
+
+
+
+
+
+
+
+
+1x
+
+
+
+
+
+
+
+
+
+1x
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+1x
+
+
+
+
+
+
+
+
+
+
+1x
+3x
+3x
+3x
+3x
+3x
+
+3x
+
+
+
+
+
+
+1x
+2x
+2x
+2x
+2x
+
+
+2x
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+1x
+1x
+1x
+1x
+1x
+
+
+1x
+
+
+
+
+
+
+1x
+
+
+
+
+
+
+1x
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+ import axios from 'axios';
+import buildUrlPack from 'build-url';
+import config from '../../../config';
+import formatResponse from './helpers';
+
+const getUsersUrlParams = function() {
+ return {
+ host: config.plex.plexApiUrl,
+ path: '/users',
+ queryParams: {
+ 'X-Plex-Token': config.plex.token,
+ },
+ };
+};
+
+const getSectionsUrlParams = function() {
+ return {
+ host: config.plex.plexServerUrl,
+ path: '/library/sections',
+ queryParams: {
+ 'X-Plex-Token': config.plex.token,
+ },
+ };
+};
+
+const mostWatchedUrlParams = function(req) {
+ return {
+ host: config.plex.plexServerUrl,
+ path: '/library/all/top',
+ queryParams: {
+ ...(req && req.query.accountID && {accountID: req.query.accountID}),
+ ...(req && req.query.type && {type: req.query.type}),
+ ...((req && (req.query.limit && {limit: req.query.limit})) || {
+ limit: 10,
+ }),
+ 'X-Plex-Token': config.plex.token,
+ },
+ };
+};
+
+const getLibraryDataBySectionUrlParams = function(req) {
+ const sectionId = req.sectionId || req.params.id;
+ return {
+ host: config.plex.plexServerUrl,
+ path: `/library/sections/${sectionId}/all`,
+ queryParams: {
+ 'X-Plex-Token': config.plex.token,
+ },
+ };
+};
+
+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);
+ return response.MediaContainer.User;
+};
+
+const getMostWatched = async function(req) {
+ const urlParams = mostWatchedUrlParams(req);
+ const mostWatchedUrl = buildUrl(urlParams);
+ const response = await request(mostWatchedUrl);
+ return response.MediaContainer.Metadata;
+};
+
+const getSections = async function() {
+ const urlParams = getSectionsUrlParams();
+ const getSectionsUrl = buildUrl(urlParams);
+ const response = await 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);
+ return response.MediaContainer.Metadata;
+ } catch (error) {
+ return error;
+ }
+};
+
+export default {
+ getUsers,
+ getMostWatched,
+ getSections,
+ getLibraryDataBySection,
+ getUsersUrlParams,
+ getLibraryDataBySectionUrlParams,
+ getSectionsUrlParams,
+ buildUrl,
+ request,
+};
+
+
+
+
+
+
+
+
+
+
+
+
diff --git a/index.js b/index.js
index 41b57a5..67bba92 100644
--- a/index.js
+++ b/index.js
@@ -3,7 +3,6 @@ import config from './config';
const server = require('./server').default();
server.create(config);
-
server.start();
export default server.create(config);
diff --git a/server/controllers/apis/plex/index.js b/server/controllers/plex.controller.js
similarity index 91%
rename from server/controllers/apis/plex/index.js
rename to server/controllers/plex.controller.js
index 734f648..203b029 100644
--- a/server/controllers/apis/plex/index.js
+++ b/server/controllers/plex.controller.js
@@ -1,5 +1,5 @@
import {Router} from 'express';
-import plexService from '../../../services/plexApi';
+import plexService from '../services/plex';
const router = Router();
diff --git a/server/index.js b/server/index.js
index eb4bb19..34e7af5 100644
--- a/server/index.js
+++ b/server/index.js
@@ -1,8 +1,7 @@
import express from 'express';
import {json, urlencoded} from 'body-parser';
import {sequelize} from './models';
-
-const routes = require('./routes').default;
+import plex from './routes/plex.route';
export default () => {
const server = express();
@@ -18,7 +17,8 @@ export default () => {
server.use(urlencoded({extended: true}));
// Set up routes
- routes.init(server);
+ server.use('/plex', plex);
+
return server;
};
@@ -27,14 +27,11 @@ export default () => {
const port = server.get('port');
+ console.log('port', port);
sequelize.sync().then(() => {
- if (!module.parent) {
- server.listen(port, () => {
- console.log(
- `Express server listening on - http://${hostname}:${port}`,
- );
- });
- }
+ server.listen(port, () => {
+ console.log(`Express server listening on - http://${hostname}:${port}`);
+ });
});
};
diff --git a/server/routes/apis/index.js b/server/routes/apis/index.js
deleted file mode 100644
index 45a2910..0000000
--- a/server/routes/apis/index.js
+++ /dev/null
@@ -1,8 +0,0 @@
-import {Router} from 'express';
-import v1ApiController from './v1';
-
-const router = Router();
-
-router.use('/v1', v1ApiController);
-
-export default router;
diff --git a/server/routes/apis/v1/index.js b/server/routes/apis/v1/index.js
deleted file mode 100644
index 2fa72e4..0000000
--- a/server/routes/apis/v1/index.js
+++ /dev/null
@@ -1,8 +0,0 @@
-import {Router} from 'express';
-import plexController from '../../../controllers/apis/plex';
-
-const router = Router();
-
-router.use('/plex', plexController);
-
-export default router;
diff --git a/server/routes/index.js b/server/routes/index.js
deleted file mode 100644
index 1ae36df..0000000
--- a/server/routes/index.js
+++ /dev/null
@@ -1,20 +0,0 @@
-import apiRoute from './apis';
-
-const init = server => {
- server.get('*', (req, res, next) => {
- console.log(`Request was made to: ${req.originalUrl}`);
- return next();
- });
-
- server.use('/api', apiRoute);
-
- server.use((err, req, res, next) => {
- if (err) {
- console.log('Error in routes/index', err);
- }
- });
-};
-
-export default {
- init,
-};
diff --git a/server/routes/plex.route.js b/server/routes/plex.route.js
new file mode 100644
index 0000000..f9c9a36
--- /dev/null
+++ b/server/routes/plex.route.js
@@ -0,0 +1,10 @@
+import plexController from '../controllers/plex.controller';
+
+const express = require('express');
+
+const router = express.Router();
+
+console.log('Made it here');
+router.use(plexController);
+
+export default router;
diff --git a/server/services/plexApi/auth.js b/server/services/plex/auth.js
similarity index 100%
rename from server/services/plexApi/auth.js
rename to server/services/plex/auth.js
diff --git a/server/services/plexApi/helpers.js b/server/services/plex/helpers.js
similarity index 100%
rename from server/services/plexApi/helpers.js
rename to server/services/plex/helpers.js
diff --git a/server/services/plexApi/importData.js b/server/services/plex/importData.js
similarity index 100%
rename from server/services/plexApi/importData.js
rename to server/services/plex/importData.js
diff --git a/server/services/plexApi/index.js b/server/services/plex/index.js
similarity index 97%
rename from server/services/plexApi/index.js
rename to server/services/plex/index.js
index bcc52e3..4f55e62 100644
--- a/server/services/plexApi/index.js
+++ b/server/services/plex/index.js
@@ -1,7 +1,6 @@
import plexApi from './plexApi';
import importData from './importData';
import auth from './auth';
-import helpers from './helpers';
const getAuthToken = async (req, res) => {
const {username} = req.query;
@@ -13,6 +12,7 @@ const getAuthToken = async (req, res) => {
};
const getUsers = async (req, res) => {
+ console.log('getUsers-mike-');
const users = await plexApi.getUsers();
res.json(users);
};
diff --git a/server/services/plexApi/plexApi.js b/server/services/plex/plexApi.js
similarity index 100%
rename from server/services/plexApi/plexApi.js
rename to server/services/plex/plexApi.js
diff --git a/test/server/services/plexApi/.DS_Store b/test/server/services/plex/.DS_Store
similarity index 100%
rename from test/server/services/plexApi/.DS_Store
rename to test/server/services/plex/.DS_Store
diff --git a/test/server/services/plex/auth.test.js b/test/server/services/plex/auth.test.js
new file mode 100644
index 0000000..2d52a0b
--- /dev/null
+++ b/test/server/services/plex/auth.test.js
@@ -0,0 +1,32 @@
+// import chai from 'chai';
+// import chaiHttp from 'chai-http';
+// import nock from 'nock';
+// import sinon from 'sinon';
+// import responses from './mocks/plexResponses';
+// import app from '../../../../index';
+
+// chai.use(chaiHttp);
+// const should = chai.should();
+// describe('Users', () => {
+// describe('GET /api/v1/plex/auth', async () => {
+// it('should get plex auth token', (done) => {
+// const response = `${__dirname}/mocks/authResponse.xml`;
+// nock('https://plex.tv')
+// .post(uri => uri.includes('/users/sign_in.xml'))
+// .replyWithFile(200, response, {
+// 'Content-Type': 'text/xml',
+// });
+
+// chai
+// .request(app)
+// .get('/api/v1/plex/auth')
+// .query({ username: 'username', password: 'password' })
+// .end((err, res) => {
+// console.log('mike', res.body);
+// res.should.have.status(200);
+// res.body.should.equal('testPlexApiToken');
+// done();
+// });
+// });
+// });
+// });
diff --git a/test/server/services/plexApi/index.test.js b/test/server/services/plex/index.test.js
similarity index 85%
rename from test/server/services/plexApi/index.test.js
rename to test/server/services/plex/index.test.js
index 0b047e3..192daa0 100644
--- a/test/server/services/plexApi/index.test.js
+++ b/test/server/services/plex/index.test.js
@@ -8,7 +8,7 @@ chai.use(chaiHttp);
const should = chai.should();
describe('Users', () => {
- describe('GET /api/v1/plex/users', async () => {
+ describe('GET plex/users', async () => {
it('should get all plex users', (done) => {
const usersResponse = `${__dirname}/mocks/getUsersResponse.xml`;
nock('https://plex.tv')
@@ -17,7 +17,7 @@ describe('Users', () => {
chai
.request(app)
- .get('/api/v1/plex/users')
+ .get('/plex/users')
.end((err, res) => {
res.should.have.status(200);
res.body.should.be.a('array');
@@ -29,7 +29,7 @@ describe('Users', () => {
});
describe('Most Watched', () => {
- describe('GET /api/v1/plex/most-watched?:type', async () => {
+ describe('GET plex/most-watched?:type', async () => {
it('should return most watched history', (done) => {
nock('https://plex.mjrflix.com')
.get('/library/all/top?type=2&limit=10&X-Plex-Token=testPlexApiToken')
@@ -39,7 +39,7 @@ describe('Most Watched', () => {
chai
.request(app)
- .get('/api/v1/plex/most-watched?type=2')
+ .get('/plex/most-watched?type=2')
.end((err, res) => {
res.should.have.status(200);
res.body.should.be.a('array');
@@ -48,7 +48,7 @@ describe('Most Watched', () => {
});
});
});
- describe('GET /api/v1/plex/most-watched?:accountID&:type', async () => {
+ describe('GET plex/most-watched?:accountID&:type', async () => {
it('should return most watched history per account', (done) => {
nock('https://plex.mjrflix.com')
.get(
@@ -60,7 +60,7 @@ describe('Most Watched', () => {
chai
.request(app)
- .get('/api/v1/plex/most-watched?accountID=22099864&type=2')
+ .get('/plex/most-watched?accountID=22099864&type=2')
.end((err, res) => {
res.should.have.status(200);
res.body.should.be.a('array');
@@ -72,7 +72,7 @@ describe('Most Watched', () => {
});
describe('Sections', () => {
- describe('GET /api/v1/plex/sections', async () => {
+ describe('GET plex/sections', async () => {
it('should sections', (done) => {
nock('https://plex.mjrflix.com')
.get('/library/sections?X-Plex-Token=testPlexApiToken')
@@ -81,7 +81,7 @@ describe('Sections', () => {
});
chai
.request(app)
- .get('/api/v1/plex/sections')
+ .get('/plex/sections')
.end((err, res) => {
res.should.have.status(200);
res.body.should.be.a('array');
@@ -93,7 +93,7 @@ describe('Sections', () => {
});
describe('Library Data', () => {
- describe('GET /api/v1/plex/library?sectionId=3', async () => {
+ describe('GET plex/library?sectionId=3', async () => {
it('should sections', (done) => {
nock('https://plex.mjrflix.com')
.get('/library/sections/3/all?X-Plex-Token=testPlexApiToken')
@@ -102,7 +102,7 @@ describe('Library Data', () => {
});
chai
.request(app)
- .get('/api/v1/plex/library/3')
+ .get('/plex/library/3')
.end((err, res) => {
res.should.have.status(200);
res.body.should.be.a('array');
diff --git a/test/server/services/plexApi/mocks/.DS_Store b/test/server/services/plex/mocks/.DS_Store
similarity index 100%
rename from test/server/services/plexApi/mocks/.DS_Store
rename to test/server/services/plex/mocks/.DS_Store
diff --git a/test/server/services/plexApi/mocks/authResponse.xml b/test/server/services/plex/mocks/authResponse.xml
similarity index 100%
rename from test/server/services/plexApi/mocks/authResponse.xml
rename to test/server/services/plex/mocks/authResponse.xml
diff --git a/test/server/services/plexApi/mocks/getUsersResponse.xml b/test/server/services/plex/mocks/getUsersResponse.xml
similarity index 100%
rename from test/server/services/plexApi/mocks/getUsersResponse.xml
rename to test/server/services/plex/mocks/getUsersResponse.xml
diff --git a/test/server/services/plexApi/mocks/plexResponses.js b/test/server/services/plex/mocks/plexResponses.js
similarity index 100%
rename from test/server/services/plexApi/mocks/plexResponses.js
rename to test/server/services/plex/mocks/plexResponses.js
diff --git a/test/server/services/plexApi/plexApi.test.js b/test/server/services/plex/plexApi.test.js
similarity index 84%
rename from test/server/services/plexApi/plexApi.test.js
rename to test/server/services/plex/plexApi.test.js
index 34ac409..dc82b08 100644
--- a/test/server/services/plexApi/plexApi.test.js
+++ b/test/server/services/plex/plexApi.test.js
@@ -4,19 +4,11 @@ import chai from 'chai';
import chaiHttp from 'chai-http';
import nock from 'nock';
import plexResponses from './mocks/plexResponses';
-import plexApi from '../../../../server/services/plexApi/plexApi';
+import plexApi from '../../../../server/services/plex/plexApi';
const should = chai.should();
describe('plexApi', () => {
- // it('sets options when passed valid options object', () => {
- // const options = { token: 'testPlexApiToken' };
- // const result = plexApi.options;
- // result.should.deep.equal({
- // token: 'testPlexApiToken',
- // });
- // });
-
it('return url params object', () => {
const result = plexApi.getUsersUrlParams();
result.should.deep.equal({
diff --git a/test/server/services/plexApi/auth.test.js b/test/server/services/plexApi/auth.test.js
deleted file mode 100644
index a8adfd8..0000000
--- a/test/server/services/plexApi/auth.test.js
+++ /dev/null
@@ -1,32 +0,0 @@
-import chai from 'chai';
-import chaiHttp from 'chai-http';
-import nock from 'nock';
-import sinon from 'sinon';
-import responses from './mocks/plexResponses';
-import app from '../../../../index';
-
-chai.use(chaiHttp);
-const should = chai.should();
-describe('Users', () => {
- describe('GET /api/v1/plex/auth', async () => {
- it('should get plex auth token', (done) => {
- const response = `${__dirname}/mocks/authResponse.xml`;
- nock('https://plex.tv')
- .post(uri => uri.includes('/users/sign_in.xml'))
- .replyWithFile(200, response, {
- 'Content-Type': 'text/xml',
- });
-
- chai
- .request(app)
- .get('/api/v1/plex/auth')
- .query({ username: 'username', password: 'password' })
- .end((err, res) => {
- console.log('mike', res.body);
- res.should.have.status(200);
- res.body.should.equal('testPlexApiToken');
- done();
- });
- });
- });
-});