· 5 years ago · Jun 15, 2020, 01:48 PM
1var request = require("request");
2var util = require("util");
3var fs = require("fs");
4var rename = util.promisify(fs.rename);
5
6function details() {
7 return {
8 id: "Tdarr_Plugin_drdd_update_radarr",
9 Name: "DrDD Send command to Radarr to update movie file",
10 Stage: "Post-processing",
11 Type: "Video",
12 Operation: "",
13 Description: "Updates the movie file in Radarr",
14 Version: "1.0",
15 Tags: "3rd party,post-processing,configurable",
16 Inputs: [
17 { name: "radar_host", tooltip: "" },
18 { name: "radar_port", tooltip: "" },
19 { name: "api_key", tooltip: "" }
20 ],
21 };
22}
23
24/**
25 * Returns the matching movie details in the Radarr response body based on the
26 * current file name.
27 * @param {*} body the Radarr response body
28 * @param {*} fileName the current file name
29 */
30function getMovieMatch(body, fileName) {
31 return JSON.parse(body).find(function(movie) {
32 return "movieFile" in movie && movie.movieFile.relativePath === fileName;
33 });
34}
35
36/**
37 * Trigger a Radarr file scan for the given movie ID.
38 * @param {*} id the movie ID.
39 * @param {*} radarrHost the Radarr host
40 * @param {*} radarrPort the Radarr port
41 * @param {*} radarrApiKey the Radarr API key
42 */
43function triggerRaddarFileScan(id, radarrHost, radarrPort, radarrApiKey) {
44 return new Promise(function(resolve, reject) {
45 request.post(`http://${radarrHost}:${radarrPort}/api/command?apikey=${radarrApiKey}`, {
46 body: JSON.stringify({
47 name: "RescanMovie",
48 movieId: id
49 })}, function(error) {
50 if (error) reject(error);
51 resolve();
52 }
53 );
54 })
55}
56
57async function plugin(file, _librarySettings, inputs) {
58 var radarrHost = inputs.radar_host;
59 var radarrPort = inputs.radar_port;
60 var radarrApiKey = inputs.api_key;
61 var isFileRenamed = false;
62
63 request.get(`http://${radarrHost}:${radarrPort}/api/movie?apikey=${radarrApiKey}`, async function (_err, _response, body) {
64 var movie = getMovieMatch(body, file.meta.FileName);
65 if (!movie) {
66 console.log(`Could not find matching movie for file ${file.meta.FileName} in Radarr.`);
67 }
68
69 try {
70 // Rename the file to force a file rescan.
71 var fileNameWithoutExtension = movie.movieFile.relativePath.replace(/\.[^/.]+$/, "");
72 var extension = movie.movieFile.relativePath.match(/\.[^/.]+$/);
73 await rename(
74 `${file.meta.Directory}/${movie.movieFile.relativePath}`,
75 `${file.meta.Directory}/${fileNameWithoutExtension}_${extension}`
76 );
77 isFileRenamed = true;
78
79 await triggerRaddarFileScan(movie.id, radarrHost, radarrPort, radarrApiKey);
80
81 // Rename the file back
82 await rename(
83 `${file.meta.Directory}/${fileNameWithoutExtension}_${extension}`,
84 `${file.meta.Directory}/${movie.movieFile.relativePath}`
85 );
86 isFileRenamed = false;
87
88 await triggerRaddarFileScan(movie.id, radarrHost, radarrPort, radarrApiKey);
89 } catch(error) {
90 if (isFileRenamed) {
91 // Rename back
92 await rename(
93 `${file.meta.Directory}/${fileNameWithoutExtension}_${extension}`,
94 `${file.meta.Directory}/${movie.movieFile.relativePath}`
95 );
96 }
97 }
98 });
99}
100
101module.exports.details = details;
102module.exports.plugin = plugin;