-
Notifications
You must be signed in to change notification settings - Fork 71
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #73 from lewisnyman/commits-per-country
Added a countries page
- Loading branch information
Showing
6 changed files
with
223 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,160 @@ | ||
#!/usr/bin/env ruby | ||
|
||
Encoding.default_external = Encoding::UTF_8 | ||
require 'erb' | ||
require 'yaml' | ||
require 'nokogiri' | ||
require 'open_uri_redirections' | ||
require 'time' | ||
require 'json' | ||
|
||
COMPANY_NOT_FOUND='not_found' | ||
COMPANY_NOT_DEFINED='not_defined' | ||
UPDATE_NONE=0 | ||
UPDATE_NOT_FOUND=1 | ||
UPDATE_ALL=2 | ||
|
||
name_variants = Hash.new(0) | ||
if File.exists? ('../data/company_infos.yml') | ||
companies_info = YAML::load_file('../data/company_infos.yml') | ||
else | ||
companies_info = Hash.new(0) | ||
end | ||
if File.exists? ('../data/country_mapping.yml') | ||
company_mapping = YAML::load_file('../data/country_mapping.yml') || Hash.new(0) | ||
else | ||
company_mapping = Hash.new(0) | ||
end | ||
update=UPDATE_NONE | ||
if ARGV.length == 1 | ||
if ARGV[0] == '--update-all' | ||
update=UPDATE_ALL | ||
else | ||
if ARGV[0] == '--update-not-found' | ||
update=UPDATE_NOT_FOUND | ||
end | ||
end | ||
end | ||
|
||
i = 1; | ||
lastOrder = -1; | ||
lastMentions = 0; | ||
file = file = File.read('../../dist/data.json') | ||
data = JSON.parse(file) | ||
contributors = data['contributors'] | ||
companies = Hash.new(0) | ||
|
||
def ensure_company(companies, companies_info, key, title, link) | ||
unless companies.key? key | ||
companies[key] = Hash.new(0) | ||
companies[key]['contributors'] = Hash.new(0) | ||
if companies_info.key? key | ||
companies[key]['title'] = companies_info[key]['title'] | ||
companies[key]['link'] = companies_info[key]['link'] | ||
else | ||
companies[key]['title'] = title | ||
companies[key]['link'] = link | ||
end | ||
end | ||
end | ||
|
||
contributors.sort_by {|k, v| v }.reverse.each do |name,mentions| | ||
if company_mapping.key? name | ||
if update == UPDATE_NONE or (update == UPDATE_NOT_FOUND and company_mapping[name] != COMPANY_NOT_FOUND) | ||
ensure_company(companies, companies_info, company_mapping[name], 'should be filled via company infos', 'should be filled via company infos') | ||
companies[company_mapping[name]]['mentions'] += mentions | ||
companies[company_mapping[name]]['contributors'][name] = mentions | ||
next | ||
end | ||
end | ||
if name_variants.key? name | ||
urlname = name_variants[name].gsub ' ', '-'; | ||
else | ||
urlname = name.gsub ' ', '-'; | ||
end | ||
url = "https://www.drupal.org/u/#{urlname}" | ||
url = URI::encode(url) | ||
begin | ||
html = open(url, :allow_redirections => :safe) | ||
doc = Nokogiri::HTML(html) | ||
rescue | ||
next | ||
end | ||
found = true | ||
doc.css('title').each do |title| | ||
if title.text == 'Page not found | Drupal.org' | ||
found = false | ||
end | ||
end | ||
unless found | ||
ensure_company(companies, companies_info, COMPANY_NOT_FOUND, 'Users not found', 'Users not found') | ||
companies[COMPANY_NOT_FOUND]['mentions'] += mentions | ||
companies[COMPANY_NOT_FOUND]['contributors'][name] = mentions | ||
end | ||
if found | ||
found = false | ||
if company_wrapper = doc.at_css('.field-name-field-country') | ||
if company_wrapper.at_css('img') | ||
company = company_wrapper.at_css('img')['alt'] | ||
else | ||
company = company_wrapper.text | ||
end | ||
if company_wrapper.at_css('a') | ||
link = company_wrapper.at_css('a') | ||
link['href'] = 'https://drupal.org' + link['href'] | ||
# If we still don't have the company name, follow the link to the page. | ||
unless company | ||
html = open(link['href'], :allow_redirections => :safe) | ||
company_page = Nokogiri::HTML(html) | ||
if company_title = company_page.at_css('#page-subtitle') | ||
company = company_title.text | ||
end | ||
end | ||
else | ||
# If there is no link, use the company name instead. | ||
link = company | ||
end | ||
company = company.strip | ||
company_key = company.downcase | ||
ensure_company(companies, companies_info, company_key, company, link.to_s) | ||
companies[company_key]['mentions'] += mentions | ||
companies[company_key]['contributors'][name] = mentions | ||
found = true | ||
end | ||
unless found | ||
ensure_company(companies, companies_info, COMPANY_NOT_DEFINED, 'Not specified', 'Not specified') | ||
companies[COMPANY_NOT_DEFINED]['mentions'] += mentions | ||
companies[COMPANY_NOT_DEFINED]['contributors'][name] = mentions | ||
end | ||
end | ||
end | ||
|
||
companies = companies.sort_by {|k, v| v['mentions'] }.reverse | ||
companies.each do |k, values| | ||
unless companies_info.key? k | ||
companies_info[k] = Hash.new(0) | ||
companies_info[k]['title'] = values['title'] | ||
companies_info[k]['link'] = values['link'] | ||
end | ||
values['contributors'].each do |name, mentions| | ||
company_mapping[name] = k | ||
end | ||
if values['contributors'].length == 0 | ||
companies_info.delete(k) | ||
end | ||
end | ||
File.open('../data/company_infos.yml', 'w') { |f| YAML.dump(companies_info, f) } | ||
File.open('../data/country_mapping.yml', 'w') { |f| YAML.dump(company_mapping, f) } | ||
|
||
sum = contributors.values.reduce(:+).to_f | ||
puts ERB.new(DATA.readlines.join, 0, '>').result | ||
|
||
time = Time.now() | ||
description = "A simple table of all contributors to Drupal 8 core" | ||
header = ERB.new(File.new("../templates/partials/header.html.erb").read).result(binding) | ||
footer = ERB.new(File.new("../templates/partials/footer.html.erb").read).result(binding) | ||
companies_template = File.open("../templates/countries.html.erb", 'r').read | ||
renderer = ERB.new(companies_template) | ||
puts output = renderer.result() | ||
|
||
__END__ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
<%= header %> | ||
<div class="main-content-wrap" class="outer"> | ||
<section class="main-content inner"> | ||
<div class="table-filter"> | ||
Total: <%= companies.length %> countries listed | ||
<ul> | ||
<li><a href="index.html">List Contributors</a></li> | ||
<li><a href="companies.html">List Companies</a></li> | ||
<li><a href="countries.html">List Countries</a></li> | ||
</ul> | ||
</div> | ||
|
||
<p class="updated">Last updated <span class="timeago" title="<%= time.iso8601(10) %>"><%= time %></span></p> | ||
<table cellpadding="4" style="border: 1px solid #000000; border-collapse: collapse;" border="1" class="companies"> | ||
<col width="5%"> | ||
<col width="50%"> | ||
<col width="15%"> | ||
<col width="15%"> | ||
<col width="15%"> | ||
<tr> | ||
<th>#</th> | ||
<th>Country</th> | ||
<th>Contributors</th> | ||
<th>Mentions</th> | ||
<th>Percent</th> | ||
</tr> | ||
<% companies.each do |name, values| %> | ||
<tr id="<%= name %>"> | ||
<td id="<%= name %>"><%= (lastMentions == values['mentions']) ? lastOrder : i %></td> | ||
<td><%= values['link'] %> <img src="images/icon_info.png" alt="Info" title="Toggle employees" class="toggle"> | ||
<table class="employees" style="display: none"> | ||
<% values['contributors'].each do |contributor, mentions| %> | ||
<tr><td><a href="https://www.drupal.org/u/<%= contributor.gsub ' ', '-' %>"><%= contributor %></a></td><td><%= mentions %></td></tr> | ||
<% end %> | ||
</table> | ||
</td> | ||
<td><%= values['contributors'].length %></td> | ||
<td><%= values['mentions'] %> (~<%= values['mentions'] / values['contributors'].length %>)</td> | ||
<td><%= ((values['mentions']/sum)*100).round(4) %>%</td> | ||
<% if lastMentions != values['mentions'] %> | ||
<% lastOrder = i %> | ||
<% end %> | ||
<% i += 1 %> | ||
<% lastMentions = values['mentions'] %> | ||
</tr> | ||
<% end %> | ||
|
||
</table> | ||
</section> | ||
</div> | ||
<%= footer %> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters