|
@GrabResolver(name='gstorm', root='http://dl.bintray.com/kdabir/maven') |
|
@GrabConfig(systemClassLoader = true) @Grab('gstorm:gstorm:0.6') |
|
import gstorm.* |
|
import groovy.json.* |
|
|
|
def g = new Gstorm() // could give a file path here if we want the storage to be persistent |
|
|
|
// define what you want to collect / store |
|
class Repo { |
|
String name, full_name, html_url, language, default_branch |
|
int stargazers_count, watchers_count, forks_count, open_issues_count |
|
} |
|
|
|
g.stormify(Repo) // stormifying creates the table and adds crud methods to class |
|
|
|
// get data using github api |
|
def user = ((args.length)? args.first() : "kdabir"), page = 1, json = [] |
|
def interesting_fields = ["name" , "full_name" , "html_url" , "language" , "stargazers_count" , "watchers_count" , "forks_count" , "open_issues_count" , "default_branch"] |
|
|
|
println "fetching data from github account of ${user}" |
|
while (json = new JsonSlurper().parse(new URL("https://api.github.com/users/${user}/starred?page=${page++}&per_page=80"))){ |
|
json.collect { print "."; new Repo(it.subMap(interesting_fields)).save() } // its just as easy to persist this in db |
|
println "" |
|
} |
|
|
|
// and you are done, use sql queries to do fancy reporting |
|
|
|
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") |
|
|
|
println "Number of Repos with more than 100 forks:" + |
|
Repo.count("forks_count > 100") |
|
|
|
// little more advanced stuff, still do'able with gstorm |
|
println "5 top starred groovy repos :" + |
|
Repo.where("language like 'Groovy' order by stargazers_count desc limit 5").collectEntries { [it.name, it.stargazers_count]} |
|
|
|
println "5 top forked groovy repos : " + |
|
Repo.where("language like 'Groovy' order by forks_count desc limit 5").collectEntries { [it.name, it.forks_count]} |
|
|
|
// for some operations, you will have to resort to using sql, and gstorm doesn't come in your way |
|
println "Your starred repos by Languages : " + |
|
g.sql.rows("select language, count(language) as repos_count from repo group by language order by repos_count desc ").collectEntries { [it.language, it.repos_count]} |
|
|
|
println "various default branch names that peopl use:" + |
|
g.sql.rows("select distinct(default_branch) from repo").collect {it.default_branch}. join(", ") |