diff --git a/source/net/sourceforge/filebot/cli/ScriptShell.java b/source/net/sourceforge/filebot/cli/ScriptShell.java index eebd8002..8324081b 100644 --- a/source/net/sourceforge/filebot/cli/ScriptShell.java +++ b/source/net/sourceforge/filebot/cli/ScriptShell.java @@ -14,6 +14,7 @@ import java.security.PrivilegedActionException; import java.security.PrivilegedExceptionAction; import java.security.ProtectionDomain; import java.util.PropertyPermission; +import java.util.logging.Logger; import javax.script.Bindings; import javax.script.ScriptContext; @@ -25,23 +26,22 @@ import javax.script.SimpleScriptContext; import org.codehaus.groovy.jsr223.GroovyScriptEngineFactory; import net.sourceforge.filebot.MediaTypes; +import net.sourceforge.filebot.WebServices; import net.sourceforge.filebot.format.PrivilegedInvocation; +import net.sourceforge.filebot.mediainfo.MediaInfo; +import net.sourceforge.filebot.web.EpisodeListProvider; +import net.sourceforge.filebot.web.MovieIdentificationService; class ScriptShell { - private final ScriptEngine engine = new GroovyScriptEngineFactory().getScriptEngine();; + private final ScriptEngine engine = new GroovyScriptEngineFactory().getScriptEngine(); - public ScriptShell(CmdlineInterface cli, ArgumentBean defaults, AccessControlContext acc) throws ScriptException { - Bindings bindings = new SimpleBindings(); - bindings.put("_cli", PrivilegedInvocation.newProxy(CmdlineInterface.class, cli, acc)); - bindings.put("_args", defaults); - bindings.put("_types", MediaTypes.getDefault()); - bindings.put("_log", CLILogger); - + public ScriptShell(CmdlineInterface cli, ArgumentBean args, AccessControlContext acc) throws ScriptException { + // setup script context ScriptContext context = new SimpleScriptContext(); - context.setBindings(bindings, ScriptContext.ENGINE_SCOPE); + context.setBindings(initializeBindings(cli, args, acc), ScriptContext.GLOBAL_SCOPE); engine.setContext(context); // import additional functions into the shell environment @@ -49,6 +49,32 @@ class ScriptShell { } + protected Bindings initializeBindings(CmdlineInterface cli, ArgumentBean args, AccessControlContext acc) { + Bindings bindings = new SimpleBindings(); + bindings.put("_cli", PrivilegedInvocation.newProxy(CmdlineInterface.class, cli, acc)); + bindings.put("_args", args); + bindings.put("_types", MediaTypes.getDefault()); + bindings.put("_log", CLILogger); + + // initialize web services + for (EpisodeListProvider service : WebServices.getEpisodeListProviders()) { + bindings.put(service.getName().toLowerCase(), PrivilegedInvocation.newProxy(EpisodeListProvider.class, service, acc)); + } + for (MovieIdentificationService service : WebServices.getMovieIdentificationServices()) { + bindings.put(service.getName().toLowerCase(), PrivilegedInvocation.newProxy(MovieIdentificationService.class, service, acc)); + } + + // load media info native lib + try { + bindings.put("mi", new MediaInfo()); + } catch (LinkageError e) { + Logger.getLogger(MediaInfo.class.getName()).warning("Failed to load MediaInfo"); + } + + return bindings; + } + + public Object evaluate(final String script, final Bindings bindings) throws Exception { try { return AccessController.doPrivileged(new PrivilegedExceptionAction() { diff --git a/source/net/sourceforge/filebot/cli/ScriptShell.lib.groovy b/source/net/sourceforge/filebot/cli/ScriptShell.lib.groovy index 8951b403..f9ca1ddc 100644 --- a/source/net/sourceforge/filebot/cli/ScriptShell.lib.groovy +++ b/source/net/sourceforge/filebot/cli/ScriptShell.lib.groovy @@ -1,25 +1,6 @@ // static imports for this script import static groovy.io.FileType.* -// File, Collection, Scanner, Random, UUID, etc. -import java.io.* -import java.util.* -import groovy.io.* -import groovy.util.* - -// our own functionality that might be useful -import net.sourceforge.tuned.FileUtilities -import net.sourceforge.filebot.WebServices -import net.sourceforge.filebot.MediaTypes -import net.sourceforge.filebot.ui.rename.MatchSimilarityMetric; - -import net.sourceforge.filebot.mediainfo.* -import net.sourceforge.filebot.hash.* -import net.sourceforge.filebot.similarity.* -import net.sourceforge.filebot.web.* - - - File.metaClass.isVideo = { _types.getFilter("video").accept(delegate) } File.metaClass.isSubtitle = { _types.getFilter("subtitle").accept(delegate) } diff --git a/source/net/sourceforge/filebot/format/ExpressionFormat.java b/source/net/sourceforge/filebot/format/ExpressionFormat.java index 0aff5436..82841bc1 100644 --- a/source/net/sourceforge/filebot/format/ExpressionFormat.java +++ b/source/net/sourceforge/filebot/format/ExpressionFormat.java @@ -157,7 +157,7 @@ public class ExpressionFormat extends Format { // initialize script context with the privileged bindings ScriptContext context = new SimpleScriptContext(); - context.setBindings(priviledgedBindings, ScriptContext.ENGINE_SCOPE); + context.setBindings(priviledgedBindings, ScriptContext.GLOBAL_SCOPE); // reset exception state lastException = null; diff --git a/website/data/shell/rsam.groovy b/website/data/shell/rsam.groovy new file mode 100644 index 00000000..769c698f --- /dev/null +++ b/website/data/shell/rsam.groovy @@ -0,0 +1,20 @@ +import net.sourceforge.filebot.similarity.* + +def lang = Locale.ENGLISH +def isMatch(a, b) { new NameSimilarityMetric().getSimilarity(a, b) > 0.9 } + +/* + * Rename anime, tv shows or movies (assuming each folder represents one item) + */ +args.eachMediaFolder { dir -> + def n = dir.getName() + + [ [db:anidb, query:{ anidb.search(n, lang).find{ isMatch(it, n) } }], + [db:thetvdb, query:{ thetvdb.search(n, lang).find{ isMatch(it, n) } }], + [db:themoviedb, query:{ themoviedb.searchMovie(n, lang).find{ isMatch(it, n) } }] + ].find { + def match = it.query() + if (match) { rename(folder:dir, db:it.db.getName(), query:match.getName()) } + return match + } +}