mirror of
https://github.com/mitb-archive/filebot
synced 2025-03-09 22:09:47 -04:00
filebot -revert /path
command
This commit is contained in:
parent
81efca26df
commit
94517baa98
@ -116,7 +116,7 @@ public class Main {
|
||||
|
||||
// CLI mode => run command-line interface and then exit
|
||||
if (args.runCLI()) {
|
||||
int status = new ArgumentProcessor().process(args, new CmdlineOperations());
|
||||
int status = new ArgumentProcessor().run(args);
|
||||
System.exit(status);
|
||||
}
|
||||
|
||||
|
@ -75,6 +75,9 @@ public class ArgumentBean {
|
||||
@Option(name = "-mediainfo", usage = "Get media info")
|
||||
public boolean mediaInfo = false;
|
||||
|
||||
@Option(name = "-revert", usage = "Revert files")
|
||||
public boolean revert = false;
|
||||
|
||||
@Option(name = "-extract", usage = "Extract archives")
|
||||
public boolean extract = false;
|
||||
|
||||
@ -118,7 +121,7 @@ public class ArgumentBean {
|
||||
public List<String> arguments = new ArrayList<String>();
|
||||
|
||||
public boolean runCLI() {
|
||||
return rename || getSubtitles || check || list || mediaInfo || extract || script != null;
|
||||
return rename || getSubtitles || check || list || mediaInfo || revert || extract || script != null;
|
||||
}
|
||||
|
||||
public boolean printVersion() {
|
||||
|
@ -11,6 +11,7 @@ import java.net.URI;
|
||||
import java.net.URL;
|
||||
import java.util.Collection;
|
||||
import java.util.LinkedHashSet;
|
||||
import java.util.List;
|
||||
import java.util.MissingResourceException;
|
||||
import java.util.logging.Level;
|
||||
import java.util.regex.Matcher;
|
||||
@ -28,30 +29,60 @@ import net.filebot.cli.ScriptShell.ScriptProvider;
|
||||
|
||||
public class ArgumentProcessor {
|
||||
|
||||
public int process(ArgumentBean args, CmdlineInterface cli) {
|
||||
public int run(ArgumentBean args) {
|
||||
try {
|
||||
if (args.script == null) {
|
||||
// execute command
|
||||
return runCommand(args);
|
||||
} else {
|
||||
// execute user script
|
||||
runScript(args);
|
||||
|
||||
// script finished successfully
|
||||
log.finest("Done ヾ(@⌒ー⌒@)ノ" + System.lineSeparator());
|
||||
return 0;
|
||||
}
|
||||
} catch (Throwable e) {
|
||||
if (findCause(e, CmdlineException.class) != null) {
|
||||
log.log(Level.WARNING, findCause(e, CmdlineException.class).getMessage());
|
||||
} else if (findCause(e, ScriptDeath.class) != null) {
|
||||
log.log(Level.WARNING, findCause(e, ScriptDeath.class).getMessage());
|
||||
} else {
|
||||
log.log(Level.SEVERE, String.format("%s: %s", getRootCause(e).getClass().getSimpleName(), getRootCauseMessage(e)), getRootCause(e));
|
||||
}
|
||||
}
|
||||
|
||||
// script failed with exception -> exit with non-zero exit code (and use positive code to avoid issues with launch4j launcher)
|
||||
log.finest("Failure (°_°)");
|
||||
return 1;
|
||||
}
|
||||
|
||||
public int runCommand(ArgumentBean args) throws Exception {
|
||||
CmdlineInterface cli = new CmdlineOperations();
|
||||
|
||||
// sanity checks
|
||||
if (args.getSubtitles && args.recursive) {
|
||||
throw new CmdlineException("`filebot -get-subtitles -r` has been disabled due to abuse. Please see http://bit.ly/suball for details.");
|
||||
}
|
||||
|
||||
// print episode info
|
||||
if (args.list) {
|
||||
log.setLevel(Level.WARNING); // make sure to disable any logging on standard output
|
||||
for (String eps : cli.fetchEpisodeList(args.query, args.format, args.db, args.order, args.filter, args.lang)) {
|
||||
System.out.println(eps);
|
||||
}
|
||||
return 0;
|
||||
List<String> lines = cli.fetchEpisodeList(args.query, args.format, args.db, args.order, args.filter, args.lang);
|
||||
lines.forEach(System.out::println);
|
||||
return lines.isEmpty() ? 1 : 0;
|
||||
}
|
||||
|
||||
// print media info
|
||||
if (args.mediaInfo) {
|
||||
for (String line : cli.getMediaInfo(args.getFiles(true), args.format, args.filter)) {
|
||||
System.out.println(line);
|
||||
}
|
||||
return 0;
|
||||
List<String> lines = cli.getMediaInfo(args.getFiles(true), args.format, args.filter);
|
||||
lines.forEach(System.out::println);
|
||||
return lines.isEmpty() ? 1 : 0;
|
||||
}
|
||||
|
||||
// execute CLI operations
|
||||
if (args.script == null) {
|
||||
// sanity checks
|
||||
if (args.getSubtitles && args.recursive) {
|
||||
throw new CmdlineException("`filebot -get-subtitles -r` has been disabled due to abuse. Please see http://bit.ly/suball for details.");
|
||||
// revert files
|
||||
if (args.revert) {
|
||||
List<File> files = cli.revert(args.getFiles(false), args.filter, "TEST".equalsIgnoreCase(args.action));
|
||||
return files.isEmpty() ? 1 : 0;
|
||||
}
|
||||
|
||||
// file operations
|
||||
@ -79,8 +110,11 @@ public class ArgumentProcessor {
|
||||
cli.compute(files, args.output, args.encoding);
|
||||
}
|
||||
}
|
||||
} else {
|
||||
// execute user script
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
public void runScript(ArgumentBean args) throws Throwable {
|
||||
Bindings bindings = new SimpleBindings();
|
||||
bindings.put(ScriptShell.SHELL_ARGV_BINDING_NAME, args.getArray());
|
||||
bindings.put(ScriptShell.ARGV_BINDING_NAME, args.getFiles(false));
|
||||
@ -106,24 +140,6 @@ public class ArgumentProcessor {
|
||||
shell.runScript(script, bindings);
|
||||
}
|
||||
|
||||
// script finished successfully
|
||||
log.finest("Done ヾ(@⌒ー⌒@)ノ" + System.lineSeparator());
|
||||
return 0;
|
||||
} catch (Throwable e) {
|
||||
if (findCause(e, CmdlineException.class) != null) {
|
||||
log.log(Level.WARNING, findCause(e, CmdlineException.class).getMessage());
|
||||
} else if (findCause(e, ScriptDeath.class) != null) {
|
||||
log.log(Level.WARNING, findCause(e, ScriptDeath.class).getMessage());
|
||||
} else {
|
||||
log.log(Level.SEVERE, String.format("%s: %s", getRootCause(e).getClass().getSimpleName(), getRootCauseMessage(e)), getRootCause(e));
|
||||
}
|
||||
}
|
||||
|
||||
// script failed with exception -> exit with non-zero exit code (and use positive code to avoid issues with launch4j launcher)
|
||||
log.finest("Failure (°_°)");
|
||||
return 1;
|
||||
}
|
||||
|
||||
public static class DefaultScriptProvider implements ScriptProvider {
|
||||
|
||||
public static final String SCHEME_REMOTE_STABLE = "fn";
|
||||
|
@ -14,6 +14,8 @@ public interface CmdlineInterface {
|
||||
|
||||
List<File> rename(Map<File, File> renameMap, RenameAction renameAction, String conflict) throws Exception;
|
||||
|
||||
List<File> revert(Collection<File> files, String filter, boolean test) throws Exception;
|
||||
|
||||
List<File> getSubtitles(Collection<File> files, String db, String query, String lang, String output, String encoding, String format, boolean strict) throws Exception;
|
||||
|
||||
List<File> getMissingSubtitles(Collection<File> files, String db, String query, String lang, String output, String encoding, String format, boolean strict) throws Exception;
|
||||
|
@ -3,6 +3,7 @@ package net.filebot.cli;
|
||||
import static java.nio.charset.StandardCharsets.*;
|
||||
import static java.util.Arrays.*;
|
||||
import static java.util.Collections.*;
|
||||
import static java.util.stream.Collectors.*;
|
||||
import static net.filebot.Logging.*;
|
||||
import static net.filebot.MediaTypes.*;
|
||||
import static net.filebot.Settings.*;
|
||||
@ -22,6 +23,7 @@ import java.util.AbstractMap;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
import java.util.HashMap;
|
||||
import java.util.HashSet;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.LinkedHashSet;
|
||||
import java.util.List;
|
||||
@ -29,6 +31,7 @@ import java.util.Locale;
|
||||
import java.util.Map;
|
||||
import java.util.Map.Entry;
|
||||
import java.util.NoSuchElementException;
|
||||
import java.util.Objects;
|
||||
import java.util.Set;
|
||||
import java.util.SortedSet;
|
||||
import java.util.TreeMap;
|
||||
@ -1070,24 +1073,45 @@ public class CmdlineOperations implements CmdlineInterface {
|
||||
|
||||
@Override
|
||||
public List<String> getMediaInfo(Collection<File> files, String format, String filter) throws Exception {
|
||||
if (filter != null && filter.length() > 0) {
|
||||
ExpressionFileFilter includes = new ExpressionFileFilter(new ExpressionFilter(filter), false);
|
||||
files = filter(files, includes);
|
||||
|
||||
if (files.isEmpty()) {
|
||||
throw new CmdlineException("No files: " + files);
|
||||
}
|
||||
}
|
||||
|
||||
ExpressionFormat formatter = new ExpressionFormat(format != null && format.length() > 0 ? format : "{fn} [{resolution} {vc} {channels} {ac} {minutes+'m'}]");
|
||||
FileFilter fileFilter = filter == null || filter.isEmpty() ? f -> true : new ExpressionFileFilter(new ExpressionFilter(filter), false);
|
||||
|
||||
List<String> output = new ArrayList<String>();
|
||||
for (File file : files) {
|
||||
for (File file : filter(files, fileFilter)) {
|
||||
String line = formatter.format(new MediaBindingBean(readMetaInfo(file), file, null));
|
||||
output.add(line);
|
||||
}
|
||||
return output;
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<File> revert(Collection<File> files, String filter, boolean test) throws Exception {
|
||||
FileFilter fileFilter = filter == null || filter.isEmpty() ? f -> true : new ExpressionFileFilter(new ExpressionFilter(filter), false);
|
||||
|
||||
Set<File> whitelist = new HashSet<File>(files);
|
||||
Map<File, File> history = HistorySpooler.getInstance().getCompleteHistory().getRenameMap();
|
||||
|
||||
return history.entrySet().stream().filter(it -> {
|
||||
File current = it.getValue();
|
||||
return current.exists() && listPath(current).stream().anyMatch(whitelist::contains) && fileFilter.accept(current);
|
||||
}).map(it -> {
|
||||
File original = it.getKey();
|
||||
File current = it.getValue();
|
||||
|
||||
log.info(format("Revert [%s] to [%s]", current, original));
|
||||
if (test) {
|
||||
return original;
|
||||
}
|
||||
|
||||
try {
|
||||
return StandardRenameAction.revert(current, original);
|
||||
} catch (Exception e) {
|
||||
log.warning(format("Failed to revert file: %s", e.getMessage()));
|
||||
return null;
|
||||
}
|
||||
}).filter(Objects::nonNull).collect(toList());
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<File> extract(Collection<File> files, String output, String conflict, FileFilter filter, boolean forceExtractAll) throws Exception {
|
||||
ConflictAction conflictAction = ConflictAction.forName(conflict);
|
||||
@ -1170,4 +1194,5 @@ public class CmdlineOperations implements CmdlineInterface {
|
||||
|
||||
return extractedFiles;
|
||||
}
|
||||
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user