Improve subtitle upload behaviour

This commit is contained in:
Reinhard Pointner 2016-02-03 18:14:11 +00:00
parent 9ab34c3202
commit ec14f3c084
2 changed files with 39 additions and 17 deletions

View File

@ -289,14 +289,14 @@ abstract class SubtitleDropTarget extends JButton {
// 1. try to find exact match in drop data // 1. try to find exact match in drop data
return findMatch(subtitle, videos, FileUtilities::getName).orElseGet(() -> { return findMatch(subtitle, videos, FileUtilities::getName).orElseGet(() -> {
// 2. guess movie file from the parent folder if only a subtitle file was dropped in // 2. guess movie file from the parent folder if only a subtitle file was dropped in
return findMatch(subtitle, getChildren(subtitle.getParentFile(), VIDEO_FILES), FileUtilities::getName).get(); return findMatch(subtitle, getChildren(subtitle.getParentFile(), VIDEO_FILES), FileUtilities::getName).orElse(null);
}); });
} }
private Optional<File> findMatch(File file, List<File> options, Function<File, String> comparator) { private Optional<File> findMatch(File file, List<File> options, Function<File, String> comparator) {
String name = comparator.apply(file).toLowerCase(); String subtitleFileName = comparator.apply(file).toLowerCase();
for (File it : options) { for (File it : options) {
if (name.length() > 0 && comparator.apply(it).toLowerCase().startsWith(name)) { if (subtitleFileName.length() > 0 && subtitleFileName.startsWith(comparator.apply(it).toLowerCase())) {
return Optional.of(it); return Optional.of(it);
} }
} }

View File

@ -1,5 +1,6 @@
package net.filebot.web; package net.filebot.web;
import static java.util.Arrays.*;
import static java.util.Collections.*; import static java.util.Collections.*;
import static net.filebot.util.StringUtilities.*; import static net.filebot.util.StringUtilities.*;
@ -13,6 +14,7 @@ import java.net.URL;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collection; import java.util.Collection;
import java.util.HashMap; import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.Map.Entry; import java.util.Map.Entry;
@ -163,14 +165,37 @@ public class OpenSubtitlesXmlRpc {
return null; return null;
} }
@SuppressWarnings("unchecked") private static final Pattern CDI_PATTERN = Pattern.compile("(?<!\\p{Alnum})CD(?<i>[1-9])(?!\\p{Digit})", Pattern.CASE_INSENSITIVE | Pattern.UNICODE_CHARACTER_CLASS);
public TryUploadResponse tryUploadSubtitles(SubFile... subtitles) throws XmlRpcFault {
Map<String, SubFile> struct = new HashMap<String, SubFile>(); private Map<String, Object> getUploadStruct(BaseInfo baseInfo, SubFile... subtitles) {
Map<String, Object> struct = new LinkedHashMap<String, Object>();
// put baseinfo
if (baseInfo != null) {
struct.put("baseinfo", baseInfo);
}
// put cd1, cd2, ... // put cd1, cd2, ...
for (SubFile cd : subtitles) { for (SubFile it : subtitles) {
struct.put(String.format("cd%d", struct.size() + 1), cd); int i = 1;
Matcher m = CDI_PATTERN.matcher(it.toString());
while (m.find()) {
i = Integer.parseInt(m.group("i"));
} }
String key = "cd" + i;
if (!struct.containsKey(key)) {
struct.put(key, it);
} else {
throw new IllegalArgumentException(String.format("Duplicate key: %s: %s", key, asList(subtitles)));
}
}
return struct;
}
@SuppressWarnings("unchecked")
public TryUploadResponse tryUploadSubtitles(SubFile... subtitles) throws XmlRpcFault {
Map<String, Object> struct = getUploadStruct(null, subtitles);
Map<?, ?> response = invoke("TryUploadSubtitles", token, struct); Map<?, ?> response = invoke("TryUploadSubtitles", token, struct);
@ -187,15 +212,7 @@ public class OpenSubtitlesXmlRpc {
} }
public URI uploadSubtitles(BaseInfo baseInfo, SubFile... subtitles) throws XmlRpcFault { public URI uploadSubtitles(BaseInfo baseInfo, SubFile... subtitles) throws XmlRpcFault {
Map<String, Object> struct = new HashMap<String, Object>(); Map<String, Object> struct = getUploadStruct(baseInfo, subtitles);
// put cd1, cd2, ...
for (SubFile cd : subtitles) {
struct.put(String.format("cd%d", struct.size() + 1), cd);
}
// put baseinfo
struct.put("baseinfo", baseInfo);
Map<?, ?> response = invoke("UploadSubtitles", token, struct); Map<?, ?> response = invoke("UploadSubtitles", token, struct);
@ -498,6 +515,11 @@ public class OpenSubtitlesXmlRpc {
} }
} }
@Override
public String toString() {
return String.format("(%s, %s)", get("moviefilename"), get("subfilename"));
}
} }
public static final class TryUploadResponse { public static final class TryUploadResponse {