Script for updating all requested and downloading request status to downloaded if exist in plex

This commit is contained in:
2019-07-25 00:48:16 +02:00
parent 6aba9774c6
commit af7b1f2424

View File

@@ -1,41 +1,39 @@
const PlexRepository = require('src/plex/plexRepository'); const Plex = require('src/plex/plex')
const configuration = require('src/config/configuration').getInstance(); const configuration = require('src/config/configuration').getInstance();
const establishedDatabase = require('src/database/database'); const plex = new Plex(configuration.get('plex', 'ip'))
const establishedDatabase = require('src/database/database');
const plexRepository = new PlexRepository();
class UpdateRequestsInPlex { class UpdateRequestsInPlex {
constructor() { constructor() {
this.database = establishedDatabase; this.database = establishedDatabase;
this.queries = { this.queries = {
getRequests: `SELECT * FROM requests WHERE status IS 'requested' OR 'downloaded'`, getMovies: `SELECT * FROM requests WHERE status = 'requested' OR status = 'downloading'`,
saveNewStatus: `UPDATE requests SET status = ? WHERE id IS ? and type IS ?`, // 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))))
}
getRequests() { commitNewStatus(status, id, type, title) {
return this.database.all(this.queries.getRequests); console.log(type, title, 'updated to:', status)
} this.database.run(this.queries.saveNewStatus, [status, id, type])
}
scrub() {
return this.getRequests() updateStatus(status) {
.then((requests) => Promise.all(requests.map(async (movie) => { this.getByStatus()
return plexRepository.inPlex(movie) .then(requests => Promise.all(requests.map(request => plex.existsInPlex(request))))
}))) .then(matchedRequests => matchedRequests.filter(request => request.existsInPlex))
.then((requests_checkInPlex) => requests_checkInPlex.filter((movie) => movie.matchedInPlex)) .then(newMatches => newMatches.map(match => this.commitNewStatus(status, match.id, match.type, match.title)))
} }
updateStatus(status) {
this.scrub().then((newInPlex) =>
newInPlex.map((movie) => {
console.log('updated', movie.title, 'to', status)
// this.database.run(this.queries.saveNewStatus, [status, movie.id, movie.type])
})
)
}
} }
var requestsUpdater = new UpdateRequestsInPlex(); var requestsUpdater = new UpdateRequestsInPlex();
requestsUpdater.updateStatus('downloaded') requestsUpdater.updateStatus('downloaded')
module.exports = UpdateRequestsInPlex module.exports = UpdateRequestsInPlex