Skip to content

Instantly share code, notes, and snippets.

@DRKV333
Forked from pmdgithub/grab_avatars_for_git_users.pl
Last active November 4, 2020 18:53
Show Gist options
  • Save DRKV333/00ccbe5f36fdf06293c5926c2dc01bac to your computer and use it in GitHub Desktop.
Save DRKV333/00ccbe5f36fdf06293c5926c2dc01bac to your computer and use it in GitHub Desktop.
Grab avatars for github users, use for: gource --user-image-dir .git/avatar/
#!/usr/bin/perl
#fetch Gravatars
use strict;
use warnings;
use LWP::Simple;
use LWP::Protocol::https;
my $size = 90;
my $output_dir = '.git/avatar';
die("no .git/ directory found in current path\n") unless -d '.git';
mkdir($output_dir) unless -d $output_dir;
open(GITLOG, q/git log --pretty=format:"%ae|%an" |/) or die("failed to read git-log: $!\n");
my %processed_authors;
while(<GITLOG>) {
chomp;
my($email, $author) = split(/\|/, $_);
next if $processed_authors{$author}++;
my $author_image_file = $output_dir . '/' . $author . '.png';
#skip images we have
next if -e $author_image_file;
#try and fetch image
if ($email =~ m/^(?:(?<userId>\d+)\+)?(?<userName>[a-zA-Z\d-]{1,39})\@users.noreply.github.com$/) {
$author = $+{userName};
}
my $url = "https://github.com/".lc($author).".png?size=".$size;
warn "fetching image for '$author' $email ($url)...\n";
my $rc = getstore($url, $author_image_file);
sleep(1);
if($rc != 200) {
warn $rc;
unlink($author_image_file);
next;
}
}
close GITLOG;
@DRKV333
Copy link
Author

DRKV333 commented Nov 4, 2020

This currently only works, if the user didn't set a full name, or if they have a users.noreply.github.com email address. Otherwise I don't think there's a way to get the GitHub username just from git log.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment