Created
February 28, 2010 23:32
-
-
Save dizzi/317915 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import groovyx.net.http.* | |
import static groovyx.net.http.ContentType.* | |
import static groovyx.net.http.Method.* | |
def dlUrl = 'http://helheim.sh.cvut.cz' | |
def dlPath = '/~dizzi/autodl/' | |
def dlLocPath = 'd:/' | |
def http = new HTTPBuilder( dlUrl ) | |
// perform a GET request, expecting JSON response data | |
http.request( GET, TEXT ) { | |
uri.path = dlPath | |
// response handler for a success response code: | |
response.success = { resp, text -> | |
text.eachLine { line -> | |
if (line.startsWith("<li>")){ | |
println(line) | |
match = (line =~ /(?<=href=\")(.*)(?=\">)/) | |
if (match.getCount()>=1){ | |
dlFileName=match[0][0] | |
println(dlFileName) | |
println(dlUrl+dlPath+dlFileName) | |
def file = new File(dlLocPath+dlFileName) | |
if (file.exists()){ | |
println("File already exists... skipping") | |
} else { | |
def fos = new FileOutputStream(file) | |
def out = new BufferedOutputStream(fos) | |
println("Saving file...") | |
out << new URL(dlUrl+dlPath+dlFileName).openStream() | |
out.close() | |
} | |
} | |
} | |
} | |
} | |
// handler for any failure status code: | |
response.failure = { resp -> | |
println "Unexpected error: ${resp.statusLine.statusCode} : ${resp.statusLine.reasonPhrase}" | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment