Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
retrieves my starred repos from Github v3 API and stores them in HSQL
@GrabResolver(name='gstorm', root='http://dl.bintray.com/kdabir/maven')
@GrabConfig(systemClassLoader = true) @Grab('gstorm:gstorm:0.6')
import gstorm.*
import groovy.json.*
@Grab(group='org.codehaus.groovy.modules.http-builder', module='http-builder', version='0.7.1')
@GrabExclude(group='xerces', module='xercesImpl')
import static groovyx.net.http.ContentType.TEXT
@Grab(group='commons-io', module='commons-io', version='2.3')
import org.apache.commons.io.IOUtils;
class Repo {
String starred_at, name, full_name, html_url, language, default_branch
int stargazers_count
}
def g = new Gstorm("/Volumes/RamDiskCache/starred")
g.stormify(Repo)
def repo_fields = ["name" , "full_name" , "html_url" , "language" , "stargazers_count" , "default_branch"]
def user = args[0], page = 1, json = [], token = args[1]
println "fetching data from github account of ${user}"
while (json = new groovy.json.JsonSlurper().parseText(IOUtils.toString(new groovyx.net.http.HTTPBuilder("https://api.github.com/user/starred?page=${page++}&per_page=100")
.get(contentType: TEXT, headers: ["Authorization": "token ${token}", "Accept": "application/vnd.github.v3.star+json", "User-Agent": "Mozilla/5.0"])))) {
json.collect { println it.repo.name; r = new Repo(it.repo.subMap(repo_fields) + it.subMap(["starred_at"])); println "${r.name} ${r.starred_at}"; r.save() }
}
println ""
println "Your Starred Repo's Summary"
println "="*80
// some methods are available on the class
println "Total starred Repos: " +
Repo.count
println "Number of Repos with more than 100 stars:" +
Repo.count("stargazers_count > 100")
// little more advanced stuff, still do'able with gstorm
println "20 top starred java repos :" +
Repo.where("language like 'Java' order by stargazers_count desc limit 20").collectEntries { [it.name, it.stargazers_count]}
println "various default branch names that peopl use:" +
g.sql.rows("select distinct(default_branch) from repo").collect{it.default_branch}.join(", ")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment