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/Profile.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 '')
| -rw-r--r-- | lib/GitHub/Collector/Role/Profile.pm | 84 |
1 files changed, 84 insertions, 0 deletions
diff --git a/lib/GitHub/Collector/Role/Profile.pm b/lib/GitHub/Collector/Role/Profile.pm new file mode 100644 index 0000000..01241d9 --- /dev/null +++ b/lib/GitHub/Collector/Role/Profile.pm @@ -0,0 +1,84 @@ +package GitHub::Collector::Role::Profile; + +use Try::Tiny; +use Moose::Role; +use boolean; + +with qw/ + GitHub::Collector::Role::Pause + GitHub::Collector::Role::Relation + /; + +sub fetch_profile { + my ( $self, $profile ) = @_; + + my ( $res, $error ); + + try { + $res = $self->spore_client->get_info( + format => 'json', + username => $profile, + ); + } + catch { + $error = $_; + }; + + if ($error) { + if ($error->status == 403){ + $self->debug( [ "need to pause (while working on %s)", $profile ] ); + + sleep( $self->pause_on_error ); + return $self->fetch_profile($profile); + }elsif($error->status == 404){ + $self->debug("profile $profile doesn't exists anymore"); + $self->delete_profile($profile); + return; + }else{ + $self->return("can't fetch information for $profile: $error"); + return; + } + } + sleep($self->pause); + return $res->body; +} + +sub save_profile { + my ( $self, $profile_info ) = @_; + + my $id = delete $profile_info->{user}->{id}; + my $time = time(); + + $self->db_profiles->update( + { login => $profile_info->{user}->{login} }, + { + '$set' => { + crawled_at => $time, + repositories_done => false, + %{ $profile_info->{user} } + }, + } + ); + + $self->log( "profile " . $profile_info->{user}->{login} . " saved" ); +} + +sub delete_profile { + my ($self, $profile) = @_; + + $self->db_profiles->remove({login => $profile}); + foreach my $type (qw/target source/){ + $self->db_relations->remove({$type => $profile}); + } + $self->log("all informations regarding $profile have been deleted"); +} + +sub profile_is_done { + my ( $self, $login ) = @_; + $self->db_profiles->update( + { login => $login }, + { '$set' => { done => true } }, + ); +} + +1; |
