* basic internal OSDB subtitle-upload support

This commit is contained in:
Reinhard Pointner 2013-07-23 19:28:24 +00:00
parent 4581f2221f
commit f33c01be94
5 changed files with 100 additions and 32 deletions

View File

@ -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

View File

@ -166,9 +166,15 @@ public class OpenSubtitlesXmlRpc {
Map<?, ?> response = invoke("TryUploadSubtitles", token, struct);
boolean uploadRequired = response.get("alreadyindb").equals("0");
Map<String, String> subtitleData = (Map<String, String>) response.get("data");
List<Map<String, String>> subtitleData = new ArrayList<Map<String, String>>();
return new TryUploadResponse(uploadRequired, Property.asEnumMap(subtitleData));
if (response.get("data") instanceof Map) {
subtitleData.add((Map<String, String>) response.get("data"));
} else if (response.get("data") instanceof List) {
subtitleData.addAll((List<Map<String, String>>) 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<Property, String> subtitleData;
private final List<Map<String, String>> subtitleData;
private TryUploadResponse(boolean uploadRequired, Map<Property, String> subtitleData) {
private TryUploadResponse(boolean uploadRequired, List<Map<String, String>> subtitleData) {
this.uploadRequired = uploadRequired;
this.subtitleData = subtitleData;
}
@ -501,9 +514,15 @@ public class OpenSubtitlesXmlRpc {
}
public Map<Property, String> getSubtitleData() {
public List<Map<String, String>> getSubtitleData() {
return subtitleData;
}
@Override
public String toString() {
return String.format("TryUploadResponse: %s => %s", uploadRequired, subtitleData);
}
}
}

View File

@ -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;
}

View File

@ -14,16 +14,16 @@ public interface VideoHashSubtitleService {
public Map<File, List<SubtitleDescriptor>> 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();
}

View File

@ -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()));
}