Skip to content

Commit

Permalink
Add --github|gh argument to pull public contributions data out of github
Browse files Browse the repository at this point in the history
( as suggested by dgryski here #15 )
  • Loading branch information
karthik committed Sep 25, 2013
1 parent a97b154 commit efe40e0
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 34 deletions.
12 changes: 6 additions & 6 deletions Makefile.PL
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ use warnings;
use ExtUtils::MakeMaker;

WriteMakefile(
NAME => 'git-cal',
($ExtUtils::MakeMaker::VERSION >= 6.3002 ? ('LICENSE' => 'mit') : ()),
EXE_FILES => [ 'git-cal', ],
PL_FILES => { },
PREREQ_PM => { },
dist => { COMPRESS => 'gzip -9f', SUFFIX => 'gz', },
NAME => 'git-cal',
( $ExtUtils::MakeMaker::VERSION >= 6.3002 ? ( 'LICENSE' => 'mit' ) : () ),
EXE_FILES => [ 'git-cal', ],
PL_FILES => {},
PREREQ_PM => { 'LWP::Simple' => 0, 'JSON::PP' => 0},
dist => { COMPRESS => 'gzip -9f', SUFFIX => 'gz', },
);
86 changes: 58 additions & 28 deletions git-cal
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,21 @@ use strict;
use utf8;
use Getopt::Long;
use Pod::Usage;
use Time::Local;
use LWP::Simple;
use JSON::PP qw ( decode_json );
use Data::Dumper;

binmode(STDOUT, ":utf8");
#command line options
my ( $help, $period, $use_ascii, $author, $filepath );
my ( $help, $period, $use_ascii, $author, $filepath, $github_username );

GetOptions(
'help|?' => \$help,
'period|p=n' => \$period,
'ascii' => \$use_ascii,
'author=s' => \$author,
'github|gh=s' => \$github_username
) or pod2usage(2);
pod2usage(1) if $help;

Expand All @@ -40,38 +44,59 @@ my ( $cur_start, $max_start, $max_end, $cur_weekdays_start, $max_weekdays_start,

sub process {
init_cal_stuff();
my $extra_args = "";
$extra_args = " --author=\"$author\"" if $author;
if ($filepath) {
if ( -e $filepath ) {
$extra_args .= " -- " . $filepath;
if ($github_username) {
process_github_contributions();
}
else {
my $extra_args = "";
$extra_args = " --author=\"$author\"" if $author;
if ($filepath) {
if ( -e $filepath ) {
$extra_args .= " -- " . $filepath;
}
else {
print "fatal: $filepath do not exists\n";
exit(2);
}
}
else {
print "fatal: $filepath do not exists\n";
my $git_command = "git log --no-merges --pretty=format:\"%at\" --since=\"13 months\"" . $extra_args; #commits might not be in strict time order, check some past too
my $epochs = qx/$git_command/;
if ($?) {
print "fatal: git-cal failed to get the git log\n";
exit(2);
}
}
my $git_command = "git log --no-merges --pretty=format:\"%at\" --since=\"13 months\"" . $extra_args; #commits might not be in strict time order, check some past too
my $epochs = qx/$git_command/;
if ($?) {
print "fatal: git-cal failed to get the git log\n";
exit(2);
}
my @epochs = split /\n/, $epochs;
if (! @epochs) {
print "git-cal: got empty log, nothing to do\n";
exit(1);
}
my $status;
foreach (@epochs) {
$status = add_epoch($_);
last if !$status;
my @epochs = split /\n/, $epochs;
if ( !@epochs ) {
print "git-cal: got empty log, nothing to do\n";
exit(1);
}
my $status;
foreach (@epochs) {
$status = add_epoch($_);
last if !$status;
}
}
compute_stats();
print_grid();
}


sub process_github_contributions {
my $content = get("https://github.com/users/" . $github_username . "/contributions_calendar_data");
my $result = decode_json ( $content ) if $content;
if (!$content || !$result) {
print "fatal: could not get github contributions data of " . $github_username . "\n";
exit(1);
}
foreach my $contrib (@$result) {
next if !$contrib->[1];
my ( $year, $mon, $mday ) = split "/", $contrib->[0];
my $epoch = timelocal( 0, 0, 0, $mday, $mon-1, $year );
add_epoch( $epoch, $contrib->[1] );
}
}


sub init_cal_stuff {
my ( $wday, $yday, $month, $year ) = ( localtime(time) )[ 6, 7, 4, 5 ];
$cur_year = $year;
Expand Down Expand Up @@ -125,7 +150,7 @@ sub init_cal_stuff {


sub add_epoch {
my $epoch = shift;
my ($epoch, $count) = @_;
if ( $epoch > $max_epoch || $epoch < $min_epoch ) {
return 1;
}
Expand All @@ -139,15 +164,16 @@ sub add_epoch {
$pos = ( $jan1 - ( $total - $yday ) );
}
return 0 if $pos < 0; #just in case
add_to_grid( $pos, $epoch );
add_to_grid( $pos, $epoch, $count );
return 1;
}

sub add_to_grid {
my ( $pos, $epoch ) = @_;
my ( $pos, $epoch, $count ) = @_;
$count ||= 1;
my $r = int $pos / 7;
my $c = $pos % 7;
$grid[$r][$c]->{commits}++;
$grid[$r][$c]->{commits}+=$count;
$grid[$r][$c]->{epoch} = $epoch;
$max_commits = $grid[$r][$c]->{commits} if $grid[$r][$c]->{commits} > $max_commits;
}
Expand Down Expand Up @@ -361,6 +387,10 @@ Shows the last n months (and the current month)
=back
=item --github|gh=<github_username>
Show public contributions data from github
=item --help|?
Print this message.
Expand Down

0 comments on commit efe40e0

Please sign in to comment.