diff --git a/server/controllers/apis/plex/index.js b/server/controllers/apis/plex/index.js index c948cf4..3dc12fb 100644 --- a/server/controllers/apis/plex/index.js +++ b/server/controllers/apis/plex/index.js @@ -5,9 +5,13 @@ const router = Router(); // 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); diff --git a/server/services/plexApi/importData.js b/server/services/plexApi/importData.js index d26ad25..fd33f67 100644 --- a/server/services/plexApi/importData.js +++ b/server/services/plexApi/importData.js @@ -27,6 +27,15 @@ const importLibraries = async () => { }); }; +const importMostWatched = async req => { + const plexApi = plexApiClient(); + const mostWatched = await plexApi.getMostWatched(req); + console.log('6========', mostWatched); + mostWatched.forEach(async libraryData => { + await updateLibrary([libraryData]); + }); +}; + const importLibrary = async sectionId => { const plexApi = plexApiClient(); const libraryData = await plexApi.getLibraryDataBySection({ @@ -36,6 +45,30 @@ const importLibrary = async sectionId => { 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 => { console.log(libraryData[1]); libraryData.forEach(async data => { @@ -61,4 +94,4 @@ const createLibrary = libraryData => { }); }; -export default {importSections, importLibraries}; +export default {importSections, importLibraries, importMostWatched}; diff --git a/server/services/plexApi/index.js b/server/services/plexApi/index.js index 0fd38d6..5c83f4d 100644 --- a/server/services/plexApi/index.js +++ b/server/services/plexApi/index.js @@ -39,6 +39,12 @@ const importLibraries = async (req, res) => { 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, @@ -46,4 +52,5 @@ export default { getLibraryDataBySection, importSections, importLibraries, + importMostWatched, }; diff --git a/server/services/plexApi/plexApi.js b/server/services/plexApi/plexApi.js index 28112cf..14ab725 100644 --- a/server/services/plexApi/plexApi.js +++ b/server/services/plexApi/plexApi.js @@ -36,9 +36,11 @@ PlexApiClient.prototype.mostWatchedUrlParams = function(req) { host: config.plex.plexServerUrl, path: '/library/all/top', queryParams: { - ...(req.query.accountID && {accountID: req.query.accountID}), - ...(req.query.type && {type: req.query.type}), - ...((req.query.limit && {limit: req.query.limit}) || {limit: 10}), + ...(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': this.options.token || config.plex.token, }, }; @@ -106,6 +108,7 @@ PlexApiClient.prototype.getMostWatched = async function(req) { const urlParams = this.mostWatchedUrlParams(req); const mostWatchedUrl = this.buildUrl(urlParams); const response = await this.request(mostWatchedUrl); + console.log(response.MediaContainer.Metadata); return response.MediaContainer.Metadata; };