Updated and moved update requests to /scripts
This commit is contained in:
44
seasoned_api/scripts/updateRequestsInPlex.js
Normal file
44
seasoned_api/scripts/updateRequestsInPlex.js
Normal file
@@ -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));
|
||||
Reference in New Issue
Block a user