From ec14f3c084ff365f776a7f5a2952821e35168e60 Mon Sep 17 00:00:00 2001 From: Reinhard Pointner Date: Wed, 3 Feb 2016 18:14:11 +0000 Subject: [PATCH] Improve subtitle upload behaviour --- .../ui/subtitle/SubtitleDropTarget.java | 6 +-- .../net/filebot/web/OpenSubtitlesXmlRpc.java | 50 +++++++++++++------ 2 files changed, 39 insertions(+), 17 deletions(-) diff --git a/source/net/filebot/ui/subtitle/SubtitleDropTarget.java b/source/net/filebot/ui/subtitle/SubtitleDropTarget.java index 18f24a7b..daebe2f2 100644 --- a/source/net/filebot/ui/subtitle/SubtitleDropTarget.java +++ b/source/net/filebot/ui/subtitle/SubtitleDropTarget.java @@ -289,14 +289,14 @@ abstract class SubtitleDropTarget extends JButton { // 1. try to find exact match in drop data return findMatch(subtitle, videos, FileUtilities::getName).orElseGet(() -> { // 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 findMatch(File file, List options, Function comparator) { - String name = comparator.apply(file).toLowerCase(); + String subtitleFileName = comparator.apply(file).toLowerCase(); 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); } } diff --git a/source/net/filebot/web/OpenSubtitlesXmlRpc.java b/source/net/filebot/web/OpenSubtitlesXmlRpc.java index 09fd5805..12dffe88 100644 --- a/source/net/filebot/web/OpenSubtitlesXmlRpc.java +++ b/source/net/filebot/web/OpenSubtitlesXmlRpc.java @@ -1,5 +1,6 @@ package net.filebot.web; +import static java.util.Arrays.*; import static java.util.Collections.*; import static net.filebot.util.StringUtilities.*; @@ -13,6 +14,7 @@ import java.net.URL; import java.util.ArrayList; import java.util.Collection; import java.util.HashMap; +import java.util.LinkedHashMap; import java.util.List; import java.util.Map; import java.util.Map.Entry; @@ -163,15 +165,38 @@ public class OpenSubtitlesXmlRpc { return null; } - @SuppressWarnings("unchecked") - public TryUploadResponse tryUploadSubtitles(SubFile... subtitles) throws XmlRpcFault { - Map struct = new HashMap(); + private static final Pattern CDI_PATTERN = Pattern.compile("(?[1-9])(?!\\p{Digit})", Pattern.CASE_INSENSITIVE | Pattern.UNICODE_CHARACTER_CLASS); + + private Map getUploadStruct(BaseInfo baseInfo, SubFile... subtitles) { + Map struct = new LinkedHashMap(); + + // put baseinfo + if (baseInfo != null) { + struct.put("baseinfo", baseInfo); + } // put cd1, cd2, ... - for (SubFile cd : subtitles) { - struct.put(String.format("cd%d", struct.size() + 1), cd); + for (SubFile it : subtitles) { + 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 struct = getUploadStruct(null, subtitles); + Map response = invoke("TryUploadSubtitles", token, struct); boolean uploadRequired = response.get("alreadyindb").toString().equals("0"); @@ -187,15 +212,7 @@ public class OpenSubtitlesXmlRpc { } public URI uploadSubtitles(BaseInfo baseInfo, SubFile... subtitles) throws XmlRpcFault { - Map struct = new HashMap(); - - // put cd1, cd2, ... - for (SubFile cd : subtitles) { - struct.put(String.format("cd%d", struct.size() + 1), cd); - } - - // put baseinfo - struct.put("baseinfo", baseInfo); + Map struct = getUploadStruct(baseInfo, subtitles); 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 {