summary refs log tree commit diff
path: root/lib/githubexplorer/Profile.pm
blob: d4024dfa35c1fcd0174842dffad013d33f6b3b33 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
package githubexplorer::Profile;
use 5.010;
use Moose::Role;
use Net::GitHub::V2::Users;

sub fetch_profile {
    my ( $self, $login, $depth ) = @_;

    return if $depth > 2;
    my $profile = $self->_profile_exists($login);

    say "fetch profile for $login ($depth)...";
    sleep(1);
    my $github = Net::GitHub::V2::Users->new(
        owner => $login,
        login => $self->api_login,
        token => $self->api_token,
    );
    sleep(2);

    if ( !$profile ) {
        $profile = $self->_create_profile( $login, $github->show, $depth );
        if ( $self->with_repo ) {
            foreach my $repo ( @{ $github->list } ) {
                $self->fetch_repo( $profile, $repo->{name} );
            }
        }
        sleep(1);
    }
    my $followers   = $github->followers();
    my $local_depth = $depth + 1;
    foreach my $f (@$followers) {
        my $p = $self->fetch_profile( $f, $local_depth );
        next unless $p;
        $self->schema->resultset('Follow')
            ->create(
            { id_following => $profile->id, id_follower => $p->id } );
    }
    $profile;
}

sub _profile_exists {
    my ( $self, $login ) = @_;
    my $profile
        = $self->schema->resultset('Profiles')->find( { login => $login } );
    return $profile;
}

sub _create_profile {
    my ( $self, $user_name, $profile, $depth ) = @_;

    $profile->{depth} = $depth;

    my $profile_rs = $self->schema->resultset('Profiles')->create($profile);
    say $profile_rs->login."'s profile created";
    return $profile_rs;
}

1;