Run lint fix

This commit is contained in:
HaveAGitGat 2023-09-25 08:25:25 +01:00
parent a0273d3417
commit 73482c5a10

View file

@ -1,102 +1,98 @@
const loadDefaultValues = require('../methods/loadDefaultValues');
const fs = require('fs'); const fs = require('fs');
const path = require('path'); const path = require('path');
const loadDefaultValues = require('../methods/loadDefaultValues');
const details = () => { const details = () => ({
return { id: 'Tdarr_Plugin_rename_based_on_codec_schadi',
id: "Tdarr_Plugin_rename_based_on_codec_schadi", Stage: 'Post-processing',
Stage: "Post-processing", Name: 'Rename based on codec Video and Audio',
Name: "Rename based on codec Video and Audio", Type: 'Video_Audio',
Type: "Video_Audio", Operation: 'Transcode',
Operation: "Transcode", Description: `
Description: `
If the filename contains a codec information like h264, av1 or similar for video and AC3, AAC or trueHD \n\n If the filename contains a codec information like h264, av1 or similar for video and AC3, AAC or trueHD \n\n
the plugin will read the codec info from the file and rename it accordingly. \n\n the plugin will read the codec info from the file and rename it accordingly. \n\n
It also takes care off addiotnal files deffined in the input Option. \n\n`, It also takes care off addiotnal files deffined in the input Option. \n\n`,
Version: "1.00", Version: '1.00',
Tags: "post-processing", Tags: 'post-processing',
Inputs:[ Inputs: [
{ {
name: 'rename_audio', name: 'rename_audio',
type: 'boolean', type: 'boolean',
defaultValue: false, defaultValue: false,
inputUI: { inputUI: {
type: 'dropdown', type: 'dropdown',
options: [ options: [
'false', 'false',
'true', 'true',
], ],
}, },
tooltip: `Will Rename According to Audio Codec after x264 or x265. tooltip: `Will Rename According to Audio Codec after x264 or x265.
\\nExample:\\n \\nExample:\\n
true true
\\nExample:\\n \\nExample:\\n
false`, false`,
},
{
name: 'rename_video',
type: 'boolean',
defaultValue: false,
inputUI: {
type: 'dropdown',
options: [
'false',
'true',
],
}, },
{ tooltip: `Will Rename According to Video Codec after x264 or x265.
name: 'rename_video',
type: 'boolean',
defaultValue: false,
inputUI: {
type: 'dropdown',
options: [
'false',
'true',
],
},
tooltip: `Will Rename According to Video Codec after x264 or x265.
\\nExample:\\n \\nExample:\\n
true true
\\nExample:\\n \\nExample:\\n
false`, false`,
}, },
{ {
name: 'additional_extensions', name: 'additional_extensions',
type: 'text', type: 'text',
defaultValue: '.nfo,.srt', defaultValue: '.nfo,.srt',
inputUI: { inputUI: {
type: 'text', type: 'text',
}, },
tooltip: `Additional file extensions to rename (comma-separated). tooltip: `Additional file extensions to rename (comma-separated).
\\nExample:\\n \\nExample:\\n
.nfo,.srt`, .nfo,.srt`,
}, },
], ],
}; });
};
const plugin = (file, librarySettings, inputs, otherArguments) => { const plugin = (file, librarySettings, inputs, otherArguments) => {
inputs = loadDefaultValues(inputs, details); inputs = loadDefaultValues(inputs, details);
const fileNameOld = file._id; const fileNameOld = file._id;
const response = {
var response = { file,
file, removeFromDB: false,
removeFromDB: false, updateDB: true,
updateDB: true, infoLog: '',
infoLog: "",
processFile: false, processFile: false,
}; };
const codecMap = { const codecMap = {
'aac': 'AAC', aac: 'AAC',
'ac3': 'AC3', ac3: 'AC3',
'av1': 'AV1', av1: 'AV1',
'avc': 'h264', avc: 'h264',
'dts': 'DTS', dts: 'DTS',
'eac3': 'EAC3', eac3: 'EAC3',
'flac': 'FLAC', flac: 'FLAC',
'hevc': 'h265', hevc: 'h265',
'mp2': 'MP2', mp2: 'MP2',
'mp3': 'MP3', mp3: 'MP3',
'mpeg2': 'MPEG2', mpeg2: 'MPEG2',
'truehd': 'TrueHD', truehd: 'TrueHD',
'x264': 'h264', x264: 'h264',
'x265': 'h265', x265: 'h265',
'h264': 'h264', h264: 'h264',
'h265': 'h265', h265: 'h265',
'dts': 'DTS-X', dts: 'DTS-X',
'dts-hd ma': 'DTS-HD MA', 'dts-hd ma': 'DTS-HD MA',
'dts-es': 'DTS-HD ES', 'dts-es': 'DTS-HD ES',
'dts-hd hra': 'DTS-HD HRA', 'dts-hd hra': 'DTS-HD HRA',
@ -104,8 +100,8 @@ const plugin = (file, librarySettings, inputs, otherArguments) => {
'dts 96/24': 'DTS', 'dts 96/24': 'DTS',
}; };
var firstVideoStreamCodec; let firstVideoStreamCodec;
var firstAudioStreamCodec; let firstAudioStreamCodec;
const videoCodecRegex = /(h264|h265|x264|x265|avc|hevc|mpeg2|av1)/gi; const videoCodecRegex = /(h264|h265|x264|x265|avc|hevc|mpeg2|av1)/gi;
const audioCodecRegex = /(aac|ac3|eac3|flac|mp2|mp3|truehd|dts[-. ]hd[-. ]ma|dts[-. ]hd[-. ]es|dts[-. ]hd[-. ]hra|dts[-. ]express|dts)/gi; const audioCodecRegex = /(aac|ac3|eac3|flac|mp2|mp3|truehd|dts[-. ]hd[-. ]ma|dts[-. ]hd[-. ]es|dts[-. ]hd[-. ]hra|dts[-. ]express|dts)/gi;
@ -114,7 +110,7 @@ const plugin = (file, librarySettings, inputs, otherArguments) => {
if (videoStream && inputs.rename_video) { if (videoStream && inputs.rename_video) {
const videoCodec = videoStream.codec_name.toLowerCase(); const videoCodec = videoStream.codec_name.toLowerCase();
firstVideoStreamCodec = videoCodec; firstVideoStreamCodec = videoCodec;
if (videoCodec in codecMap) { if (videoCodec in codecMap) {
const renamedCodec = codecMap[videoCodec]; const renamedCodec = codecMap[videoCodec];
@ -127,7 +123,7 @@ const plugin = (file, librarySettings, inputs, otherArguments) => {
if (audioStream && inputs.rename_audio) { if (audioStream && inputs.rename_audio) {
const audioCodec = audioStream.codec_name.toLowerCase(); const audioCodec = audioStream.codec_name.toLowerCase();
firstAudioStreamCodec = audioCodec; firstAudioStreamCodec = audioCodec;
if (audioCodec in codecMap) { if (audioCodec in codecMap) {
const renamedCodec = codecMap[audioCodec]; const renamedCodec = codecMap[audioCodec];
@ -136,68 +132,64 @@ const plugin = (file, librarySettings, inputs, otherArguments) => {
} }
} }
let additionalFilesCount = 0; // Counter for additional files found let additionalFilesCount = 0; // Counter for additional files found
if ((audioStream && inputs.rename_audio) || (videoStream && inputs.rename_video)) { if ((audioStream && inputs.rename_audio) || (videoStream && inputs.rename_video)) {
const filename = path.basename(fileNameOld); const filename = path.basename(fileNameOld);
const JustName = path.parse(filename).name; const JustName = path.parse(filename).name;
const popJustnamen = JustName.split('.'); const popJustnamen = JustName.split('.');
popJustnamen.splice(popJustnamen.length - 5); popJustnamen.splice(popJustnamen.length - 5);
const modJustname = popJustnamen.join('.'); const modJustname = popJustnamen.join('.');
const fileDir = path.dirname(fileNameOld); const fileDir = path.dirname(fileNameOld);
const directoryPath = fileDir; const directoryPath = fileDir;
const additionalExtensions = inputs.additional_extensions.split(','); const additionalExtensions = inputs.additional_extensions.split(',');
let fileList = []; // Array to store the file names const fileList = []; // Array to store the file names
const files = fs.readdirSync(directoryPath); const files = fs.readdirSync(directoryPath);
files.forEach((supportFile) => { files.forEach((supportFile) => {
fileList.push(supportFile); // Add all files to the fileList array fileList.push(supportFile); // Add all files to the fileList array
}); });
const extensionList = additionalExtensions.map(extension => extension.trim()); // Remove leading/trailing spaces from extensions const extensionList = additionalExtensions.map((extension) => extension.trim()); // Remove leading/trailing spaces from extensions
const regex = new RegExp(`(${extensionList.join('|')})$`, 'i'); const regex = new RegExp(`(${extensionList.join('|')})$`, 'i');
files.forEach((supportFile) => { files.forEach((supportFile) => {
if (supportFile.startsWith(modJustname) && regex.test(supportFile)) { if (supportFile.startsWith(modJustname) && regex.test(supportFile)) {
const renamedFileWithVideoCodec = supportFile.replace(videoCodecRegex, codecMap[firstVideoStreamCodec]); const renamedFileWithVideoCodec = supportFile.replace(videoCodecRegex, codecMap[firstVideoStreamCodec]);
const renamedFileWithBothCodecs = renamedFileWithVideoCodec.replace(audioCodecRegex, codecMap[firstAudioStreamCodec]); const renamedFileWithBothCodecs = renamedFileWithVideoCodec.replace(audioCodecRegex, codecMap[firstAudioStreamCodec]);
fs.renameSync(`${directoryPath}/${supportFile}`, `${directoryPath}/${renamedFileWithBothCodecs}`, { fs.renameSync(`${directoryPath}/${supportFile}`, `${directoryPath}/${renamedFileWithBothCodecs}`, {
overwrite: true, overwrite: true,
}); });
response.infoLog += `${directoryPath}/${supportFile} renamed to ${directoryPath}/${renamedFileWithBothCodecs}\n`;
additionalFilesCount++; // Increment the count for each additional file found
}
});
//const textFilePath = path.join(directoryPath, `${modJustname}.txt`);
//fs.writeFileSync(textFilePath, fileList.filter(file => file.startsWith(modJustname) && regex.test(file)).join('\n'), 'utf-8');
}
response.infoLog += `${directoryPath}/${supportFile} renamed to ${directoryPath}/${renamedFileWithBothCodecs}\n`;
additionalFilesCount++; // Increment the count for each additional file found
}
});
// const textFilePath = path.join(directoryPath, `${modJustname}.txt`);
// fs.writeFileSync(textFilePath, fileList.filter(file => file.startsWith(modJustname) && regex.test(file)).join('\n'), 'utf-8');
}
if (fileNameOld !== file._id) { if (fileNameOld !== file._id) {
fs.renameSync(fileNameOld, file._id, { fs.renameSync(fileNameOld, file._id, {
overwrite: true, overwrite: true,
}); });
response.infoLog += `Renamed file to: ${file._id}\n` response.infoLog += `Renamed file to: ${file._id}\n`;
if (additionalFilesCount > 0) { if (additionalFilesCount > 0) {
response.infoLog += `and: ${additionalFilesCount} additional Files!\n`; response.infoLog += `and: ${additionalFilesCount} additional Files!\n`;
} }
return response; return response;
} }
else {
response.infoLog += `Video File not renamed!\n`
if (additionalFilesCount > 0) {
response.infoLog += `But: ${additionalFilesCount} additional Files!\n`;
}
return response;
}
response.infoLog += 'Video File not renamed!\n';
if (additionalFilesCount > 0) {
response.infoLog += `But: ${additionalFilesCount} additional Files!\n`;
}
return response;
}; };
module.exports.details = details; module.exports.details = details;