1
0
mirror of https://github.com/mitb-archive/filebot synced 2024-11-16 14:25:02 -05:00

* enhancements useful for scripting

This commit is contained in:
Reinhard Pointner 2011-12-28 06:29:02 +00:00
parent 99c52281f9
commit cb16e56f87
3 changed files with 40 additions and 26 deletions

View File

@ -56,12 +56,18 @@ def parallel(List closures, int threads = Runtime.getRuntime().availableProcesso
// Web and File IO helpers // Web and File IO helpers
import java.nio.ByteBuffer
import java.nio.charset.Charset import java.nio.charset.Charset
import static net.sourceforge.filebot.web.WebRequest.* import static net.sourceforge.filebot.web.WebRequest.*
URL.metaClass.parseHtml = { new XmlParser(false, false).parseText(getXmlString(getHtmlDocument(delegate))) } URL.metaClass.post = { parameters -> post(delegate.openConnection(), parameters) }
URL.metaClass.saveAs = { f -> writeFile(fetch(delegate), f); f.absolutePath } URL.metaClass.getHtml = { new XmlParser(false, false).parseText(getXmlString(getHtmlDocument(delegate))) }
String.metaClass.saveAs = { f, csn = "utf-8" -> writeFile(Charset.forName(csn).encode(delegate), f); f.absolutePath } 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 // Template Engine helpers
import groovy.text.XmlTemplateEngine import groovy.text.XmlTemplateEngine

View File

@ -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 @Override
public Movie[] getMovieDescriptors(File[] movieFiles, Locale locale) throws Exception { public Movie[] getMovieDescriptors(File[] movieFiles, Locale locale) throws Exception {
// create result array // create result array

View File

@ -48,12 +48,12 @@ public final class WebRequest {
return getHtmlDocument(url.openConnection()); return getHtmlDocument(url.openConnection());
} }
public static Document getHtmlDocument(URLConnection connection) throws IOException, SAXException { public static Document getHtmlDocument(URLConnection connection) throws IOException, SAXException {
return getHtmlDocument(getReader(connection)); return getHtmlDocument(getReader(connection));
} }
public static Reader getReader(URLConnection connection) throws IOException { public static Reader getReader(URLConnection connection) throws IOException {
try { try {
connection.addRequestProperty("Accept-Encoding", "gzip,deflate"); connection.addRequestProperty("Accept-Encoding", "gzip,deflate");
@ -76,7 +76,7 @@ public final class WebRequest {
return new InputStreamReader(inputStream, charset); return new InputStreamReader(inputStream, charset);
} }
public static Document getHtmlDocument(Reader reader) throws SAXException, IOException { public static Document getHtmlDocument(Reader reader) throws SAXException, IOException {
DOMParser parser = new DOMParser(); DOMParser parser = new DOMParser();
parser.setFeature("http://xml.org/sax/features/namespaces", false); parser.setFeature("http://xml.org/sax/features/namespaces", false);
@ -85,17 +85,17 @@ public final class WebRequest {
return parser.getDocument(); return parser.getDocument();
} }
public static Document getDocument(URL url) throws IOException, SAXException { public static Document getDocument(URL url) throws IOException, SAXException {
return getDocument(url.openConnection()); return getDocument(url.openConnection());
} }
public static Document getDocument(URLConnection connection) throws IOException, SAXException { public static Document getDocument(URLConnection connection) throws IOException, SAXException {
return getDocument(new InputSource(getReader(connection))); return getDocument(new InputSource(getReader(connection)));
} }
public static Document getDocument(InputSource source) throws IOException, SAXException { public static Document getDocument(InputSource source) throws IOException, SAXException {
try { try {
return DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(source); return DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(source);
@ -105,17 +105,17 @@ public final class WebRequest {
} }
} }
public static ByteBuffer fetch(URL resource) throws IOException { public static ByteBuffer fetch(URL resource) throws IOException {
return fetch(resource, 0, null); return fetch(resource, 0, null);
} }
public static ByteBuffer fetchIfModified(URL resource, long ifModifiedSince) throws IOException { public static ByteBuffer fetchIfModified(URL resource, long ifModifiedSince) throws IOException {
return fetch(resource, ifModifiedSince, null); return fetch(resource, ifModifiedSince, null);
} }
public static ByteBuffer fetch(URL url, long ifModifiedSince, Map<String, String> requestParameters) throws IOException { public static ByteBuffer fetch(URL url, long ifModifiedSince, Map<String, String> requestParameters) throws IOException {
URLConnection connection = url.openConnection(); URLConnection connection = url.openConnection();
connection.setIfModifiedSince(ifModifiedSince); connection.setIfModifiedSince(ifModifiedSince);
@ -151,8 +151,8 @@ public final class WebRequest {
return buffer.getByteBuffer(); return buffer.getByteBuffer();
} }
public static ByteBuffer post(HttpURLConnection connection, Map<String, String> parameters) throws IOException { public static ByteBuffer post(HttpURLConnection connection, Map<String, ?> parameters) throws IOException {
byte[] postData = encodeParameters(parameters).getBytes("UTF-8"); byte[] postData = encodeParameters(parameters).getBytes("UTF-8");
// add content type and content length headers // add content type and content length headers
@ -189,7 +189,7 @@ public final class WebRequest {
return buffer.getByteBuffer(); return buffer.getByteBuffer();
} }
private static Charset getCharset(String contentType) { private static Charset getCharset(String contentType) {
if (contentType != null) { if (contentType != null) {
// e.g. Content-Type: text/html; charset=iso-8859-1 // e.g. Content-Type: text/html; charset=iso-8859-1
@ -213,23 +213,26 @@ public final class WebRequest {
return Charset.forName("UTF-8"); return Charset.forName("UTF-8");
} }
public static String encodeParameters(Map<String, String> parameters) { public static String encodeParameters(Map<String, ?> parameters) {
StringBuilder sb = new StringBuilder(); StringBuilder sb = new StringBuilder();
for (Entry<String, String> entry : parameters.entrySet()) { for (Entry<String, ?> entry : parameters.entrySet()) {
if (sb.length() > 0) if (sb.length() > 0) {
sb.append("&"); sb.append("&");
}
sb.append(entry.getKey()); sb.append(entry.getKey());
sb.append("="); if (entry.getValue() != null) {
sb.append(encode(entry.getValue())); sb.append("=");
sb.append(encode(entry.getValue().toString()));
}
} }
return sb.toString(); return sb.toString();
} }
public static String encode(String string) { public static String encode(String string) {
try { try {
return URLEncoder.encode(string, "UTF-8"); return URLEncoder.encode(string, "UTF-8");
@ -238,7 +241,7 @@ public final class WebRequest {
} }
} }
public static SSLSocketFactory createIgnoreCertificateSocketFactory() { public static SSLSocketFactory createIgnoreCertificateSocketFactory() {
// create a trust manager that does not validate certificate chains // create a trust manager that does not validate certificate chains
TrustManager trustAnyCertificate = new X509TrustManager() { TrustManager trustAnyCertificate = new X509TrustManager() {
@ -247,11 +250,11 @@ public final class WebRequest {
return null; return null;
} }
public void checkClientTrusted(X509Certificate[] certs, String authType) { public void checkClientTrusted(X509Certificate[] certs, String authType) {
} }
public void checkServerTrusted(X509Certificate[] certs, String authType) { public void checkServerTrusted(X509Certificate[] certs, String authType) {
} }
}; };
@ -265,7 +268,7 @@ public final class WebRequest {
} }
} }
/** /**
* Dummy constructor to prevent instantiation. * Dummy constructor to prevent instantiation.
*/ */