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,40 +1,38 @@
const PlexRepository = require('src/plex/plexRepository');
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 plexRepository = new PlexRepository();
class UpdateRequestsInPlex {
constructor() {
this.database = establishedDatabase;
this.queries = {
getRequests: `SELECT * FROM requests WHERE status IS 'requested' OR 'downloaded'`,
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 ?`,
}
}
getRequests() {
return this.database.all(this.queries.getRequests);
getByStatus() {
return this.database.all(this.queries.getMovies);
}
scrub() {
return this.getRequests()
.then((requests) => Promise.all(requests.map(async (movie) => {
return plexRepository.inPlex(movie)
})))
.then((requests_checkInPlex) => requests_checkInPlex.filter((movie) => movie.matchedInPlex))
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.scrub().then((newInPlex) =>
newInPlex.map((movie) => {
console.log('updated', movie.title, 'to', status)
// this.database.run(this.queries.saveNewStatus, [status, movie.id, movie.type])
})
)
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')