Add endpoint and tests for Plex sections.

This commit is contained in:
mike.rode
2019-02-13 01:32:21 -06:00
parent ca92855171
commit 652973f228
5 changed files with 172 additions and 5 deletions

View File

@@ -5,5 +5,6 @@ const router = Router();
router.get('/users', plexService.getUsers);
router.get('/most-watched', plexService.getMostWatched);
router.get('/sections', plexService.getSections);
export default router;

View File

@@ -8,11 +8,19 @@ const getUsers = async (req, res) => {
const getMostWatched = async (req, res) => {
const plexApi = plexApiClient();
const mostWatched = await plexApi.getMostWatched(2);
console.log('url---', req.query);
const mostWatched = await plexApi.getMostWatched(req.query.type);
res.json(mostWatched);
};
const getSections = async (req, res) => {
const plexApi = plexApiClient();
const sections = await plexApi.getSections();
res.json(sections);
};
export default {
getUsers,
getMostWatched,
getSections,
};

View File

@@ -21,6 +21,16 @@ PlexApiClient.prototype.getUsersUrlParams = function() {
};
};
PlexApiClient.prototype.getSectionsUrlParams = function() {
return {
host: config.plex.plexServerUrl,
path: '/library/sections',
queryParams: {
'X-Plex-Token': this.options.token || config.plex.token,
},
};
};
PlexApiClient.prototype.mostWatchedUrlParams = function(type, limit = 10) {
return {
host: config.plex.plexServerUrl,
@@ -94,6 +104,13 @@ PlexApiClient.prototype.getMostWatched = async function(type, limit = 10) {
return response.MediaContainer.Metadata;
};
PlexApiClient.prototype.getSections = async function() {
const urlParams = this.getSectionsUrlParams();
const getUsersUrl = this.buildUrl(urlParams);
const response = await this.request(getUsersUrl);
return response.MediaContainer.Directory;
};
const plexApiClient = (options = []) => {
return new PlexApiClient(options);
};