diff options
| author | franck cuny <franck@lumberjaph.net> | 2011-06-13 16:33:23 +0200 |
|---|---|---|
| committer | franck cuny <franck@lumberjaph.net> | 2011-06-13 16:33:23 +0200 |
| commit | 871336c030209b46ae6b124a702677363487f9a8 (patch) | |
| tree | 86f234d42c68b26a7aeb9cc373667127ad661e19 /lib/GitHub/Collector/Role/Relation.pm | |
| parent | use template_toolkit and add infos about colors (diff) | |
| download | stargit-871336c030209b46ae6b124a702677363487f9a8.tar.gz | |
import github::collector
Signed-off-by: franck cuny <franck@lumberjaph.net>
Diffstat (limited to 'lib/GitHub/Collector/Role/Relation.pm')
| -rw-r--r-- | lib/GitHub/Collector/Role/Relation.pm | 70 |
1 files changed, 70 insertions, 0 deletions
diff --git a/lib/GitHub/Collector/Role/Relation.pm b/lib/GitHub/Collector/Role/Relation.pm new file mode 100644 index 0000000..b89d0fc --- /dev/null +++ b/lib/GitHub/Collector/Role/Relation.pm @@ -0,0 +1,70 @@ +package GitHub::Collector::Role::Relation; + +use Try::Tiny; +use Moose::Role; + +with qw/GitHub::Collector::Role::Pause/; + +has types => ( + is => 'ro', + isa => 'ArrayRef', + auto_deref => 1, + default => sub { [qw/followers following/] } +); + +sub add_relations { + my ( $self, $login ) = @_; + + foreach my $type ($self->types) { + my $users = $self->_grab_relations( $login, $type ); + foreach my $user (@$users) { + $self->_bootstrap_profile($user); + if ($type eq 'followers'){ + $self->_add_relation($user, $login); + }else{ + $self->_add_relation($login, $user); + } + } + } +} + +sub _grab_relations { + my ( $self, $login, $type ) = @_; + + $self->log( [ "fetching %s informations for %s", $type, $login ] ); + + my $method = 'list_' . $type; + my ( $users, $error ); + try { + $users = $self->spore_client->$method( + format => 'json', + user => $login, + )->body->{users}; + } + catch { + $error = $_; + if ( $error->status == 403 ) { + $self->debug( + [ "need to pause (while grabbing relations for %s)", $login ] ); + sleep($self->pause_on_error); + $self->_grab_relations( $login, $type ); + } + else { + $self->debug( + [ "can't fetch %s relation for %s: %s", $type, $login, $error ] + ); + } + }; + + sleep( $self->pause ); + return $users; +} + +sub _add_relation { + my ($self, $source, $target) = @_; + my $search = {source => $source, target => $target}; + my $exists = $self->db_relations->find_one($search); + $self->db_relations->insert($search) if !$exists; +} + +1; |
