codereposのコミット数ランキングページを作る

| 0 Comments | 0 TrackBacks | このエントリーをはてなブックマークに追加 このエントリーのはてなブックマーク件数

先日tokuhiromさんのブログで、ohlohというサービスを知りました。

凄いですね、1位tokuhiromさん、2位cho45さん両方とも1000commits超えています。さて今日はこのohlohという開発者向けサイトが提供するAPIを使って、codereposコミット数ランキングを作ってみたいと思います。

ohlohのAPIドキュメントによると、ContributorFactというAPIでtokuhiromさんの記事にある様なランキングが取得出来る事が分かりました。

APIを利用する為にはAPIキーが必要になりますので、アカウントを取得後にAPI Keysのページでキーを取得、リクエストクエリの後にapi_keyというパラメータで送信すればAPIが実行出来ます。単純にDumpしても面白くないので、今日はXMLで結果取得したデータをJSONで出力し、Ajaxで扱えるマッシュアップサイトぽい物を作ってみます。

perlのコードは単純に

#!/usr/bin/perl

use strict;
use warnings;
use LWP::Simple;
use XML::Simple;
use JSON::Syck;
use CGI;

my $api_key = "your-api-key";
my $callback = "";

my $q = new CGI;
$callback = $q->param("callback") if defined $q->param('callback');
my $content = get("http://www.ohloh.net/projects/8359/contributors.xml?api_key=$api_key");
my $parser = XML::Simple->new();
my $xml = $parser->XMLin($content);
my $json = JSON::Syck::Dump($xml);

print $q->header(
  -type    => 'text/javascript',
  -charset => 'utf-8'
  );
print "$callback($json)";

という、XMLinからJSON::Syckへの流し込みのコードになります。

これをCGIとして実行し、JSONを生成します。またクライアント側は

<script type="text/javascript" src="jquery-latest.js"></script>
<script type="text/javascript"><!--
$(function() {
	$('#coderepos-committers').html('');
	$.getJSON("http://your.example.com/coderepos-ranking.cgi?callback=?", function(data) {
		$.each(data.result.contributor_fact, function(index, item) {
			$('<div>').html(
				'<b>' + item.contributor_name + '</b><br />' +
				'<blockquote>' +
				'main language: ' + item.primary_language_nice_name + '<br />' +
				'commits: ' + item.commits + '<br />' +
				'</blockquote>').appendTo('#coderepos-committers');
		});
	});
})();
--></script>
<div id="coderepos-committers"></div>

とすれば、以下の様な画面が表示されます。

http://perl-mongers.org/2008/06/20/coderepos-ranking/coderepos-ranking.png

XMLinからJSON::Syck::Dumpの流れが便利すぎますね。:-)

このランキングを作るロジック自体、手数の多そうな処理になりますからこのAPI便利かもしれません。他のプロジェクトでこの様なランキングページを作ってみても面白いかもしれませんね。

No TrackBacks

TrackBack URL: http://perl-mongers.org/MT/mt-tb.cgi/64

Leave a comment

About this Entry

This page contains a single entry by mattn.myopenid.com published on June 23, 2008 3:22 PM.

S3に写真を一括アップロード was the previous entry in this blog.

実用! PerlでコマンドラインからTwitter投稿 is the next entry in this blog.

Find recent content on the main index or look in the archives to find all content.

Categories

Pages

Creative Commons License
This blog is licensed under a Creative Commons License.
Powered by Movable Type 4.21-en