Skip to content

Commit

Permalink
use only Octokit API instead of web scraping
Browse files Browse the repository at this point in the history
  • Loading branch information
syxanash committed Jul 8, 2020
1 parent cfb6367 commit f66882b
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 53 deletions.
8 changes: 3 additions & 5 deletions Gemfile
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
source "https://rubygems.org"
ruby "2.1.2"
ruby "2.7.0"

gem 'octokit', '~> 3.2.0'
gem 'sinatra', '~> 1.4.8'
gem 'nokogiri', '~> 1.8.2'
gem 'rest-client'
gem 'octokit'
gem 'sinatra'
26 changes: 3 additions & 23 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2,29 +2,14 @@ GEM
remote: https://rubygems.org/
specs:
addressable (2.3.8)
domain_name (0.5.20170404)
unf (>= 0.0.5, < 1.0.0)
faraday (0.9.2)
multipart-post (>= 1.2, < 3)
http-cookie (1.0.3)
domain_name (~> 0.5)
mime-types (3.1)
mime-types-data (~> 3.2015)
mime-types-data (3.2016.0521)
mini_portile2 (2.3.0)
multipart-post (2.0.0)
netrc (0.11.0)
nokogiri (1.8.2)
mini_portile2 (~> 2.3.0)
octokit (3.2.0)
sawyer (~> 0.5.3)
rack (1.6.8)
rack-protection (1.5.3)
rack
rest-client (2.0.2)
http-cookie (>= 1.0.2, < 2.0)
mime-types (>= 1.16, < 4.0)
netrc (~> 0.8)
sawyer (0.5.5)
addressable (~> 2.3.5)
faraday (~> 0.8, < 0.10)
Expand All @@ -33,21 +18,16 @@ GEM
rack-protection (~> 1.4)
tilt (>= 1.3, < 3)
tilt (2.0.8)
unf (0.1.4)
unf_ext
unf_ext (0.0.7.4)

PLATFORMS
ruby

DEPENDENCIES
nokogiri (~> 1.8.2)
octokit (~> 3.2.0)
rest-client
sinatra (~> 1.4.8)
octokit
sinatra

RUBY VERSION
ruby 2.1.2p95
ruby 2.7.0p0

BUNDLED WITH
1.16.1
1 change: 0 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,6 @@ Here listed a few things I used for developing this project.

## Bugs & known issues

* Fetching the number of commits for a repo has a few problems, I need to get the number of commits without wasting too many [requests per hour](https://developer.github.com/v3/rate_limit/).
* __MCP__ apparently is still alive...

## Easter eggs
Expand Down
4 changes: 2 additions & 2 deletions app.rb
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@
erb :error_rate_limit
end

before %r{^\/errors\/(input|reponame|ragequit)} do
before %r{\/errors\/(input|reponame|ragequit)} do
unless achieved?(@achievement_list[:zuse])
@achievements.push(@achievement_list[:zuse])
save(@achievements)
Expand Down Expand Up @@ -125,7 +125,7 @@

player[:score] = github.score
player[:avatar], player[:disk] = github.generate_avatar
player[:branches], player[:commits] = github.additional_disk_info
player[:branches], player[:contribs] = github.additional_disk_info
rescue RepoNotFoundError
redirect '/errors/reponame'
end
Expand Down
55 changes: 34 additions & 21 deletions lib/Github.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
require 'octokit'
require 'nokogiri'
require 'rest-client'

class RepoNotFoundError < StandardError; end
class RateLimitError < StandardError; end
Expand Down Expand Up @@ -50,39 +48,54 @@ def set_repository(owner, repo)
raise RepoNotFoundError, 'repository does not exist!'
end

# unfortunately due to octokit limitations some info are obtained
# directly from repository web page thus parsing the HTML
# to create a nokogiri document
html_content = RestClient.get("https://github.com/#{@my_repository}")
nokogiri_doc = Nokogiri::HTML::Document.parse(html_content)

# count the number of commits and releases
stats = []
nokogiri_doc.css('.text-emphasized').each do |num|
stats.push(num.text.strip.gsub(',', ''))
end

# build repository information
@repo_info = {
size: @client.repository(@my_repository).size,
forks: @client.repository(@my_repository).forks_count,
stars: @client.repository(@my_repository).stargazers_count,
date: @client.repository(@my_repository).created_at,
commits: stats[0],
branches: stats[1],
releases: stats[2],
contribs: stats[3]
branches: calc_total(@my_repository, 'branches'),
releases: calc_total(@my_repository, 'releases'),
contribs: calc_total(@my_repository, 'contribs')
}
end

# Public: get branches and number of commits for a repository
# Thank you snowe wherever you are!
# https://stackoverflow.com/a/43374518
def calc_total(repo, repo_info)
begin
number_of_items_in_first_page = @client.send(repo_info, repo).size
repo_sum = 0
if number_of_items_in_first_page >= 100
links = @client.last_response.rels

unless links.empty?
last_page_url = links[:last].href

/.*page=(?<page_num>\d+)/ =~ last_page_url
repo_sum += (page_num.to_i - 1) * 100 # we add the last page manually
repo_sum += links[:last].get.data.size
end
else
repo_sum += number_of_items_in_first_page
end
rescue Octokit::Forbidden
# used in rare cases like torvalds/linux

repo_sum = '∞'
end

repo_sum
end

# Public: get branches and number of contributors for a repository
#
# Returns number of branches
# Returns number of commits
# Returns number of contributors
def additional_disk_info
raise "repository hasn't been set!" unless @my_repository

return @repo_info[:branches], @repo_info[:commits]
return @repo_info[:branches], @repo_info[:contribs]
end


Expand Down
2 changes: 1 addition & 1 deletion views/fight.erb
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ end

$('#disk').click(function(){
noty({
text : '<div id="info_box">program: <a href="http://github.com/<%= winner[:name] %>/<%= winner[:repo] %>" target="_blank"><%= winner[:repo] %></a> <br /> user: <%= winner[:name] %> <br /> branches: <%= winner[:branches] %> <br /> commits: <a href="http://github.com/<%= winner[:name] %>/<%= winner[:repo] %>/commits/master" target="_blank"><%= winner[:commits] %></a> <br /> character: <a href="http://tron.wikia.com/wiki/Special:Search?search=<%= character_name %>&fulltext=Search" target="_blank"><%= character_name %></a> <br /> score: <span class="blinky"><%= winner[:score] %></span></div>',
text : '<div id="info_box">program: <a href="http://github.com/<%= winner[:name] %>/<%= winner[:repo] %>" target="_blank"><%= winner[:repo] %></a> <br /> user: <%= winner[:name] %> <br /> branches: <%= winner[:branches] %> <br /> contributors: <a href="http://github.com/<%= winner[:name] %>/<%= winner[:repo] %>/commits/master" target="_blank"><%= winner[:contribs] %></a> <br /> character: <a href="http://tron.wikia.com/wiki/Special:Search?search=<%= character_name %>&fulltext=Search" target="_blank"><%= character_name %></a> <br /> score: <span class="blinky"><%= winner[:score] %></span></div>',
layout : 'topRight',
type : 'information',
theme : 'custom',
Expand Down

0 comments on commit f66882b

Please sign in to comment.