From f33c01be946d67b3218c8026d70ee2a9fd523a5f Mon Sep 17 00:00:00 2001 From: Reinhard Pointner Date: Tue, 23 Jul 2013 19:28:24 +0000 Subject: [PATCH] * basic internal OSDB subtitle-upload support --- .../filebot/web/OpenSubtitlesClient.java | 57 ++++++++++++++++-- .../filebot/web/OpenSubtitlesXmlRpc.java | 59 ++++++++++++------- .../filebot/web/SublightSubtitleClient.java | 2 +- .../filebot/web/VideoHashSubtitleService.java | 10 ++-- .../filebot/web/OpenSubtitlesXmlRpcTest.java | 4 +- 5 files changed, 100 insertions(+), 32 deletions(-) diff --git a/source/net/sourceforge/filebot/web/OpenSubtitlesClient.java b/source/net/sourceforge/filebot/web/OpenSubtitlesClient.java index 707350e7..5480b739 100644 --- a/source/net/sourceforge/filebot/web/OpenSubtitlesClient.java +++ b/source/net/sourceforge/filebot/web/OpenSubtitlesClient.java @@ -6,8 +6,10 @@ import static java.lang.Math.*; import static java.util.Arrays.*; import static java.util.Collections.*; import static net.sourceforge.filebot.web.OpenSubtitlesHasher.*; +import static net.sourceforge.tuned.FileUtilities.*; import java.io.File; +import java.io.IOException; import java.math.BigInteger; import java.net.URI; import java.security.MessageDigest; @@ -32,7 +34,12 @@ import javax.swing.Icon; import net.sourceforge.filebot.Cache; import net.sourceforge.filebot.Cache.Key; import net.sourceforge.filebot.ResourceManager; +import net.sourceforge.filebot.mediainfo.MediaInfo; +import net.sourceforge.filebot.mediainfo.MediaInfo.StreamKind; +import net.sourceforge.filebot.web.OpenSubtitlesXmlRpc.BaseInfo; import net.sourceforge.filebot.web.OpenSubtitlesXmlRpc.Query; +import net.sourceforge.filebot.web.OpenSubtitlesXmlRpc.SubFile; +import net.sourceforge.filebot.web.OpenSubtitlesXmlRpc.TryUploadResponse; import net.sourceforge.tuned.Timer; import redstone.xmlrpc.XmlRpcException; @@ -191,8 +198,50 @@ public class OpenSubtitlesClient implements SubtitleProvider, VideoHashSubtitleS @Override - public boolean publishSubtitle(int imdbid, String languageName, File videoFile, File subtitleFile) throws Exception { - //TODO implement upload feature + public boolean publishSubtitle(int imdbid, String languageName, File[] videoFile, File[] subtitleFile) throws Exception { + SubFile[] subs = new SubFile[subtitleFile.length]; + + // subhash (md5 of subtitles), subfilename, moviehash, moviebytesize, moviefilename. + for (int i = 0; i < subtitleFile.length; i++) { + SubFile sub = new SubFile(); + sub.setSubHash(md5(subtitleFile[i])); + sub.setSubFileName(subtitleFile[i].getName()); + sub.setMovieHash(computeHash(videoFile[i])); + sub.setMovieByteSize(videoFile[i].length()); + sub.setMovieFileName(videoFile[i].getName()); + subs[i] = sub; + } + + // require login + login(); + + // check if subs already exist in db + TryUploadResponse response = xmlrpc.tryUploadSubtitles(subs); + System.out.println(response); // TODO only upload if necessary OR return false + + BaseInfo info = new BaseInfo(); + info.setIDMovieImdb(imdbid); + info.setSubLanguageID(getSubLanguageID(languageName)); + + // encode subtitle contents + for (int i = 0; i < subtitleFile.length; i++) { + // grab subtitle content + subs[i].setSubContent(readFile(subtitleFile[i])); + + try { + // grab media info + MediaInfo mi = new MediaInfo(); + mi.open(videoFile[i]); + subs[i].setMovieFPS(mi.get(StreamKind.Video, 0, "FrameRate")); + subs[i].setMovieTimeMS(mi.get(StreamKind.General, 0, "Duration")); + mi.close(); + } catch (Throwable e) { + Logger.getLogger(getClass().getName()).log(Level.WARNING, e.getMessage(), e); + } + } + + URI resource = xmlrpc.uploadSubtitles(info, subs); + System.out.println(resource); return false; } @@ -200,10 +249,10 @@ public class OpenSubtitlesClient implements SubtitleProvider, VideoHashSubtitleS /** * Calculate MD5 hash. */ - private String md5(byte[] data) { + private String md5(File file) throws IOException { try { MessageDigest hash = MessageDigest.getInstance("MD5"); - hash.update(data); + hash.update(readFile(file)); return String.format("%032x", new BigInteger(1, hash.digest())); // as hex string } catch (NoSuchAlgorithmException e) { throw new RuntimeException(e); // won't happen diff --git a/source/net/sourceforge/filebot/web/OpenSubtitlesXmlRpc.java b/source/net/sourceforge/filebot/web/OpenSubtitlesXmlRpc.java index 33d04934..3b548d61 100644 --- a/source/net/sourceforge/filebot/web/OpenSubtitlesXmlRpc.java +++ b/source/net/sourceforge/filebot/web/OpenSubtitlesXmlRpc.java @@ -166,9 +166,15 @@ public class OpenSubtitlesXmlRpc { Map response = invoke("TryUploadSubtitles", token, struct); boolean uploadRequired = response.get("alreadyindb").equals("0"); - Map subtitleData = (Map) response.get("data"); + List> subtitleData = new ArrayList>(); - return new TryUploadResponse(uploadRequired, Property.asEnumMap(subtitleData)); + if (response.get("data") instanceof Map) { + subtitleData.add((Map) response.get("data")); + } else if (response.get("data") instanceof List) { + subtitleData.addAll((List>) response.get("data")); + } + + return new TryUploadResponse(uploadRequired, subtitleData); } @@ -457,21 +463,6 @@ public class OpenSubtitlesXmlRpc { } - public void setMovieTimeMS(int movietimems) { - put("movietimems", movietimems); - } - - - public void setMovieFrames(int movieframes) { - put("movieframes", movieframes); - } - - - public void setMovieFPS(double moviefps) { - put("moviefps", moviefps); - } - - public void setMovieFileName(String moviefilename) { put("moviefilename", moviefilename); } @@ -480,6 +471,28 @@ public class OpenSubtitlesXmlRpc { public void setSubContent(byte[] data) { put("subcontent", encodeData(data)); } + + + public void setMovieTimeMS(String movietimems) { + if (movietimems.length() > 0) { + put("movietimems", movietimems); + } + } + + + public void setMovieFPS(String moviefps) { + if (moviefps.length() > 0) { + put("moviefps", moviefps); + } + } + + + public void setMovieFrames(String movieframes) { + if (movieframes.length() > 0) { + put("movieframes", movieframes); + } + } + } @@ -487,10 +500,10 @@ public class OpenSubtitlesXmlRpc { private final boolean uploadRequired; - private final Map subtitleData; + private final List> subtitleData; - private TryUploadResponse(boolean uploadRequired, Map subtitleData) { + private TryUploadResponse(boolean uploadRequired, List> subtitleData) { this.uploadRequired = uploadRequired; this.subtitleData = subtitleData; } @@ -501,9 +514,15 @@ public class OpenSubtitlesXmlRpc { } - public Map getSubtitleData() { + public List> getSubtitleData() { return subtitleData; } + + + @Override + public String toString() { + return String.format("TryUploadResponse: %s => %s", uploadRequired, subtitleData); + } } } diff --git a/source/net/sourceforge/filebot/web/SublightSubtitleClient.java b/source/net/sourceforge/filebot/web/SublightSubtitleClient.java index 25f48a9d..b8fcb143 100644 --- a/source/net/sourceforge/filebot/web/SublightSubtitleClient.java +++ b/source/net/sourceforge/filebot/web/SublightSubtitleClient.java @@ -246,7 +246,7 @@ public class SublightSubtitleClient implements SubtitleProvider, VideoHashSubtit @Override - public boolean publishSubtitle(int imdbid, String languageName, File videoFile, File subtitleFile) throws Exception { + public boolean publishSubtitle(int imdbid, String languageName, File[] videoFile, File[] subtitleFile) throws Exception { //TODO implement upload feature return false; } diff --git a/source/net/sourceforge/filebot/web/VideoHashSubtitleService.java b/source/net/sourceforge/filebot/web/VideoHashSubtitleService.java index fa906b5f..6c71406c 100644 --- a/source/net/sourceforge/filebot/web/VideoHashSubtitleService.java +++ b/source/net/sourceforge/filebot/web/VideoHashSubtitleService.java @@ -14,16 +14,16 @@ public interface VideoHashSubtitleService { public Map> getSubtitleList(File[] videoFiles, String languageName) throws Exception; - - public boolean publishSubtitle(int imdbid, String languageName, File videoFile, File subtitleFile) throws Exception; - + public boolean publishSubtitle(int imdbid, String languageName, File[] videoFile, File[] subtitleFile) throws Exception; + + public String getName(); - + public URI getLink(); - + public Icon getIcon(); } diff --git a/test/net/sourceforge/filebot/web/OpenSubtitlesXmlRpcTest.java b/test/net/sourceforge/filebot/web/OpenSubtitlesXmlRpcTest.java index 4d521b39..1f12839d 100644 --- a/test/net/sourceforge/filebot/web/OpenSubtitlesXmlRpcTest.java +++ b/test/net/sourceforge/filebot/web/OpenSubtitlesXmlRpcTest.java @@ -106,8 +106,8 @@ public class OpenSubtitlesXmlRpcTest { TryUploadResponse response = xmlrpc.tryUploadSubtitles(subtitle); assertFalse(response.isUploadRequired()); - assertEquals("4513264", response.getSubtitleData().get(Property.IDSubtitle)); - assertEquals("eng", response.getSubtitleData().get(Property.SubLanguageID)); + assertEquals("4513264", response.getSubtitleData().get(0).get(Property.IDSubtitle.toString())); + assertEquals("eng", response.getSubtitleData().get(0).get(Property.SubLanguageID.toString())); }