From cb16e56f875fea24708e8fe7796ac5cef82d8cca Mon Sep 17 00:00:00 2001 From: Reinhard Pointner Date: Wed, 28 Dec 2011 06:29:02 +0000 Subject: [PATCH] * enhancements useful for scripting --- .../filebot/cli/ScriptShell.lib.groovy | 12 +++-- .../filebot/web/OpenSubtitlesClient.java | 5 ++ .../sourceforge/filebot/web/WebRequest.java | 49 ++++++++++--------- 3 files changed, 40 insertions(+), 26 deletions(-) diff --git a/source/net/sourceforge/filebot/cli/ScriptShell.lib.groovy b/source/net/sourceforge/filebot/cli/ScriptShell.lib.groovy index 4ad3b19e..c9666ed3 100644 --- a/source/net/sourceforge/filebot/cli/ScriptShell.lib.groovy +++ b/source/net/sourceforge/filebot/cli/ScriptShell.lib.groovy @@ -56,12 +56,18 @@ def parallel(List closures, int threads = Runtime.getRuntime().availableProcesso // Web and File IO helpers +import java.nio.ByteBuffer import java.nio.charset.Charset import static net.sourceforge.filebot.web.WebRequest.* -URL.metaClass.parseHtml = { new XmlParser(false, false).parseText(getXmlString(getHtmlDocument(delegate))) } -URL.metaClass.saveAs = { f -> writeFile(fetch(delegate), f); f.absolutePath } -String.metaClass.saveAs = { f, csn = "utf-8" -> writeFile(Charset.forName(csn).encode(delegate), f); f.absolutePath } +URL.metaClass.post = { parameters -> post(delegate.openConnection(), parameters) } +URL.metaClass.getHtml = { new XmlParser(false, false).parseText(getXmlString(getHtmlDocument(delegate))) } +ByteBuffer.metaClass.getHtml = { csn = "utf-8" -> new XmlParser(false, false).parseText(getXmlString(getHtmlDocument(new StringReader(Charset.forName(csn).decode(delegate.duplicate()).toString())))) } + +ByteBuffer.metaClass.saveAs = { f -> f = f instanceof File ? f : new File(f.toString()); writeFile(delegate.duplicate(), f); f.absolutePath }; +URL.metaClass.saveAs = { f -> fetch(delegate).saveAs(f) } +String.metaClass.saveAs = { f, csn = "utf-8" -> Charset.forName(csn).encode(delegate).saveAs(f) } + // Template Engine helpers import groovy.text.XmlTemplateEngine diff --git a/source/net/sourceforge/filebot/web/OpenSubtitlesClient.java b/source/net/sourceforge/filebot/web/OpenSubtitlesClient.java index c4926279..04fd89e8 100644 --- a/source/net/sourceforge/filebot/web/OpenSubtitlesClient.java +++ b/source/net/sourceforge/filebot/web/OpenSubtitlesClient.java @@ -189,6 +189,11 @@ public class OpenSubtitlesClient implements SubtitleProvider, VideoHashSubtitleS } + public Movie getMovieDescriptor(File movieFile, Locale locale) throws Exception { + return getMovieDescriptors(new File[] { movieFile }, locale)[0]; + } + + @Override public Movie[] getMovieDescriptors(File[] movieFiles, Locale locale) throws Exception { // create result array diff --git a/source/net/sourceforge/filebot/web/WebRequest.java b/source/net/sourceforge/filebot/web/WebRequest.java index 00964871..d588070d 100644 --- a/source/net/sourceforge/filebot/web/WebRequest.java +++ b/source/net/sourceforge/filebot/web/WebRequest.java @@ -48,12 +48,12 @@ public final class WebRequest { return getHtmlDocument(url.openConnection()); } - + public static Document getHtmlDocument(URLConnection connection) throws IOException, SAXException { return getHtmlDocument(getReader(connection)); } - + public static Reader getReader(URLConnection connection) throws IOException { try { connection.addRequestProperty("Accept-Encoding", "gzip,deflate"); @@ -76,7 +76,7 @@ public final class WebRequest { return new InputStreamReader(inputStream, charset); } - + public static Document getHtmlDocument(Reader reader) throws SAXException, IOException { DOMParser parser = new DOMParser(); parser.setFeature("http://xml.org/sax/features/namespaces", false); @@ -85,17 +85,17 @@ public final class WebRequest { return parser.getDocument(); } - + public static Document getDocument(URL url) throws IOException, SAXException { return getDocument(url.openConnection()); } - + public static Document getDocument(URLConnection connection) throws IOException, SAXException { return getDocument(new InputSource(getReader(connection))); } - + public static Document getDocument(InputSource source) throws IOException, SAXException { try { return DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(source); @@ -105,17 +105,17 @@ public final class WebRequest { } } - + public static ByteBuffer fetch(URL resource) throws IOException { return fetch(resource, 0, null); } - + public static ByteBuffer fetchIfModified(URL resource, long ifModifiedSince) throws IOException { return fetch(resource, ifModifiedSince, null); } - + public static ByteBuffer fetch(URL url, long ifModifiedSince, Map requestParameters) throws IOException { URLConnection connection = url.openConnection(); connection.setIfModifiedSince(ifModifiedSince); @@ -151,8 +151,8 @@ public final class WebRequest { return buffer.getByteBuffer(); } - - public static ByteBuffer post(HttpURLConnection connection, Map parameters) throws IOException { + + public static ByteBuffer post(HttpURLConnection connection, Map parameters) throws IOException { byte[] postData = encodeParameters(parameters).getBytes("UTF-8"); // add content type and content length headers @@ -189,7 +189,7 @@ public final class WebRequest { return buffer.getByteBuffer(); } - + private static Charset getCharset(String contentType) { if (contentType != null) { // e.g. Content-Type: text/html; charset=iso-8859-1 @@ -213,23 +213,26 @@ public final class WebRequest { return Charset.forName("UTF-8"); } - - public static String encodeParameters(Map parameters) { + + public static String encodeParameters(Map parameters) { StringBuilder sb = new StringBuilder(); - for (Entry entry : parameters.entrySet()) { - if (sb.length() > 0) + for (Entry entry : parameters.entrySet()) { + if (sb.length() > 0) { sb.append("&"); + } sb.append(entry.getKey()); - sb.append("="); - sb.append(encode(entry.getValue())); + if (entry.getValue() != null) { + sb.append("="); + sb.append(encode(entry.getValue().toString())); + } } return sb.toString(); } - + public static String encode(String string) { try { return URLEncoder.encode(string, "UTF-8"); @@ -238,7 +241,7 @@ public final class WebRequest { } } - + public static SSLSocketFactory createIgnoreCertificateSocketFactory() { // create a trust manager that does not validate certificate chains TrustManager trustAnyCertificate = new X509TrustManager() { @@ -247,11 +250,11 @@ public final class WebRequest { return null; } - + public void checkClientTrusted(X509Certificate[] certs, String authType) { } - + public void checkServerTrusted(X509Certificate[] certs, String authType) { } }; @@ -265,7 +268,7 @@ public final class WebRequest { } } - + /** * Dummy constructor to prevent instantiation. */