もう一年もおわりですね。
ってことでGithubへのContribute数を取得してみます。
Contribute取得方法
GithubにはAPIが用意されていてcommitの詳細を取得することができます。
以前このAPIは使用して1日のcommit数を集計してツイートするアプリケーションを作成しました。
詳しくはこちらをどうぞ。
ですが、この方法だとprivateリポジトリへのcommitが取れないので今回はスクレイピングします(スクレイピングは自己責任でお願いします。
Contribute数を数えているクラスは下記になります。
gemはnokogiri
を使用しています。
require 'nokogiri'
require 'open-uri'
class GithubContributeCount
def create_tweet_text
count = count_contribute
"通算で" + count + "回Githubへコミットしています。https://github.com/yshogo Github contribute count app author shogo yamda"
end
private
def count_contribute
html = Nokogiri::HTML open 'https://github.com/yshogo'
text = html.xpath('//h2[@class = "f4 text-normal mb-2"]').first.inner_text
text.delete! 'in the last year'
text.delete! 'cobuo'
text.gsub(/[\r\n]/,"")
end
end
このクラスを使ってContribute数をTwitterへ投稿するアプリケーションを作成しました。
通算で698回Githubへコミットしています。https://github.com/yshogo Github contribute count app author shogo yamda
— @yshogo87
詳しくは下記のブログに書いたのでご覧ください。
Rubyについてもっと勉強したいな〜