diff --git a/server/services/plex/importData.js b/server/services/plex/importData.js index 67ec132..8c8a861 100644 --- a/server/services/plex/importData.js +++ b/server/services/plex/importData.js @@ -3,42 +3,42 @@ import plexApi from './plexApi'; import models from '../../db/models'; import config from '../../../config'; import MovieDb from 'moviedb-promise'; -import { Op } from 'sequelize'; +import {Op} from 'sequelize'; const mdb = new MovieDb(config.server.movieApiKey); const updateOrCreate = async (model, where, newItem, beforeCreate) => { - const item = await model.findOne({ where }); + const item = await model.findOne({where}); if (!item) { const createItem = await model.create(newItem, { returning: true, plain: true, raw: true, }); - return { createItem, created: true }; + return {createItem, created: true}; } else { const updatedItem = await model.update( newItem, - { where: where }, - { returning: true, plain: true, raw: true }, + {where: where}, + {returning: true, plain: true, raw: true}, ); - return { item, created: false }; + return {item, created: false}; } }; const importTvPosters = async user => { try { const mostWatched = await models.PlexLibrary.findAll({ - where: { UserId: user.id, type: 'show', views: { [Op.gt]: 0 } }, + where: {UserId: user.id, type: 'show', views: {[Op.gt]: 0}}, }); const imageUrls = await mostWatched.map(async show => { - const res = await mdb.searchTv({ query: show.title }); + const res = await mdb.searchTv({query: show.title}); return models.PlexLibrary.update( { poster_path: res.results[0].poster_path, }, { - where: { UserId: user.id, title: show.title }, + where: {UserId: user.id, title: show.title}, }, ); }); @@ -49,6 +49,7 @@ const importTvPosters = async user => { const importSections = async user => { const sections = await plexApi.getSections(user); + console.log('getSections response', sections); const dbSections = await createSections(sections, user); return dbSections; }; @@ -70,7 +71,7 @@ const createSections = async (sections, user) => { newSection, ); }).catch(err => { - console.log(err); + console.log('create section error', err); }); return updatedSections; }; @@ -132,7 +133,9 @@ const importMostWatched = async user => { }; const importMostWatchedData = async (sectionKey, user) => { - const mostWatchedData = await plexApi.getMostWatched({ sectionKey }, user); + const mostWatchedData = await plexApi.getMostWatched({sectionKey}, user); + console.log('most watched data', mostWatchedData); + const mostWatchedDbData = await updateLibrary(mostWatchedData, user); return mostWatchedDbData; }; @@ -159,7 +162,7 @@ const updateLibrary = async (libraryData, user) => { newData, ); }).catch(err => { - console.log(err); + console.log('Unable to import most watched', err); }); }; diff --git a/server/services/plex/plexApi.js b/server/services/plex/plexApi.js index 794eb97..497b595 100644 --- a/server/services/plex/plexApi.js +++ b/server/services/plex/plexApi.js @@ -1,7 +1,7 @@ import config from '../../../config'; import helpers from '../helpers'; -const getUsersUrlParams = function (user) { +const getUsersUrlParams = function(user) { return { host: user.plexUrl, path: '/users', @@ -11,7 +11,7 @@ const getUsersUrlParams = function (user) { }; }; -const getSectionsUrlParams = function (user) { +const getSectionsUrlParams = function(user) { return { host: user.plexUrl, path: '/library/sections', @@ -21,20 +21,20 @@ const getSectionsUrlParams = function (user) { }; }; -const mostWatchedUrlParams = function (accountId, sectionKey, limit = 10, user) { +const mostWatchedUrlParams = function(accountId, sectionKey, limit = 10, user) { return { host: user.plexUrl, path: '/library/all/top', queryParams: { - ...(accountId && { accountId }), - ...(sectionKey && { type: sectionKey }), - ...(limit && { limit }), + ...(accountId && {accountId}), + ...(sectionKey && {type: sectionKey}), + ...(limit && {limit}), 'X-Plex-Token': user.plexToken, }, }; }; -const getLibraryDataBySectionUrlParams = function (sectionId, user) { +const getLibraryDataBySectionUrlParams = function(sectionId, user) { return { host: user.plexUrl, path: `/library/sections/${sectionId}/all`, @@ -44,7 +44,7 @@ const getLibraryDataBySectionUrlParams = function (sectionId, user) { }; }; -const getUsers = async function (user) { +const getUsers = async function(user) { try { const urlParams = getUsersUrlParams(user); const getUsersUrl = helpers.buildUrl(urlParams); @@ -55,18 +55,22 @@ const getUsers = async function (user) { } }; -const getMostWatched = async function ( - { accountId, sectionKey, limit = 10 }, +const getMostWatched = async function( + {accountId, sectionKey, limit = 10}, user, ) { try { const urlParams = mostWatchedUrlParams(accountId, sectionKey, limit, user); const mostWatchedUrl = helpers.buildUrl(urlParams); const response = await helpers.request(mostWatchedUrl); - console.log('most-watched-raw-response---', response) - return response.MediaContainer.Metadata; + console.log('most-watched-raw-response---', response); + if (response.MediaContainer.Metadata) { + return response.MediaContainer.Metadata; + } else { + return []; + } } catch (error) { - console.log(error); + console.log('getMostWatched plexAPI error', error); return { code: error.status, message: error.statusText, @@ -75,7 +79,7 @@ const getMostWatched = async function ( } }; -const getSections = async function (user) { +const getSections = async function(user) { try { const urlParams = getSectionsUrlParams(user); const getSectionsUrl = helpers.buildUrl(urlParams); @@ -89,7 +93,7 @@ const getSections = async function (user) { } }; -const getLibraryDataBySection = async function ({ sectionKey }, user) { +const getLibraryDataBySection = async function({sectionKey}, user) { try { console.log('sectionId--', sectionKey); const urlParams = getLibraryDataBySectionUrlParams(sectionKey, user);