Skip to content

Instantly share code, notes, and snippets.

@ckaminski
Created April 7, 2014 16:54
Show Gist options
  • Save ckaminski/10024047 to your computer and use it in GitHub Desktop.
Save ckaminski/10024047 to your computer and use it in GitHub Desktop.
Grab a users gists from GitHub
@Grab(group='org.codehaus.groovy.modules.http-builder', module='http-builder', version='0.7')
@Grab(group='oauth.signpost', module='signpost-core', version='1.2.1.2')
@Grab(group='oauth.signpost', module='signpost-commonshttp4', version='1.2.1.2')
import java.io.*
import groovyx.net.http.*
import groovyx.net.http.RESTClient
import groovy.json.JsonSlurper
import static groovyx.net.http.Method.*
import static groovyx.net.http.ContentType.*
def cli = new CliBuilder(usage:'u')
cli.u(args:1, 'username')
cli.p(args:1, 'page')
def options = cli.parse(args)
userAgent = 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/33.0.1750.154 Safari/537.36'
srcUrl = 'https://api.github.com/'
restUrl = '/users/' + options.u + '/gists'
def http = new HTTPBuilder(srcUrl);
http.request(GET, JSON) {
uri.query = [page: options.p]
uri.path = restUrl
headers.'User-Agent' = userAgent
response.success = { resp, json ->
dirName = 'gists.' + options.u
dir = new File(dirName).mkdir()
resp.allHeaders.each {println it}
json.each {
it.files.each { key, value ->
fileName = value.filename
raw_url = value.raw_url
def httpChild = new HTTPBuilder('https://gist.githubusercontent.com');
httpChild.request(GET, TEXT) {
headers.'User-Agent' = userAgent
uri.path = raw_url
println 'getting url: ' + uri.path
response.success = { gistResp, content ->
f = new File(dirName, fileName)
fos = new FileOutputStream(f)
fos.leftShift(content)
}
}
}
}
}
response.'403' = { resp ->
println 'Likely missing the User-Agent header, permission denied!'
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment