LoginSignup
8
8

More than 5 years have passed since last update.

nodeで特定の人のGithubContributionsデータ(草カレンダー)を取得する。

Posted at

Githubアカウントの草データ取得

githubの誰かのアカウント行くと丁寧に生えてあるこの草のカレンダーですね。

githubcontributions.png

この草データを取得したいと思います!

これは恥ずかしながら自分のgithubの草なんですが、気にしないでください。
githubの草データはこのURLを叩けば帰ってきます。

このURLを叩いて帰ってくるのが

てなわけで、このURLを叩いてデータを取得しましょう。

kusa.js
var https = require('https')
,   parseString = require('xml2js').parseString
,   async = require('async');

// githubの草のオブジェクト
function contribution(x, y, color, count, date){
  this.x = x,
  this.y = y,
  this.color = color,
  this.count = count,
  this.date = date;
}

/* usernameのContributionsをの色と位置取得して配列にプッシュ */
var getGitHubData = function(name, callback) {
    var url = 'https://github.com/users/' + name + '/contributions';
    var width, height, contributions = [];

    https.get(url, function(res) {
        var body = '';
        res.on('data', function(chunk) {
            body += chunk;
        });

        res.on('end', function() {
            parseString(body, function(err, result) {
                // console.log(result);
                width = result.svg.$.width,
                height = result.svg.$.height,
                data = result.svg.g[0].g;

                // 並行実行
                async.forEach(data, function(datum, callback) {
                   // datum.$.transform は transform(???, 0)の形式なのでmatchで???を抽出
                    x = datum.$.transform.match(/\d+/)[0];
                    datum.rect.forEach( function(rect) {
                        y = rect.$.y;
                        color = rect.$.fill;
                        count = rect.$['data-count'];
                        date = rect.$['data-date'];

                        kusa = new contribution(x, y, color, count, date);
                        contributions.push(kusa);
                    });
                    callback();
                }, function(err) {
                    if( err ) console.log(err);
                    console.log(contributions);
                    callback(contributions);
                });
            });
        });
    }).on('error', function(e) {
        console.log(e);
    });

};

一気に書いちゃいましたが、あるnameの人のgithubの草を取得するために、URLを叩いて、帰ってきたxml形式のデータを'xml2js'でJSON形式に変換。そのデータをcontributionsに格納していく。こうして格納し終えたcontributionsのデータはこんな草オブジェクトの配列になってるはずなので、後はこれをcanvasに描画するなり、なんなりとしてやってください。

取得したcontributionsのオブジェクトデータ配列はこんな感じで帰ってくるはず。

contributions.json
 [{ x: '676',
    y: '26',
    count: '4',
    color: '#44a340',
    date: '2015-11-10' },
  { x: '676',
    y: '39',
    count: '1',
    color: '#d6e685',
    date: '2015-11-11' },
  { x: '676',
    y: '52',
    count: '1',
    color: '#d6e685',
    date: '2015-11-12' },
  { x: '676',
    y: '65',
    count: '3',
    color: '#8cc665',
    date: '2015-11-13' },
  { x: '676',
    y: '78',
    count: '4',
    color: '#44a340',
    date: '2015-11-14' },
  { x: '689',
    y: '0',
    count: '11',
    color: '#1e6823',
    date: '2015-11-15' },
  { x: '689',
    y: '13',
    count: '3',
    color: '#8cc665',
    date: '2015-11-16' } ]

自分のメモ用ということで。では。

8
8
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
8