-
Notifications
You must be signed in to change notification settings - Fork 0
/
graph.pl
executable file
·193 lines (145 loc) · 4.83 KB
/
graph.pl
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
#!/usr/bin/perl
#
# Gitub Social Graph
use strict;
use warnings;
use Config::Simple;
use File::Basename;
use LWP::Simple;
use JSON;
use Log::Handler;
use GraphViz2;
use lib "packages";
use Person;
# make a new config config object
my $currentpath = dirname(__FILE__);
my $cfg = new Config::Simple("$currentpath/graph.config");
# some global variables
my $username = $cfg->param('username');
my $password = $cfg->param('password');
my $depth = $cfg->param('depth');
my $output = $cfg->param('output');
my $json = JSON->new->allow_nonref;
# All related persons to a account
my %people;
# Made user the first person
my $self = makePerson($username);
$people{$username} = $self;
# aggregate others
followlinks($self,$depth);
my($logger) = Log::Handler -> new;
$logger -> add(
screen =>
{
maxlevel => 'debug',
message_layout => '%m',
minlevel => 'error',
}
);
#logger => $logger,
# Build the graph
my($graph) = GraphViz2 -> new(
edge => {color => 'grey'},
global => {directed => 1},
graph => {
label => 'Social graph',
layout => 'circo',
splines => 'compound',
overlap => 'false',
},
node => {shape => 'oval'},
);
print "Generate Graph\n";
# Generate nodes
while(my @person=each(%people))
{
$graph -> add_node(name => $person[0], color => 'blue',shape => 'house');
foreach my $repo (@{${$person[1]}{_repos}}) {
$graph -> add_node(name => $repo, color => 'violet',shape => 'box');
}
foreach my $star (@{${$person[1]}{_starred}}) {
$graph -> add_node(name => $star, color => 'red',shape => 'diamond');
}
}
while(my @person=each(%people))
{
foreach my $follower (@{${$person[1]}{_follower}}) {
$graph -> add_edge(from => $person[0], to => $follower, arrowsize => 1, color => 'green', dir => 'back');
}
foreach my $following (@{${$person[1]}{_following}}) {
$graph -> add_edge(from => $person[0], to => $following, arrowsize => 1, color => 'blue', dir => 'forward');
}
foreach my $star (@{${$person[1]}{_starred}}) {
$graph -> add_edge(from => $person[0], to => $star, arrowsize => 1, color => 'red', dir => 'forward');
}
foreach my $repo (@{${$person[1]}{_repos}}) {
$graph -> add_edge(from => $person[0], to => $repo, arrowsize => 1, color => 'violet', dir => 'forward');
}
}
print "Graph ready -> Output\n";
$graph->run(driver => 'sfdp', format => $output, output_file => "$username.$output");
print "Finish\n";
###### Subs
# traverse all persons on github related to the user
sub followlinks {
my $actperson = shift;
my $actdepth = shift;
if($actdepth == 0){
return;
}
$actdepth--;
foreach my $person (@{$actperson->getFollower()}) {
if(!(exists $people{$person})){
print "Create a new entry for \"$person\"\n";
my $self = makePerson($person);
$people{$person} = $self;
followlinks($self,$actdepth);
}
}
foreach my $person (@{$actperson->getFollowing()}) {
if(!(exists $people{$person})){
print "Create a new entry for \"$person\"\n";
my $self = makePerson($person);
$people{$person} = $self;
followlinks($self,$actdepth);
}
}
}
#init a new person
sub makePerson {
my $name = shift;
my @followers;
my $rawdata = get("https://$username:$password\@api.github.com/users/$name/followers");
my $json_followers = $json->decode($rawdata);
# write Followers to array
foreach my $follower (@$json_followers) {
push (@followers, $follower->{"login"});
}
# Get Following
$rawdata = get("https://$username:$password\@api.github.com/users/$name/following");
my $json_following = $json->decode($rawdata);
# write Followings to array
my @following;
foreach my $ifollow (@$json_following) {
push (@following, $ifollow->{"login"});
}
# Get Starred
$rawdata = get("https://$username:$password\@api.github.com/users/$name/starred");
my $json_starred = $json->decode($rawdata);
# write stars to array
my @starred;
foreach my $star (@$json_starred) {
push (@starred, $star->{"full_name"});
}
# Get Repos
$rawdata = get("https://$username:$password\@api.github.com/users/$name/repos");
my $json_repos = $json->decode($rawdata);
# write Repos to array
my @repos;
foreach my $repo (@$json_repos) {
push (@repos, $repo->{"full_name"});
}
# make me to the first person
my $self = new Person( $name, \@followers, \@following, \@starred, \@repos);
return $self;
}