Skip to content

Instantly share code, notes, and snippets.

@0x1b-xyz
Created March 11, 2012 15:24
Show Gist options
  • Save 0x1b-xyz/2016802 to your computer and use it in GitHub Desktop.
Save 0x1b-xyz/2016802 to your computer and use it in GitHub Desktop.
KMTTG "Custom" script that delegates file renaming to FileBot through the -script feature.
/**
* A FileBot (http://filebot.sourceforge.net) script that first renames the incoming file using the TiVo metadata
* then delegates to the rename() call of FileBot to translate the airdate of TV shows into an SXXEXX format. Movies
* should just pass through without change as long as we had a "movieYear" attribute in the metadata.
*
* To use from KMTTG, set something like this for your "Custom Command" and make sure your "metadata files" is set
* to "last". You should be able to use this strategy with encoded OR mpg files. Your file naming pattern within KMTTG
* shouldn't matter, but my setting is below.
*
* file naming value:
* [year]-[monthNum]-[mday]-[hour][min] [title]
*
* custom command example value:
* /Applications/FileBot.app/Contents/MacOS/filebot -script /Users/kphonik/Library/Scripts/Theater/FileBotRename.groovy "[mpegFile]" -trust-script -non-strict
*
* @author Jason Stiefel <[email protected]>
*/
def cleanText = { source ->
if (!source)
return ''
source.replaceAll(':','_').replaceAll('/','_')
}
def readMd = { video ->
def md = [:]
def mdFile = new File("${video.absolutePath}.txt")
mdFile.eachLine {
it.eachMatch ~/([^:]*):(.*)/, { full, name, value ->
md[name.trim()] = value.trim()
}
}
return [md, mdFile];
}
if (!args[0])
throw new InvalidArgumentException("Must provide a file to process")
def video = args[0]
if (!video.exists())
throw new FileNotFoundException(video.absolutePath)
println "Processing metadata for file ${video.absolutePath}"
def (md, mdFile) = readMd(video)
println "Loaded ${md.size()} metadata elements from ${mdFile.absolutePath}"
def title = cleanText(md['title'])
def isShow = (md['isEpisode'] == 'true')
def renamed = new StringBuilder(title)
if (isShow) {
def airDate = md['originalAirDate']?.find ~/[^T]*/
if (airDate)
renamed << " - ${airDate}"
def episode = cleanText(md['episodeTitle'])
if (episode)
renamed << " - ${episode}"
} else {
def movieYear = md['movieYear']
if (movieYear)
renamed << " (${movieYear})"
}
// I suffix my TiVo shows with ".dvr" to identify them from other sources. You can remove appearances
// of that string without breaking the script.
def videoTarget = new File(video.absoluteFile.parentFile, "${renamed}.dvr.${video.name.tokenize('.').last()}")
println "Determined target as ${videoTarget.absolutePath}"
if (!videoTarget.exists())
video.renameTo(videoTarget)
video = videoTarget
println "Video is now named: ${video.absolutePath}"
def results;
if (isShow) {
println "Renaming as a TV Show: ${video.name}"
results = rename(query: title, file: video, format: '{n} - {s00e00} - {t}.dvr', db: 'TheTVDB')
} else {
println "Renaming as a Movie: ${video.name}"
results = rename(query: title, file: video, format: '{n} ({y}){" CD$pi"}.dvr', db: 'TheMovieDB')
}
video = results[0] ?: video;
def mdTarget = new File(new File(video.absoluteFile.parentFile, "Metadata"), "${video.name}.txt")
println "Moving metadata to ${mdTarget.absolutePath}"
if (!mdTarget.exists())
mdFile.renameTo(mdTarget)
return results;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment