1
0
mirror of https://github.com/mitb-archive/filebot synced 2024-11-04 08:25:03 -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
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

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

View File

@ -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<String, String> 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<String, String> parameters) throws IOException {
public static ByteBuffer post(HttpURLConnection connection, Map<String, ?> 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<String, String> parameters) {
public static String encodeParameters(Map<String, ?> parameters) {
StringBuilder sb = new StringBuilder();
for (Entry<String, String> entry : parameters.entrySet()) {
if (sb.length() > 0)
for (Entry<String, ?> 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.
*/