1
0
mirror of https://github.com/mitb-archive/filebot synced 2024-12-23 16:28:51 -05:00

* added new IO capabilities to scripting env

This commit is contained in:
Reinhard Pointner 2012-01-04 14:39:39 +00:00
parent 9277722163
commit 9e6d7e0a2b
3 changed files with 34 additions and 6 deletions

View File

@ -60,14 +60,28 @@ import java.nio.ByteBuffer
import java.nio.charset.Charset
import static net.sourceforge.filebot.web.WebRequest.*
URL.metaClass.post = { parameters -> post(delegate.openConnection(), parameters) }
URL.metaClass.get = { readAll(getReader(delegate.openConnection())) }
URL.metaClass.fetch = { fetch(delegate) }
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())))) }
URL.metaClass.post = { Map parameters -> post(delegate.openConnection(), parameters) }
URL.metaClass.post = { byte[] data, contentType = 'application/octet-stream' -> post(delegate.openConnection(), data, contentType) }
URL.metaClass.post = { String text, csn = 'utf-8' -> delegate.post(text.getBytes(csn), 'text/plain') }
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) }
def telnet(host, int port, csn = 'utf-8', Closure handler) {
def socket = new Socket(host, port)
try {
handler.call(new PrintStream(socket.outputStream, true, csn), socket.inputStream.newReader(csn))
} finally {
socket.close()
}
}
// Template Engine helpers
import groovy.text.XmlTemplateEngine

View File

@ -153,12 +153,13 @@ public final class WebRequest {
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
connection.addRequestProperty("Content-Type", "application/x-www-form-urlencoded");
return post(connection, encodeParameters(parameters).getBytes("UTF-8"), "application/x-www-form-urlencoded");
}
public static ByteBuffer post(HttpURLConnection connection, byte[] postData, String contentType) throws IOException {
connection.addRequestProperty("Content-Length", String.valueOf(postData.length));
connection.addRequestProperty("Content-Type", contentType);
connection.setRequestMethod("POST");
connection.setDoOutput(true);

View File

@ -6,6 +6,11 @@ def episodeFormat = "V:/out/TV/{n}{'/Season '+s}/{episode}"
def movieDir = "V:/in/Movies"
def movieFormat = "V:/out/Movies/{movie}/{movie}"
// XBMC ON LOCAL MACHINE
def xbmc = ['localhost'] // (use [] to not notify any XBMC instances about updates)
// ignore chunk, part, par and hidden files
def incomplete(f) { f.name =~ /[.]chunk|[.]part\d{0,3}$|[.]par$|[.]dat$/ || f.isHidden() }
@ -63,3 +68,11 @@ movieDir.getFolders{ !it.hasFile{ incomplete(it) } && it.hasFile{ it.isVideo() }
// sort movies / subtitles
rename(file:files, db:'OpenSubtitles', format:movieFormat)
}
// make XBMC scan for new content
xbmc.each { host ->
telnet(host, 9090) { writer, reader ->
writer.println('{"jsonrpc": "2.0", "method": "VideoLibrary.ScanForContent", "id": 1}')
}
}