Skip to content

Instantly share code, notes, and snippets.

@dizzi
Created February 28, 2010 23:32
Show Gist options
  • Save dizzi/317915 to your computer and use it in GitHub Desktop.
Save dizzi/317915 to your computer and use it in GitHub Desktop.
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