* enhancements useful for scripting
This commit is contained in:
parent
99c52281f9
commit
cb16e56f87
|
@ -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
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -152,7 +152,7 @@ public final class WebRequest {
|
|||
}
|
||||
|
||||
|
||||
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
|
||||
|
@ -214,16 +214,19 @@ public final class WebRequest {
|
|||
}
|
||||
|
||||
|
||||
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());
|
||||
if (entry.getValue() != null) {
|
||||
sb.append("=");
|
||||
sb.append(encode(entry.getValue()));
|
||||
sb.append(encode(entry.getValue().toString()));
|
||||
}
|
||||
}
|
||||
|
||||
return sb.toString();
|
||||
|
|
Loading…
Reference in New Issue