8
6

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

x-rayを使ってgithubの草の情報を取得する

Posted at

 この投稿ではnode.jsのスクレイピングライブラリx-rayを用いて、Githubの各ユーザーの草(Contributions)の情報を取得する方法とコードを示します。

 投稿時(2016年4月10日)では、

https://github.com/users/<UserName>/contributions

 にアクセスすると、ユーザーごとのContributionsの様子を表す、次のようなSVGが取得できます(この仕様は今後変わる可能性があります)。

 次の画像は私のGithubのContributionsです。

Screen Shot 2016-04-10 at 9.49.33 PM.png

 さて、このSVGをnode.js向けスクレイピングライブラリx-rayを用いて、取得・パースするコードを示します。

 コードは次の通りです。

X-rayのContributionsを取得する
'use strict'
var Xray = require('x-ray');
var x = Xray();

var userName = 'RyotaMurohoshi';
var url = `https://github.com/users/${userName}/contributions`;
x(url, 'rect', [ { count: '@data-count', date: '@data-date' }])((error, result) => {
    if (error) {
        console.log(error);
    } else {
        console.log(result);
    }
}); 

 実行結果例を下記に示します。

実行結果例
[ { count: '0', date: '2015-04-08' },
  { count: '0', date: '2015-04-09' },
  { count: '0', date: '2015-04-10' },
  { count: '0', date: '2015-04-11' },
  { count: '0', date: '2015-04-12' },
  { count: '0', date: '2015-04-13' },
  { count: '0', date: '2015-04-14' },
〜中略〜
  { count: '8', date: '2016-03-23' },
  { count: '4', date: '2016-03-24' },
  { count: '3', date: '2016-03-25' },
  { count: '2', date: '2016-03-26' },
  { count: '2', date: '2016-03-27' },
  { count: '3', date: '2016-03-28' },
  { count: '4', date: '2016-03-29' },
  { count: '1', date: '2016-03-30' },
  { count: '7', date: '2016-03-31' },
  { count: '1', date: '2016-04-01' },
  { count: '1', date: '2016-04-02' },
  { count: '3', date: '2016-04-03' },
  { count: '3', date: '2016-04-04' },
  { count: '1', date: '2016-04-05' },
  { count: '6', date: '2016-04-06' },
  { count: '3', date: '2016-04-07' },
  { count: '3', date: '2016-04-08' },
  { count: '1', date: '2016-04-09' } ]

 x-rayこのように用いることで、ユーザーごとのContributionsの情報を簡潔に取得することが可能です。

8
6
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
8
6

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?