From 7a5f2233913b1c29a937cb7cd9e466209cd6e847 Mon Sep 17 00:00:00 2001 From: franck cuny Date: Tue, 9 Feb 2010 13:19:00 +0100 Subject: rename to repo --- lib/githubexplorer.pm | 5 +-- lib/githubexplorer/Profile.pm | 1 + lib/githubexplorer/Repositorie.pm | 59 --------------------------- lib/githubexplorer/Repository.pm | 85 +++++++++++++++++++++++++++++++++++++++ 4 files changed, 88 insertions(+), 62 deletions(-) delete mode 100644 lib/githubexplorer/Repositorie.pm create mode 100644 lib/githubexplorer/Repository.pm (limited to 'lib') diff --git a/lib/githubexplorer.pm b/lib/githubexplorer.pm index a3d27c9..67e9e25 100644 --- a/lib/githubexplorer.pm +++ b/lib/githubexplorer.pm @@ -1,13 +1,12 @@ package githubexplorer; use 5.010; -use lib ('/home/franck/code/git/net-github/lib'); use YAML::Syck; use Moose; use githubexplorer::Schema; use githubexplorer::Gexf; use IO::All; -with qw/githubexplorer::Profile githubexplorer::Repositorie/; +with qw/githubexplorer::Profile githubexplorer::Repository/; has seed => ( isa => 'ArrayRef', @@ -61,7 +60,7 @@ sub harvest_repo { $self->_connect unless $self->has_schema; my $profiles = $self->schema->resultset('Profiles')->search(); while ( my $p = $profiles->next ) { - $self->fetch_repo($p); + $self->fetch_repositories($p); } } diff --git a/lib/githubexplorer/Profile.pm b/lib/githubexplorer/Profile.pm index 1af64cc..bb3d462 100644 --- a/lib/githubexplorer/Profile.pm +++ b/lib/githubexplorer/Profile.pm @@ -126,6 +126,7 @@ sub _create_profile { try { $self->schema->txn_do( sub { + $profile_rs = $self->schema->resultset('Profiles')->create($profile); } diff --git a/lib/githubexplorer/Repositorie.pm b/lib/githubexplorer/Repositorie.pm deleted file mode 100644 index bfb8076..0000000 --- a/lib/githubexplorer/Repositorie.pm +++ /dev/null @@ -1,59 +0,0 @@ -package githubexplorer::Repositorie; -use 5.010; -use Moose::Role; -use Net::GitHub::V2::Repositories; - -sub fetch_repositories { - my ( $self, $profile, $repo_list ) = @_; - - foreach my $repo (@$repo_list) { - next if $self->_repo_exists( $profile, $repo->{name} ); - say "-> check " . $profile->login . "'s ".$repo->{name}; - # my $github = Net::GitHub::V2::Repositories->new( - # owner => $profile->login, - # repo => $repo->{name}, - # login => $self->api_login, - # token => $self->api_token, - # ); - - # my $langs = $github->languages(); - # sleep(1); - # return unless grep {/perl/i} keys %$langs; - # my $repo_desc = $github->show(); - # sleep(1); - # $profile->perl_total_bytes( $profile->perl_total_bytes + $langs->{Perl} ); - # $self->schema->txn_do( sub { $profile->update } ); - $self->_create_repo( $profile, $repo ); - } -} - - -sub _repo_exists { - my ( $self, $profile, $repo_name ) = @_; - return - if $self->schema->resultset('Repositories') - ->find( { name => $repo_name, id_profile => $profile->id } ); -} - -sub _create_repo { - my ( $self, $profile, $repo_desc ) = @_; - - my $repo_rs = $self->schema->resultset('Repositories') - ->find( { id_profile => $profile->id, name => $repo_desc->{name} } ); - if ( !$repo_rs ) { - my $repo_insert = { - id_profile => $profile->id, - map { $_ => $repo_desc->{$_} } - (qw/description name homepage url watchers forks fork/) - }; - $self->schema->txn_do( - sub { - $repo_rs = $self->schema->resultset('Repositories') - ->create($repo_insert); - } - ); - } - $repo_rs; -} - -1; diff --git a/lib/githubexplorer/Repository.pm b/lib/githubexplorer/Repository.pm new file mode 100644 index 0000000..7a402dd --- /dev/null +++ b/lib/githubexplorer/Repository.pm @@ -0,0 +1,85 @@ +package githubexplorer::Repository; +use 5.010; +use Moose::Role; +use Net::GitHub::V2::Repositories; +use YAML::Syck; + +sub fetch_repositories { + my ( $self, $profile ) = @_; + + my $github_profile = Net::GitHub::V2::Users->new( + owner => $profile->login, + login => $self->api_login, + token => $self->api_token, + ); + + my $repo_list = $github_profile->list(); + + foreach my $repos (@$repo_list) { + next if $self->_repo_exists( $profile, $repos->{name} ); + say "-> check " . $profile->login . "'s " . $repos->{name}; + if ( $repos->{forks} == 0 ) { + say "<- not forked, skip"; + next; + } + my $repo_rs; + unless ( $repo_rs = $self->_repo_exists( $profile, $repos->{name} ) ) { + $repo_rs = $self->_create_repo( $profile, $repos ); + say "== repository " . $repos->{name} . " created"; + } + sleep(1); + my $api_repos = Net::GitHub::V2::Repositories->new( + owner => $profile->login, + repo => $repos->{name}, + login => $self->api_login, + token => $self->api_token, + ); + my $langs = $api_repos->languages; + foreach my $lang ( keys %$langs ) { + my $lang_rs = $self->_lang_exists($lang); + $self->schema->resultset('RepoLang')->create( + { + repository => $repo_rs->id, + language => $lang_rs->name, + size => $langs->{$lang}, + } + ); + } + sleep(1); + } + sleep(1); +} + +sub _lang_exists { + my ( $self, $lang ) = @_; + $self->schema->resultset('Language')->find_or_create({name => $lang}); +} + +sub _repo_exists { + my ( $self, $profile, $repo_name ) = @_; + $self->schema->resultset('Repositories') + ->find( { name => $repo_name, id_profile => $profile->id } ); +} + +sub _create_repo { + my ( $self, $profile, $repo_desc ) = @_; + + my $repo_rs = $self->schema->resultset('Repositories') + ->find( { id_profile => $profile->id, name => $repo_desc->{name} } ); + if ( !$repo_rs ) { + my $repo_insert = { + id_profile => $profile->id, + map { $_ => $repo_desc->{$_} } + (qw/description name homepage url watchers forks fork/) + }; + $self->schema->txn_do( + sub { + $repo_rs = $self->schema->resultset('Repositories') + ->create($repo_insert); + } + ); + } + $repo_rs; +} + +1; -- cgit 1.4.1