-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #28 from ant385525/sonarr_renamer
Redo sonarr renamer to refresh sonarr and rename the file
- Loading branch information
Showing
2 changed files
with
160 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,42 +1,117 @@ | ||
import { Sonarr } from 'Shared/Sonarr'; | ||
/** | ||
* This script will send a rename command to Sonarr | ||
* @author Anthony Clerici | ||
* @author Shaun Agius | ||
* @uid 5ac44abd-cfe9-4a84-904b-9424908509de | ||
* @description This script will send a rename command to Sonarr | ||
* @revision 6 | ||
* @version 1.0.0 | ||
* @revision 5 | ||
* @param {string} URI Sonarr root URI and port (e.g. http://sonarr:1234) | ||
* @param {string} ApiKey API Key | ||
* @output Item renamed | ||
* @output Item not found | ||
*/ | ||
|
||
function Script(URI, ApiKey) { | ||
let sonarr = new Sonarr(URI, ApiKey); | ||
let folder = Variables.folder.FullName | ||
var season | ||
if (folder.includes("Season")) | ||
season = folder.indexOf('Season')-1 | ||
else | ||
{ | ||
if (folder.includes("/")) | ||
season = folder.lastIndexOf("/") | ||
else if (folder.includes("\\")) | ||
season = folder.lastIndexOf("\\") | ||
} | ||
let folder2 = folder.substring(0, season) | ||
var slash | ||
if (folder2.includes("/")) | ||
slash = folder2.lastIndexOf("/") +1 | ||
else if (folder2.includes("\\")) | ||
slash = folder2.lastIndexOf("\\") +1 | ||
let final = folder2.substring(slash) | ||
let series = sonarr.getShowByPath(final); | ||
if (!series) | ||
const folderPath = Variables.folder.FullName; | ||
const currentFileName = Variables.file.Name; | ||
let newFileName = null; | ||
let episodeFileId = null; | ||
|
||
// Find series name from sonarr | ||
let series = findSeries(folderPath, sonarr); | ||
|
||
if (!series) { | ||
Logger.WLog('Series not found for path: ' + folderPath); | ||
return 2; | ||
Logger.ILog(`Renaming ${series.title}`); | ||
let endpoint = `rename`; | ||
let queryParmeters = `seriesId=${series.id}` | ||
let response = sonarr.fetchJson(endpoint, queryParmeters); | ||
Logger.ILog(`Response ${response}`); | ||
return 1; | ||
} | ||
|
||
try { | ||
// Ensure series is refreshed before renaming | ||
let refreshBody = { | ||
seriesId: series.id | ||
} | ||
let refreshData = sonarr.sendCommand('RescanSeries', refreshBody) | ||
Logger.ILog(`Series refreshed`); | ||
|
||
// Wait for the completion of the refresh scan | ||
let refreshCompleted = sonarr.waitForCompletion(refreshData.id, sonarr); | ||
if (!refreshCompleted) { | ||
Logger.ILog('Refresh not completed'); | ||
return -1; | ||
} | ||
|
||
let renamedEpisodes = fetchRenamedFiles(series.id, sonarr); | ||
if (!renamedEpisodes) { | ||
Logger.ILog('No episodes need to be renamed'); | ||
return 2; | ||
} | ||
|
||
Logger.ILog(`Searching for an episode previously named ${currentFileName}`); | ||
renamedEpisodes.every((episode) => { | ||
if (episode.existingPath.endsWith(currentFileName)) { | ||
episodeFileId = episode.episodeFileId; | ||
newFileName = System.IO.Path.GetFileName(episode.newPath); | ||
Logger.ILog(`Found it, renaming file ${episodeFileId} to ${newFileName}`); | ||
return false; | ||
} | ||
return true; | ||
}); | ||
|
||
if (newFileName === null) { | ||
Logger.WLog('No matching episode found to rename.'); | ||
return 2; | ||
} | ||
|
||
let renameBody = { | ||
seriesId: series.id, | ||
files: [episodeFileId] | ||
} | ||
let renameResponse = sonarr.sendCommand('RenameFiles', renameBody, URI, ApiKey); | ||
let renameCompleted = sonarr.waitForCompletion(renameResponse.id); | ||
|
||
if (!renameCompleted) { | ||
Logger.ILog('Rename not completed'); | ||
return -1; | ||
} | ||
Logger.ILog(`Episode ${episodeFileId} successfully renamed. Setting as working file.`) | ||
|
||
// Sonarr has successfully renamed the file, set new filename as working directory | ||
let newFilePath = System.IO.Path.Combine(Variables.folder.FullName, newFileName); | ||
Flow.SetWorkingFile(newFilePath); | ||
return 1; | ||
|
||
} catch (error) { | ||
Logger.WLog('Error: ' + error.message); | ||
return -1; | ||
} | ||
} | ||
|
||
// Repeatedly try finding a show by shortening the path | ||
function findSeries(filePath, sonarr) { | ||
let currentPath = filePath; | ||
let show = null; | ||
|
||
while (currentPath) { | ||
show = sonarr.getShowByPath(currentPath); | ||
if (show) { | ||
Logger.ILog('Show found: ' + show.id); | ||
return show; | ||
} | ||
|
||
// If no show is found, go up 1 dir | ||
currentPath = System.IO.Path.GetDirectoryName(currentPath); | ||
if (currentPath === null || currentPath === "") { | ||
Logger.WLog('Unable to find show file at path ' + filePath); | ||
return null; | ||
} | ||
} | ||
} | ||
|
||
function fetchRenamedFiles(seriesId, sonarr) { | ||
let endpoint = 'rename'; | ||
let queryParams = `seriesId=${seriesId}`; | ||
let response = sonarr.fetchJson(endpoint, queryParams); | ||
return response; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters