* support renaming folders via scripting rename(...) call

* improved nfo/imdb lookup for disk folders
This commit is contained in:
Reinhard Pointner 2012-07-26 05:50:47 +00:00
parent 165c66a319
commit 441063f257
5 changed files with 31 additions and 22 deletions

View File

@ -92,11 +92,6 @@ public class CmdlineOperations implements CmdlineInterface {
Locale locale = getLanguage(lang).toLocale();
ConflictAction conflictAction = ConflictAction.forName(conflict);
List<File> mediaFiles = filter(files, VIDEO_FILES, SUBTITLE_FILES);
if (mediaFiles.isEmpty()) {
throw new Exception("No media files: " + files);
}
if (getEpisodeListProvider(db) != null) {
// tv series mode
return renameSeries(files, action, conflictAction, outputDir, format, getEpisodeListProvider(db), query, SortOrder.forName(sortOrder), filter, locale, strict);
@ -108,9 +103,10 @@ public class CmdlineOperations implements CmdlineInterface {
}
// auto-determine mode
List<File> mediaFiles = filter(files, VIDEO_FILES, SUBTITLE_FILES);
double max = mediaFiles.size();
int sxe = 0; // SxE
int cws = 0; // common word sequence
double max = mediaFiles.size();
SeriesNameMatcher nameMatcher = new SeriesNameMatcher(locale);
Collection<String> cwsList = emptySet();
@ -144,7 +140,11 @@ public class CmdlineOperations implements CmdlineInterface {
public List<File> renameSeries(Collection<File> files, RenameAction renameAction, ConflictAction conflictAction, File outputDir, ExpressionFormat format, EpisodeListProvider db, String query, SortOrder sortOrder, ExpressionFilter filter, Locale locale, boolean strict) throws Exception {
CLILogger.config(format("Rename episodes using [%s]", db.getName()));
List<File> mediaFiles = filter(files, VIDEO_FILES, SUBTITLE_FILES);
if (mediaFiles.isEmpty()) {
throw new Exception("No media files: " + files);
}
// similarity metrics for matching
List<Match<File, Object>> matches = new ArrayList<Match<File, Object>>();
@ -296,7 +296,7 @@ public class CmdlineOperations implements CmdlineInterface {
// handle movie files
Set<File> movieFiles = new TreeSet<File>(filter(fileset, VIDEO_FILES));
Set<File> nfoFiles = new TreeSet<File>(filter(fileset, MediaTypes.getDefaultFilter("application/nfo")));
Set<File> nfoFiles = new TreeSet<File>(filter(fileset, NFO_FILES));
List<File> orphanedFiles = new ArrayList<File>(filter(fileset, FILES));
orphanedFiles.removeAll(movieFiles);
@ -336,7 +336,7 @@ public class CmdlineOperations implements CmdlineInterface {
// collect useful nfo files even if they are not part of the selected fileset
Set<File> effectiveNfoFileSet = new TreeSet<File>(nfoFiles);
for (File dir : mapByFolder(movieFiles).keySet()) {
addAll(effectiveNfoFileSet, dir.listFiles(MediaTypes.getDefaultFilter("application/nfo")));
addAll(effectiveNfoFileSet, dir.listFiles(NFO_FILES));
}
for (File nfo : effectiveNfoFileSet) {
try {
@ -372,9 +372,14 @@ public class CmdlineOperations implements CmdlineInterface {
List<File> movieMatchFiles = new ArrayList<File>();
movieMatchFiles.addAll(movieFiles);
movieMatchFiles.addAll(nfoFiles);
movieMatchFiles.addAll(filter(files, DISK_FOLDERS));
movieMatchFiles.addAll(filter(files, FOLDERS));
movieMatchFiles.addAll(filter(orphanedFiles, SUBTITLE_FILES)); // run movie detection only on orphaned subtitle files
// sanity check that we have something to do
if (fileset.isEmpty() || movieMatchFiles.isEmpty()) {
throw new Exception("No media files: " + files);
}
// map movies to (possibly multiple) files (in natural order)
Map<Movie, SortedSet<File>> filesByMovie = new HashMap<Movie, SortedSet<File>>();
@ -454,7 +459,8 @@ public class CmdlineOperations implements CmdlineInterface {
private File getDestinationFile(File original, String newName, File outputDir) {
File newFile = new File(newName + "." + getExtension(original));
String extension = getExtension(original);
File newFile = new File(extension != null ? newName + '.' + extension : newName);
// resolve against output dir
if (outputDir != null && !newFile.isAbsolute()) {

View File

@ -515,7 +515,7 @@ public class MediaBindingBean {
// make sure media file is defined
checkMediaFile();
if (SUBTITLE_FILES.accept(mediaFile) || getDefaultFilter("application/nfo").accept(mediaFile)) {
if (SUBTITLE_FILES.accept(mediaFile) || NFO_FILES.accept(mediaFile)) {
// file is a subtitle
String baseName = stripReleaseInfo(FileUtilities.getName(mediaFile)).toLowerCase();

View File

@ -568,15 +568,19 @@ public class MediaDetection {
Set<Integer> collection = new LinkedHashSet<Integer>();
List<File> nfoFiles = new ArrayList<File>();
if (file.isDirectory()) {
nfoFiles.addAll(listFiles(singleton(file), 10, false));
nfoFiles.addAll(filter(listFiles(singleton(file), 10, false), NFO_FILES));
} else {
addAll(nfoFiles, file.getParentFile().listFiles(NFO_FILES));
}
// parse ids from nfo files
for (File nfo : nfoFiles) {
String text = new String(readFile(nfo), "UTF-8");
collection.addAll(grepImdbId(text));
try {
String text = new String(readFile(nfo), "UTF-8");
collection.addAll(grepImdbId(text));
} catch (Exception e) {
Logger.getLogger(MediaDetection.class.getClass().getName()).log(Level.WARNING, "Failed to read nfo: " + e.getMessage());
}
}
return collection;

View File

@ -144,7 +144,7 @@ class MovieHashMatcher implements AutoCompleteMatcher {
List<File> movieMatchFiles = new ArrayList<File>();
movieMatchFiles.addAll(movieFiles);
movieMatchFiles.addAll(nfoFiles);
movieMatchFiles.addAll(filter(files, DISK_FOLDERS));
movieMatchFiles.addAll(filter(files, FOLDERS));
movieMatchFiles.addAll(filter(orphanedFiles, SUBTITLE_FILES)); // run movie detection only on orphaned subtitle files
// match remaining movies file by file in parallel

View File

@ -105,7 +105,12 @@ public final class FileUtilities {
InputStream in = new FileInputStream(source);
try {
byte[] data = new byte[(int) source.length()];
long size = source.length();
if (size < 0 || size > Integer.MAX_VALUE) {
throw new IllegalArgumentException("Unable to read file: " + source);
}
byte[] data = new byte[(int) size];
int position = 0;
int read = 0;
@ -177,7 +182,6 @@ public final class FileUtilities {
return Charset.forName("UTF-8").decode(data).toString();
}
/**
* Pattern used for matching file extensions.
*
@ -421,7 +425,6 @@ public final class FileUtilities {
return map;
}
/**
* Invalid file name characters: \, /, :, *, ?, ", <, >, |, \r and \n
*/
@ -508,7 +511,6 @@ public final class FileUtilities {
return buffer.toString();
}
public static final long KILO = 1024;
public static final long MEGA = KILO * 1024;
public static final long GIGA = MEGA * 1024;
@ -523,7 +525,6 @@ public final class FileUtilities {
return String.format("%,d Byte", size);
}
public static final FileFilter FOLDERS = new FileFilter() {
@Override
@ -532,7 +533,6 @@ public final class FileUtilities {
}
};
public static final FileFilter FILES = new FileFilter() {
@Override
@ -541,7 +541,6 @@ public final class FileUtilities {
}
};
public static final FileFilter TEMPORARY = new FileFilter() {
private final String tmpdir = System.getProperty("java.io.tmpdir");