PageRenderTime 56ms CodeModel.GetById 30ms RepoModel.GetById 1ms app.codeStats 0ms

/lib/otr.rb

https://bitbucket.org/stephenmcd/otr
Ruby | 60 lines | 52 code | 7 blank | 1 comment | 5 complexity | 800d04ba102a96edc105dc0599f24cd3 MD5 | raw file
Possible License(s): BSD-2-Clause
  1. #!/usr/bin/env ruby
  2. require "rubygems"
  3. require "json"
  4. require "rest_client"
  5. module OTR
  6. VERSION = "0.1.3"
  7. USER_AGENT = "otr.jupo.org"
  8. def self.get_json(url)
  9. JSON.parse(RestClient.get(url, :user_agent => USER_AGENT))
  10. end
  11. def self.get_bb(username)
  12. repos = {}
  13. url = "https://api.bitbucket.org/1.0/users/#{username}/"
  14. self.get_json(url)["repositories"].map { |repo|
  15. Thread.new repo do |repo|
  16. name = repo["slug"]
  17. url = "https://bitbucket.org/#{username}/#{name}/descendants"
  18. html = RestClient.get(url).split("<span class=\"value\">")
  19. repos[name] = {
  20. "name" => name,
  21. "watchers" => html[4].to_i,
  22. "forks" => html[3].to_i,
  23. "fork" => repo["is_fork"],
  24. "urls" => ["https://bitbucket.org/#{username}/#{name}"]
  25. }
  26. end
  27. }.each { |thread| thread.join }
  28. repos
  29. end
  30. def self.get_gh(username)
  31. page = 0
  32. all_repos = []
  33. while true
  34. page += 1
  35. url = "https://api.github.com/users/#{username}/repos?page=#{page}"
  36. repos = self.get_json(url)
  37. break if repos.count == 0
  38. all_repos += repos
  39. end
  40. all_repos
  41. end
  42. def self.get(options)
  43. repos = self.get_bb(options[:bitbucket_username] || options[:username])
  44. self.get_gh(options[:github_username] || options[:username]).each do |repo|
  45. name = repo["name"]
  46. repos[name] ||= {"forks" => 0, "watchers" => 0, "urls" => []}
  47. repos[name]["urls"] << repo["url"]
  48. %w[watchers forks].each { |k| repos[name][k] += repo[k] }
  49. %w[fork name].each { |k| repos[name][k] ||= repo[k] }
  50. end
  51. repos.values
  52. end
  53. end