Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
[web] indent code 4 spaces. No functional changes.
I usually do not like pure formatting commits, but if I am to maintain
this code (and it seems it does, since it is broken, and nobody else
fixed it), I'd rather have sane indention than run screaming in circles.
Running screaming in circles decreases the contact time between me and
keyboard and monitor, making debugging harder.
  • Loading branch information
moritz committed Oct 20, 2012
1 parent 2e2790d commit d0861c3
Show file tree
Hide file tree
Showing 3 changed files with 201 additions and 199 deletions.
98 changes: 49 additions & 49 deletions web/lib/P6Project.pm
Expand Up @@ -13,94 +13,94 @@ use JSON;
use File::Slurp;

sub new {
my ($class, %opts) = @_;
my $self = \%opts;
$self->{output_dir} //= './';
my $ua = Mojo::UserAgent->new;
$ua->connect_timeout(10);
$ua->request_timeout(10);
$self->{ua} = $ua;
$self->{stats} = P6Project::Stats->new;
bless $self, $class;
$self->{info} = P6Project::Info->new(p6p=>$self);
$self->{html} = P6Project::HTML->new(p6p=>$self);
$self->{projects} = {};
return $self;
my ($class, %opts) = @_;
my $self = \%opts;
$self->{output_dir} //= './';
my $ua = Mojo::UserAgent->new;
$ua->connect_timeout(10);
$ua->request_timeout(10);
$self->{ua} = $ua;
$self->{stats} = P6Project::Stats->new;
bless $self, $class;
$self->{info} = P6Project::Info->new(p6p=>$self);
$self->{html} = P6Project::HTML->new(p6p=>$self);
$self->{projects} = {};
return $self;
}

sub ua {
my ($self) = @_;
return $self->{ua};
my ($self) = @_;
return $self->{ua};
}

sub stats {
my ($self) = @_;
return $self->{stats};
my ($self) = @_;
return $self->{stats};
}

sub output_dir {
my ($self) = @_;
return $self->{output_dir};
my ($self) = @_;
return $self->{output_dir};
}

sub getstore {
my ($self, $url, $filename) = @_;
my $file = $self->ua->get($url);
if (!$file->success) { return 0; }
open my $f, '>', $self->output_dir . $filename or die "Cannot open '$filename' for writing: $!";
print { $f } $file->res->body;
close $f or warn "Error while closing file '$filename': $!";
return 1;
my ($self, $url, $filename) = @_;
my $file = $self->ua->get($url);
if (!$file->success) { return 0; }
open my $f, '>', $self->output_dir . $filename or die "Cannot open '$filename' for writing: $!";
print { $f } $file->res->body;
close $f or warn "Error while closing file '$filename': $!";
return 1;
}

sub writeout {
my ($self, $content, $filename, $encode) = @_;
if ($encode) {
$content = encode_utf8($content);
}
write_file($self->output_dir . $filename, {binmode => ':encoding(UTF-8)'}, $content);
my ($self, $content, $filename, $encode) = @_;
if ($encode) {
$content = encode_utf8($content);
}
write_file($self->output_dir . $filename, {binmode => ':encoding(UTF-8)'}, $content);
}

sub info {
my ($self) = @_;
return $self->{info};
my ($self) = @_;
return $self->{info};
}

sub html {
my ($self) = @_;
return $self->{html};
my ($self) = @_;
return $self->{html};
}

sub min_popular {
my ($self) = @_;
return $self->{min_popular};
my ($self) = @_;
return $self->{min_popular};
}

sub load_projects {
my ($self, $url) = @_;
$self->{projects} = $self->info->get_projects($url);
my ($self, $url) = @_;
$self->{projects} = $self->info->get_projects($url);
}

sub projects {
my ($self) = @_;
return $self->{projects};
my ($self) = @_;
return $self->{projects};
}

sub template {
my ($self) = @_;
return $self->{template};
my ($self) = @_;
return $self->{template};
}

sub write_html {
my ($self, $filename) = @_;
my $content = $self->html->get_html($self->projects);
return $self->writeout($content, $filename, 1);
my ($self, $filename) = @_;
my $content = $self->html->get_html($self->projects);
return $self->writeout($content, $filename, 1);
}

sub write_json {
my ($self, $filename) = @_;
my $content = encode_json($self->projects);
return $self->writeout($content, $filename);
my ($self, $filename) = @_;
my $content = encode_json($self->projects);
return $self->writeout($content, $filename);
}

1;
176 changes: 89 additions & 87 deletions web/lib/P6Project/Hosts/Github.pm
Expand Up @@ -8,122 +8,124 @@ use Time::Piece;
use Time::Seconds;

sub new {
my ($class, %opts) = @_;
my $self = \%opts;
return bless $self, $class;
my ($class, %opts) = @_;
my $self = \%opts;
return bless $self, $class;
}

sub p6p {
my ($self) = @_;
return $self->{p6p};
my ($self) = @_;
return $self->{p6p};
}

sub raw_url {
'https://raw.github.com/';
'https://raw.github.com/';
}

sub api_url {
'https://api.github.com/';
'https://api.github.com/';
}

sub web_url {
'https://github.com/';
'https://github.com/';
}

sub get_api {
my ($self, $project, $call) = @_;
my $url = $self->api_url . "repos/$project->{auth}/$project->{repo_name}";
if ($call) {
$url .= $call;
}
return $self->p6p->ua->get($url)->res->json;
my ($self, $project, $call) = @_;
my $url = $self->api_url . "repos/$project->{auth}/$project->{repo_name}";
if ($call) {
$url .= $call;
}
return $self->p6p->ua->get($url)->res->json;
}

sub file_url {
my ($self, $project, $revision, $file) = @_;
my $url = $self->raw_url . $project->{auth} . '/' . $project->{repo_name} . '/' . $revision;
$url .= $file;
return $url;
my ($self, $project, $revision, $file) = @_;
my $url = $self->raw_url . $project->{auth} . '/' . $project->{repo_name} . '/' . $revision;
$url .= $file;
return $url;
}

sub blob_url {
my ($self, $project, $revision, $file) = @_;
my $url = $self->web_url . $project->{auth} . '/' . $project->{repo_name} . '/blob/' . $revision;
$url .= $file;
return $url;
my ($self, $project, $revision, $file) = @_;
my $url = $self->web_url . $project->{auth} . '/' . $project->{repo_name} . '/blob/' . $revision;
$url .= $file;
return $url;
}

sub set_project_info {
my ($self, $project, $previous) = @_;
my $ua = $self->p6p->ua;
my $stats = $self->p6p->stats;

my $url = $self->web_url . $project->{auth} . '/' . $project->{repo_name} . '/';
if (! $ua->get($url)->success ) {
$stats->error("Error for project $project->{name} : could not get $url (project probably dead)");
return 0;
}
$project->{url} = $url;

my $commits = $self->get_api($project, "/commits");
my $latest = $commits->[0];
my $updated = $latest->{commit}->{committer}->{date};
$project->{last_updated} = $updated;

my $ninety_days_ago = localtime() - 90 * ONE_DAY;
my $is_fresh = $updated && $updated ge $ninety_days_ago->ymd();
if ($previous && $previous->{last_updated} eq $updated) {
$previous->{badge_is_fresh} = $is_fresh;
$previous->{badge_panda} = $project->{badge_panda};
%$project = %$previous;
print "Not updated since last check, loading from cache\n";
return 1;
}
else {
$project->{badge_is_fresh} = $is_fresh;
}
print "Updated since last check\n";

my $repo = $self->get_api($project);
$project->{description} //= $repo->{description};

my $tree = $self->get_api($project, "/git/trees/$latest->{sha}?recursive=1");
my %files = map { $_->{path}, $_->{type} } @{$tree->{tree}};

## Get the logo if one exists.
my $logo_file = 'logotype/logo_32x32.png';
if ($files{logotype} && $files{$logo_file}) {
my $logo_name = $project->{name};
$logo_name =~ s/\W+/_/;
my $logo_store = "/logos/$logo_name.png";
## TODO: check filesize, and skip download if filesize is the same.
my $logo_url = $self->file_url($project, $latest->{sha}, '/'.$logo_file);
if ($self->p6p->getstore($logo_url, $logo_store)) {
$project->{logo} = './'.$logo_store;
$project->{logo} =~ s{//}{/}g;
my ($self, $project, $previous) = @_;
my $ua = $self->p6p->ua;
my $stats = $self->p6p->stats;

my $url = $self->web_url . $project->{auth} . '/' . $project->{repo_name} . '/';
if (! $ua->get($url)->success ) {
$stats->error("Error for project $project->{name} : could not get $url (project probably dead)");
return 0;
}
$project->{url} = $url;

my $commits = $self->get_api($project, "/commits");
my $latest = $commits->[0];
my $updated = $latest->{commit}->{committer}->{date};
$project->{last_updated} = $updated;

my $ninety_days_ago = localtime() - 90 * ONE_DAY;
my $is_fresh = $updated && $updated ge $ninety_days_ago->ymd();
if ($previous && $previous->{last_updated} eq $updated) {
$previous->{badge_is_fresh} = $is_fresh;
$previous->{badge_panda} = $project->{badge_panda};
%$project = %$previous;
print "Not updated since last check, loading from cache\n";
return 1;
}
}

## And now for some badges.

$project->{badge_has_tests} = $files{t} || $files{test} || $files{tests};

my @readmes = grep exists $files{$_}, qw/
README
README.pod
README.md
README.mkdn
README.mkd
README.markdown
/;

$project->{badge_has_readme} = scalar(@readmes)
else {
$project->{badge_is_fresh} = $is_fresh;
}
print "Updated since last check\n";

my $repo = $self->get_api($project);
$project->{description} //= $repo->{description};

my $tree = $self->get_api($project, "/git/trees/$latest->{sha}?recursive=1");
my %files = map { $_->{path}, $_->{type} } @{$tree->{tree}};

## Get the logo if one exists.
my $logo_file = 'logotype/logo_32x32.png';
if ($files{logotype} && $files{$logo_file}) {
my $logo_name = $project->{name};
$logo_name =~ s/\W+/_/;
my $logo_store = "/logos/$logo_name.png";
## TODO: check filesize, and skip download if filesize is the same.
my $logo_url = $self->file_url($project, $latest->{sha}, '/'.$logo_file);
if ($self->p6p->getstore($logo_url, $logo_store)) {
$project->{logo} = './'.$logo_store;
$project->{logo} =~ s{//}{/}g;
}
}

## And now for some badges.

$project->{badge_has_tests} = $files{t} || $files{test} || $files{tests};

my @readmes = grep exists $files{$_}, qw/
README
README.pod
README.md
README.mkdn
README.mkd
README.markdown
/;

$project->{badge_has_readme} = scalar(@readmes)
? $self->blob_url($project, $latest->{sha}, "/$readmes[0]")
: undef;

$project->{badge_is_popular} = $repo->{watchers} && $repo->{watchers} >= $self->p6p->min_popular;
$project->{badge_is_popular} = $repo->{watchers} && $repo->{watchers} >= $self->p6p->min_popular;

return 1;
return 1;
}

1;

# vim: set ts=4 sw=4 expantab

0 comments on commit d0861c3

Please sign in to comment.