Simplified how we fetch and update requests that have landed in plex (#146)
This commit is contained in:
@@ -1,6 +1,6 @@
|
|||||||
import Plex from "../src/plex/plexRepository";
|
import Plex from "../src/plex/plex.js";
|
||||||
import establishedDatabase from "../src/database/database";
|
import establishedDatabase from "../src/database/database.js";
|
||||||
import Configuration from "../src/config/configuration";
|
import Configuration from "../src/config/configuration.js";
|
||||||
|
|
||||||
const configuration = Configuration.getInstance();
|
const configuration = Configuration.getInstance();
|
||||||
const plex = new Plex(
|
const plex = new Plex(
|
||||||
@@ -13,32 +13,29 @@ const queries = {
|
|||||||
saveNewStatus: `UPDATE requests SET status = ? WHERE id IS ? and type IS ?`
|
saveNewStatus: `UPDATE requests SET status = ? WHERE id IS ? and type IS ?`
|
||||||
};
|
};
|
||||||
|
|
||||||
const getByStatus = () =>
|
const getRequestsNotYetInPlex = () =>
|
||||||
establishedDatabase.all(queries.getRequestsNotYetInPlex);
|
establishedDatabase.all(queries.getRequestsNotYetInPlex);
|
||||||
|
|
||||||
const commitNewStatus = (status, id, type, title) => {
|
async function getNewRequestMatchesInPlex() {
|
||||||
console.log(type, title, "updated to:", status);
|
const requests = await getRequestsNotYetInPlex();
|
||||||
return establishedDatabase.run(queries.saveNewStatus, [status, id, type]);
|
const exists = await Promise.all(
|
||||||
};
|
requests.map(request => plex.existsInPlex(request))
|
||||||
|
|
||||||
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) => {
|
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]);
|
||||||
|
}
|
||||||
|
|
||||||
|
function updateMatchInDb(match, status) {
|
||||||
return commitNewStatus(status, match.id, match.type, match.title);
|
return commitNewStatus(status, match.id, match.type, match.title);
|
||||||
};
|
}
|
||||||
|
|
||||||
getNewRequestMatchesInPlex()
|
getNewRequestMatchesInPlex()
|
||||||
.then(newMatches =>
|
.then(newMatches =>
|
||||||
Promise.all(newMatches.map(match => updateMatchInDb(match, "downloaded")))
|
Promise.all(newMatches.map(match => updateMatchInDb(match, "downloaded")))
|
||||||
)
|
)
|
||||||
.then(() => process.exit(0));
|
.finally(() => process.exit(0));
|
||||||
|
|||||||
Reference in New Issue
Block a user