Script for updating all requested and downloading request status to downloaded if exist in plex
This commit is contained in:
		| @@ -1,40 +1,38 @@ | |||||||
| 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 plex = new Plex(configuration.get('plex', 'ip')) | ||||||
| const establishedDatabase = require('src/database/database');  | 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'`, | ||||||
|  | //         getMovies: "select * from requests where status is 'reset'", | ||||||
|         saveNewStatus: `UPDATE requests SET status = ? WHERE id IS ? and type IS ?`, |         saveNewStatus: `UPDATE requests SET status = ? WHERE id IS ? and type IS ?`, | ||||||
|      } |      } | ||||||
|   } |   } | ||||||
|  |   getByStatus() { | ||||||
|    getRequests() { |      return this.database.all(this.queries.getMovies); | ||||||
|       return this.database.all(this.queries.getRequests); |  | ||||||
|   } |   } | ||||||
|  |  | ||||||
|   scrub() { |   scrub() { | ||||||
|       return this.getRequests() |      return this.getByStatus() | ||||||
|          .then((requests) => Promise.all(requests.map(async (movie) => { |         .then((requests) => Promise.all(requests.map(movie => plex.existsInPlex(movie)))) | ||||||
|             return plexRepository.inPlex(movie) |  | ||||||
|          }))) |  | ||||||
|          .then((requests_checkInPlex) => requests_checkInPlex.filter((movie) => movie.matchedInPlex)) |  | ||||||
|   } |   } | ||||||
|  |  | ||||||
|  |   commitNewStatus(status, id, type, title) { | ||||||
|  |     console.log(type, title, 'updated to:', status) | ||||||
|  |     this.database.run(this.queries.saveNewStatus, [status, id, type]) | ||||||
|  |   } | ||||||
|  |  | ||||||
|  |     | ||||||
|   updateStatus(status) { |   updateStatus(status) { | ||||||
|      this.scrub().then((newInPlex) =>  |     this.getByStatus() | ||||||
|          newInPlex.map((movie) => { |       .then(requests => Promise.all(requests.map(request => plex.existsInPlex(request)))) | ||||||
|             console.log('updated', movie.title, 'to', status) |       .then(matchedRequests => matchedRequests.filter(request => request.existsInPlex)) | ||||||
|             // this.database.run(this.queries.saveNewStatus, [status, movie.id, movie.type]) |       .then(newMatches => newMatches.map(match => this.commitNewStatus(status, match.id, match.type, match.title))) | ||||||
|          }) |  | ||||||
|       ) |  | ||||||
|   } |   } | ||||||
| } | } | ||||||
|  |  | ||||||
| var requestsUpdater = new UpdateRequestsInPlex(); | var requestsUpdater = new UpdateRequestsInPlex(); | ||||||
| requestsUpdater.updateStatus('downloaded') | requestsUpdater.updateStatus('downloaded') | ||||||
|  |  | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user