* added more advanced examples for the scripting shell
This commit is contained in:
parent
f7719ccd86
commit
c59f27d048
|
@ -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<Object>() {
|
||||
|
|
|
@ -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) }
|
||||
|
|
|
@ -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;
|
||||
|
|
|
@ -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
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue