From b384e748af57230a2f988dcb1f8f538e27158ac8 Mon Sep 17 00:00:00 2001 From: Kevin Midboe Date: Mon, 3 Jan 2022 18:32:22 +0100 Subject: [PATCH] Updated and moved update requests to /scripts --- seasoned_api/package.json | 2 +- seasoned_api/scripts/updateRequestsInPlex.js | 44 +++++++++++++++++ seasoned_api/src/plex/updateRequestsInPlex.js | 47 ------------------- 3 files changed, 45 insertions(+), 48 deletions(-) create mode 100644 seasoned_api/scripts/updateRequestsInPlex.js delete mode 100644 seasoned_api/src/plex/updateRequestsInPlex.js diff --git a/seasoned_api/package.json b/seasoned_api/package.json index 8f73d4b..d2301b2 100644 --- a/seasoned_api/package.json +++ b/seasoned_api/package.json @@ -11,7 +11,7 @@ "test": "cross-env SEASONED_CONFIG=conf/test.json NODE_PATH=. mocha --require @babel/register --recursive test/unit test/system", "coverage": "cross-env SEASONED_CONFIG=conf/test.json NODE_PATH=. nyc mocha --require @babel/register --recursive test && nyc report --reporter=text-lcov | coveralls", "lint": "./node_modules/.bin/eslint src/", - "update": "cross-env SEASONED_CONFIG=conf/development.json NODE_PATH=. node src/plex/updateRequestsInPlex.js", + "update": "cross-env SEASONED_CONFIG=conf/development.json NODE_PATH=. node scripts/updateRequestsInPlex.js", "docs": "yarn apiDocs; yarn classDocs", "apiDocs": "", "classDocs": "./script/generate-class-docs.sh" diff --git a/seasoned_api/scripts/updateRequestsInPlex.js b/seasoned_api/scripts/updateRequestsInPlex.js new file mode 100644 index 0000000..7f186e6 --- /dev/null +++ b/seasoned_api/scripts/updateRequestsInPlex.js @@ -0,0 +1,44 @@ +const Plex = require("src/plex/plex"); +const configuration = require("src/config/configuration").getInstance(); +const plex = new Plex(configuration.get("plex", "ip")); +const establishedDatabase = require("src/database/database"); + +const queries = { + getRequestsNotYetInPlex: `SELECT * FROM requests WHERE status = 'requested' OR status = 'downloading'`, + saveNewStatus: `UPDATE requests SET status = ? WHERE id IS ? and type IS ?` +}; + +const getByStatus = () => + establishedDatabase.all(queries.getRequestsNotYetInPlex); + +const checkIfRequestExistInPlex = async request => { + request.existsInPlex = await plex.existsInPlex(request); + return request; +}; + +const 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(checkIfRequestExistInPlex)) + .catch(error => + console.log("error from checking plex for existance:", error) + ) + .then(matchedRequests => + matchedRequests.filter(request => request.existsInPlex) + ); +}; + +const 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)); diff --git a/seasoned_api/src/plex/updateRequestsInPlex.js b/seasoned_api/src/plex/updateRequestsInPlex.js deleted file mode 100644 index 4242bdc..0000000 --- a/seasoned_api/src/plex/updateRequestsInPlex.js +++ /dev/null @@ -1,47 +0,0 @@ -const Plex = require("src/plex/plex"); -const configuration = require("src/config/configuration").getInstance(); -const plex = new Plex(configuration.get("plex", "ip")); -const establishedDatabase = require("src/database/database"); - -class UpdateRequestsInPlex { - constructor() { - this.database = establishedDatabase; - this.queries = { - getMovies: `SELECT * FROM requests WHERE status = 'requested' OR status = 'downloading'`, - // getMovies: "select * from requests where status is 'reset'", - saveNewStatus: `UPDATE requests SET status = ? WHERE id IS ? and type IS ?` - }; - } - getByStatus() { - return this.database.all(this.queries.getMovies); - } - scrub() { - return this.getByStatus().then(requests => - Promise.all(requests.map(movie => plex.existsInPlex(movie))) - ); - } - - commitNewStatus(status, id, type, title) { - console.log(type, title, "updated to:", status); - this.database.run(this.queries.saveNewStatus, [status, id, type]); - } - - updateStatus(status) { - this.getByStatus() - .then(requests => - Promise.all(requests.map(request => plex.existsInPlex(request))) - ) - .then(matchedRequests => - matchedRequests.filter(request => request.existsInPlex) - ) - .then(newMatches => - newMatches.map(match => - this.commitNewStatus(status, match.id, match.type, match.title) - ) - ); - } -} -var requestsUpdater = new UpdateRequestsInPlex(); -requestsUpdater.updateStatus("downloaded"); - -module.exports = UpdateRequestsInPlex;