From c3f041d27f8eb16c45a7560455b03371f7843618 Mon Sep 17 00:00:00 2001 From: Kevin Date: Sun, 25 Sep 2022 13:39:33 +0200 Subject: [PATCH] Simplified how we fetch and update requests that have landed in plex (#146) --- scripts/updateRequestsInPlex.js | 41 +++++++++++++++------------------ 1 file changed, 19 insertions(+), 22 deletions(-) diff --git a/scripts/updateRequestsInPlex.js b/scripts/updateRequestsInPlex.js index c2c818c..4d03b63 100644 --- a/scripts/updateRequestsInPlex.js +++ b/scripts/updateRequestsInPlex.js @@ -1,6 +1,6 @@ -import Plex from "../src/plex/plexRepository"; -import establishedDatabase from "../src/database/database"; -import Configuration from "../src/config/configuration"; +import Plex from "../src/plex/plex.js"; +import establishedDatabase from "../src/database/database.js"; +import Configuration from "../src/config/configuration.js"; const configuration = Configuration.getInstance(); const plex = new Plex( @@ -13,32 +13,29 @@ const queries = { saveNewStatus: `UPDATE requests SET status = ? WHERE id IS ? and type IS ?` }; -const getByStatus = () => +const getRequestsNotYetInPlex = () => establishedDatabase.all(queries.getRequestsNotYetInPlex); -const commitNewStatus = (status, id, type, title) => { - console.log(type, title, "updated to:", status); +async function getNewRequestMatchesInPlex() { + const requests = await getRequestsNotYetInPlex(); + const exists = await Promise.all( + requests.map(request => plex.existsInPlex(request)) + ); + + return requests.filter(() => exists.shift()); +} + +function commitNewStatus(status, id, type, title) { + console.log(`${type} ${title} updated to: ${status}`); return establishedDatabase.run(queries.saveNewStatus, [status, id, type]); -}; +} -const getNewRequestMatchesInPlex = async () => { - const requests = await getByStatus(); - - return Promise.all(requests.map(plex.inPlex)) - .catch(error => - console.log("error from checking plex for existance:", error) - ) - .then(matchedRequests => - matchedRequests.filter(request => request.matchedInPlex) - ); -}; - -const updateMatchInDb = (match, status) => { +function updateMatchInDb(match, status) { return commitNewStatus(status, match.id, match.type, match.title); -}; +} getNewRequestMatchesInPlex() .then(newMatches => Promise.all(newMatches.map(match => updateMatchInDb(match, "downloaded"))) ) - .then(() => process.exit(0)); + .finally(() => process.exit(0));