From cfb6512063d2458192357aea173bd0740baf0600 Mon Sep 17 00:00:00 2001 From: Reinhard Pointner Date: Tue, 29 Nov 2011 06:03:41 +0000 Subject: [PATCH] * added helper for running system commands from the groovy shell --- source/net/sourceforge/filebot/Main.java | 2 +- .../filebot/cli/ScriptShell.lib.groovy | 25 ++++++++++++-- website/data/shell/sorty.groovy | 33 +++++++++++++++++++ 3 files changed, 56 insertions(+), 4 deletions(-) create mode 100644 website/data/shell/sorty.groovy diff --git a/source/net/sourceforge/filebot/Main.java b/source/net/sourceforge/filebot/Main.java index 6cad1e64..f9b3b3d0 100644 --- a/source/net/sourceforge/filebot/Main.java +++ b/source/net/sourceforge/filebot/Main.java @@ -50,7 +50,7 @@ public class Main { final ArgumentProcessor cli = new ArgumentProcessor(); final ArgumentBean args = cli.parse(arguments); - if (args.printHelp() || args.printVersion() || isHeadless()) { + if (args.printHelp() || args.printVersion() || (!args.runCLI() && isHeadless())) { System.out.format("%s / %s%n%n", getApplicationIdentifier(), getJavaRuntimeIdentifier()); if (args.printHelp() || (!args.printVersion() && isHeadless())) { diff --git a/source/net/sourceforge/filebot/cli/ScriptShell.lib.groovy b/source/net/sourceforge/filebot/cli/ScriptShell.lib.groovy index 2e99c32f..8ecced7f 100644 --- a/source/net/sourceforge/filebot/cli/ScriptShell.lib.groovy +++ b/source/net/sourceforge/filebot/cli/ScriptShell.lib.groovy @@ -1,12 +1,15 @@ +// File selector methods import static groovy.io.FileType.* +File.metaClass.plus = { path -> new File(delegate, path) } +File.metaClass.listFiles = { c -> delegate.isDirectory() ? delegate.listFiles().findAll(c) : []} + File.metaClass.isVideo = { _types.getFilter("video").accept(delegate) } File.metaClass.isAudio = { _types.getFilter("audio").accept(delegate) } File.metaClass.isSubtitle = { _types.getFilter("subtitle").accept(delegate) } File.metaClass.isVerification = { _types.getFilter("verification").accept(delegate) } -File.metaClass.plus = { path -> new File(delegate, path) } File.metaClass.hasFile = { c -> isDirectory() && listFiles().find{ c.call(it) }} File.metaClass.getFiles = { def files = []; traverse(type:FILES) { files += it }; return files } @@ -22,7 +25,7 @@ String.metaClass.eachMediaFolder = { c -> new File(delegate).eachMediaFolder(c) List.metaClass.eachMediaFolder = { c -> getFolders().findAll{ it.hasFile{ it.isVideo() } }.each(c) } -// FileUtilities +// File utility methods import static net.sourceforge.tuned.FileUtilities.*; File.metaClass.getNameWithoutExtension = { getNameWithoutExtension(delegate.getName()) } @@ -32,15 +35,31 @@ File.metaClass.isDerived = { f -> isDerived(delegate, f) } File.metaClass.validateFileName = { validateFileName(delegate) } File.metaClass.validateFilePath = { validateFilePath(delegate) } File.metaClass.moveTo = { f -> renameFile(delegate, f) } - List.metaClass.mapByFolder = { mapByFolder(delegate) } List.metaClass.mapByExtension = { mapByExtension(delegate) } +// Shell helper +import static com.sun.jna.Platform.*; + +def run(String... cmd) { + cmd = cmd.toList() + if (isWindows()) { + cmd = ["cmd", "/c"] + cmd; + } + + // run command and print output + def process = cmd.execute() + process.waitForProcessOutput(System.out, System.err) + + return process.exitValue() +} +// Script helper def require(cond) { if (!cond()) throw new Exception('Require failed') } +// CLI bindings def rename(args) { args = _defaults(args) _guarded { _cli.rename(_files(args), args.query, args.format, args.db, args.lang, args.strict) } } diff --git a/website/data/shell/sorty.groovy b/website/data/shell/sorty.groovy new file mode 100644 index 00000000..31f10bb8 --- /dev/null +++ b/website/data/shell/sorty.groovy @@ -0,0 +1,33 @@ +// Settings +def source = "X:/source" +def target = "Y:/target" + +def episodeFormat = "{n}{'/Season '+s}/{episode}" +def movieFormat = "{movie}/{movie}" + +def exclude(file) { + file =~ /\p{Punct}chunk/ +} + +/* + * Fetch subtitles and sort into folders + */ +"$source/TV".eachMediaFolder() { dir -> + def files = dir.listFiles { !exclude(it) } + + // fetch subtitles + files += getSubtitles(file:files) + + // sort episodes / subtitles + rename(file:files, db:'TVRage', format:"$target/TV/$episodeFormat") +} + +"$source/Movies".eachMediaFolder() { dir -> + def files = dir.listFiles { !exclude(it) } + + // fetch subtitles + files += getSubtitles(file:files) + + // sort movies / subtitles + rename(file:files, db:'TheMovieDB', format:"$target/Movies/$movieFormat") +}