mirror of
https://github.com/mitb-archive/filebot
synced 2024-12-25 01:08:52 -05:00
* pluggable rename action! new it's possible to pass in a closure as rename action
* fixed some problems with absolute paths being resolved as relative paths
This commit is contained in:
parent
45594c4179
commit
d9df1f21af
@ -33,6 +33,7 @@ import org.kohsuke.args4j.CmdLineParser;
|
|||||||
|
|
||||||
import net.sourceforge.filebot.Analytics;
|
import net.sourceforge.filebot.Analytics;
|
||||||
import net.sourceforge.filebot.MediaTypes;
|
import net.sourceforge.filebot.MediaTypes;
|
||||||
|
import net.sourceforge.filebot.StandardRenameAction;
|
||||||
import net.sourceforge.filebot.cli.ScriptShell.Script;
|
import net.sourceforge.filebot.cli.ScriptShell.Script;
|
||||||
import net.sourceforge.filebot.cli.ScriptShell.ScriptProvider;
|
import net.sourceforge.filebot.cli.ScriptShell.ScriptProvider;
|
||||||
import net.sourceforge.filebot.web.CachedResource;
|
import net.sourceforge.filebot.web.CachedResource;
|
||||||
@ -106,7 +107,7 @@ public class ArgumentProcessor {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (args.rename) {
|
if (args.rename) {
|
||||||
cli.rename(files, args.action, args.conflict, args.output, args.format, args.db, args.query, args.order, args.filter, args.lang, !args.nonStrict);
|
cli.rename(files, StandardRenameAction.forName(args.action), args.conflict, args.output, args.format, args.db, args.query, args.order, args.filter, args.lang, !args.nonStrict);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (args.check) {
|
if (args.check) {
|
||||||
|
@ -6,10 +6,12 @@ import java.io.File;
|
|||||||
import java.util.Collection;
|
import java.util.Collection;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
|
import net.sourceforge.filebot.RenameAction;
|
||||||
|
|
||||||
|
|
||||||
public interface CmdlineInterface {
|
public interface CmdlineInterface {
|
||||||
|
|
||||||
List<File> rename(Collection<File> files, String action, String conflict, String output, String format, String db, String query, String sortOrder, String filter, String lang, boolean strict) throws Exception;
|
List<File> rename(Collection<File> files, RenameAction action, String conflict, String output, String format, String db, String query, String sortOrder, String filter, String lang, boolean strict) throws Exception;
|
||||||
|
|
||||||
|
|
||||||
List<File> getSubtitles(Collection<File> files, String db, String query, String lang, String output, String encoding, boolean strict) throws Exception;
|
List<File> getSubtitles(Collection<File> files, String db, String query, String lang, String output, String encoding, boolean strict) throws Exception;
|
||||||
|
@ -47,7 +47,6 @@ import net.sourceforge.filebot.Analytics;
|
|||||||
import net.sourceforge.filebot.HistorySpooler;
|
import net.sourceforge.filebot.HistorySpooler;
|
||||||
import net.sourceforge.filebot.MediaTypes;
|
import net.sourceforge.filebot.MediaTypes;
|
||||||
import net.sourceforge.filebot.RenameAction;
|
import net.sourceforge.filebot.RenameAction;
|
||||||
import net.sourceforge.filebot.StandardRenameAction;
|
|
||||||
import net.sourceforge.filebot.WebServices;
|
import net.sourceforge.filebot.WebServices;
|
||||||
import net.sourceforge.filebot.archive.Archive;
|
import net.sourceforge.filebot.archive.Archive;
|
||||||
import net.sourceforge.filebot.archive.FileMapper;
|
import net.sourceforge.filebot.archive.FileMapper;
|
||||||
@ -85,12 +84,12 @@ import net.sourceforge.tuned.FileUtilities.ParentFilter;
|
|||||||
public class CmdlineOperations implements CmdlineInterface {
|
public class CmdlineOperations implements CmdlineInterface {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public List<File> rename(Collection<File> files, String action, String conflict, String output, String formatExpression, String db, String query, String sortOrder, String filterExpression, String lang, boolean strict) throws Exception {
|
public List<File> rename(Collection<File> files, RenameAction action, String conflict, String output, String formatExpression, String db, String query, String sortOrder, String filterExpression, String lang, boolean strict)
|
||||||
|
throws Exception {
|
||||||
ExpressionFormat format = (formatExpression != null) ? new ExpressionFormat(formatExpression) : null;
|
ExpressionFormat format = (formatExpression != null) ? new ExpressionFormat(formatExpression) : null;
|
||||||
ExpressionFilter filter = (filterExpression != null) ? new ExpressionFilter(filterExpression) : null;
|
ExpressionFilter filter = (filterExpression != null) ? new ExpressionFilter(filterExpression) : null;
|
||||||
File outputDir = (output != null && output.length() > 0) ? new File(output).getAbsoluteFile() : null;
|
File outputDir = (output != null && output.length() > 0) ? new File(output).getAbsoluteFile() : null;
|
||||||
Locale locale = getLanguage(lang).toLocale();
|
Locale locale = getLanguage(lang).toLocale();
|
||||||
RenameAction renameAction = StandardRenameAction.forName(action);
|
|
||||||
ConflictAction conflictAction = ConflictAction.forName(conflict);
|
ConflictAction conflictAction = ConflictAction.forName(conflict);
|
||||||
|
|
||||||
List<File> mediaFiles = filter(files, VIDEO_FILES, SUBTITLE_FILES);
|
List<File> mediaFiles = filter(files, VIDEO_FILES, SUBTITLE_FILES);
|
||||||
@ -100,12 +99,12 @@ public class CmdlineOperations implements CmdlineInterface {
|
|||||||
|
|
||||||
if (getEpisodeListProvider(db) != null) {
|
if (getEpisodeListProvider(db) != null) {
|
||||||
// tv series mode
|
// tv series mode
|
||||||
return renameSeries(files, renameAction, conflictAction, outputDir, format, getEpisodeListProvider(db), query, SortOrder.forName(sortOrder), filter, locale, strict);
|
return renameSeries(files, action, conflictAction, outputDir, format, getEpisodeListProvider(db), query, SortOrder.forName(sortOrder), filter, locale, strict);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (getMovieIdentificationService(db) != null) {
|
if (getMovieIdentificationService(db) != null) {
|
||||||
// movie mode
|
// movie mode
|
||||||
return renameMovie(files, renameAction, conflictAction, outputDir, format, getMovieIdentificationService(db), query, locale, strict);
|
return renameMovie(files, action, conflictAction, outputDir, format, getMovieIdentificationService(db), query, locale, strict);
|
||||||
}
|
}
|
||||||
|
|
||||||
// auto-determine mode
|
// auto-determine mode
|
||||||
@ -136,9 +135,9 @@ public class CmdlineOperations implements CmdlineInterface {
|
|||||||
|
|
||||||
CLILogger.finest(format("Filename pattern: [%.02f] SxE, [%.02f] CWS", sxe / max, cws / max));
|
CLILogger.finest(format("Filename pattern: [%.02f] SxE, [%.02f] CWS", sxe / max, cws / max));
|
||||||
if (sxe >= (max * 0.65) || cws >= (max * 0.65)) {
|
if (sxe >= (max * 0.65) || cws >= (max * 0.65)) {
|
||||||
return renameSeries(files, renameAction, conflictAction, outputDir, format, WebServices.TheTVDB, query, SortOrder.forName(sortOrder), filter, locale, strict); // use default episode db
|
return renameSeries(files, action, conflictAction, outputDir, format, WebServices.TheTVDB, query, SortOrder.forName(sortOrder), filter, locale, strict); // use default episode db
|
||||||
} else {
|
} else {
|
||||||
return renameMovie(files, renameAction, conflictAction, outputDir, format, WebServices.TMDb, query, locale, strict); // use default movie db
|
return renameMovie(files, action, conflictAction, outputDir, format, WebServices.TMDb, query, locale, strict); // use default movie db
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -208,14 +207,8 @@ public class CmdlineOperations implements CmdlineInterface {
|
|||||||
File file = match.getValue();
|
File file = match.getValue();
|
||||||
Object episode = match.getCandidate();
|
Object episode = match.getCandidate();
|
||||||
String newName = (format != null) ? format.format(new MediaBindingBean(episode, file)) : validateFileName(EpisodeFormat.SeasonEpisode.format(episode));
|
String newName = (format != null) ? format.format(new MediaBindingBean(episode, file)) : validateFileName(EpisodeFormat.SeasonEpisode.format(episode));
|
||||||
File newFile = new File(outputDir, newName + "." + getExtension(file));
|
|
||||||
|
|
||||||
if (isInvalidFilePath(newFile) && !isUnixFS()) {
|
renameMap.put(file, getDestinationFile(file, newName, outputDir));
|
||||||
CLILogger.config("Stripping invalid characters from new path: " + newName);
|
|
||||||
newFile = validateFilePath(newFile);
|
|
||||||
}
|
|
||||||
|
|
||||||
renameMap.put(file, newFile);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// rename episodes
|
// rename episodes
|
||||||
@ -443,19 +436,30 @@ public class CmdlineOperations implements CmdlineInterface {
|
|||||||
File file = match.getValue();
|
File file = match.getValue();
|
||||||
Object movie = match.getCandidate();
|
Object movie = match.getCandidate();
|
||||||
String newName = (format != null) ? format.format(new MediaBindingBean(movie, file)) : validateFileName(MovieFormat.NameYear.format(movie));
|
String newName = (format != null) ? format.format(new MediaBindingBean(movie, file)) : validateFileName(MovieFormat.NameYear.format(movie));
|
||||||
File newFile = new File(outputDir, newName + "." + getExtension(file));
|
|
||||||
|
renameMap.put(file, getDestinationFile(file, newName, outputDir));
|
||||||
|
}
|
||||||
|
|
||||||
|
// rename movies
|
||||||
|
Analytics.trackEvent("CLI", "Rename", "Movie", renameMap.size());
|
||||||
|
return renameAll(renameMap, renameAction, conflictAction);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
private File getDestinationFile(File original, String newName, File outputDir) {
|
||||||
|
File newFile = new File(newName + "." + getExtension(original));
|
||||||
|
|
||||||
|
// resolve against output dir
|
||||||
|
if (outputDir != null && !newFile.isAbsolute()) {
|
||||||
|
newFile = new File(outputDir, newFile.getPath());
|
||||||
|
}
|
||||||
|
|
||||||
if (isInvalidFilePath(newFile) && !isUnixFS()) {
|
if (isInvalidFilePath(newFile) && !isUnixFS()) {
|
||||||
CLILogger.config("Stripping invalid characters from new path: " + newName);
|
CLILogger.config("Stripping invalid characters from new path: " + newName);
|
||||||
newFile = validateFilePath(newFile);
|
newFile = validateFilePath(newFile);
|
||||||
}
|
}
|
||||||
|
|
||||||
renameMap.put(file, newFile);
|
return newFile;
|
||||||
}
|
|
||||||
|
|
||||||
// rename movies
|
|
||||||
Analytics.trackEvent("CLI", "Rename", "Movie", renameMap.size());
|
|
||||||
return renameAll(renameMap, renameAction, conflictAction);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -210,7 +210,7 @@ def include(String input, Map parameters = [:], Object... args) {
|
|||||||
// CLI bindings
|
// CLI bindings
|
||||||
def rename(args) { args = _defaults(args)
|
def rename(args) { args = _defaults(args)
|
||||||
synchronized (_cli) {
|
synchronized (_cli) {
|
||||||
_guarded { _cli.rename(_files(args), args.action as String, args.conflict as String, args.output as String, args.format as String, args.db as String, args.query as String, args.order as String, args.filter as String, args.lang as String, args.strict as Boolean) }
|
_guarded { _cli.rename(_files(args), _renameFunction(args.action), args.conflict as String, args.output as String, args.format as String, args.db as String, args.query as String, args.order as String, args.filter as String, args.lang as String, args.strict as Boolean) }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -275,6 +275,20 @@ def _files(args) {
|
|||||||
return files
|
return files
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
// allow Groovy to hook into rename interface
|
||||||
|
import net.sourceforge.filebot.*
|
||||||
|
|
||||||
|
def _renameFunction(fn) {
|
||||||
|
if (fn instanceof String)
|
||||||
|
return StandardRenameAction.forName(fn)
|
||||||
|
if (fn instanceof Closure)
|
||||||
|
return [rename:fn as Closure, toString:{'CLOSURE'}] as RenameAction
|
||||||
|
|
||||||
|
return fn as RenameAction
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Fill in default values from cmdline arguments
|
* Fill in default values from cmdline arguments
|
||||||
*/
|
*/
|
||||||
|
Loading…
Reference in New Issue
Block a user