-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgithub_stats.pl
More file actions
executable file
·288 lines (269 loc) · 12.6 KB
/
github_stats.pl
File metadata and controls
executable file
·288 lines (269 loc) · 12.6 KB
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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
#!/usr/bin/perl
#======================================================================#
# Program => github_stats.pl (In Perl 5.30) version 1.0.0 #
#======================================================================#
# Autor => Fernando "El Pop" Romo (pop@cofradia.org) #
# Creation date => 19/Jun/2024 #
#----------------------------------------------------------------------#
# Info => Simple script to show the repositories info using the #
# GitHub Developer API. #
#----------------------------------------------------------------------#
# This code are released under the GPL 3.0 License. #
# #
# (c) 2024 - Fernando Romo #
# #
# This program is free software: you can redistribute it and/or modify #
# it under the terms of the GNU General Public License as published by #
# the Free Software Foundation, either version 3 of the License, or #
# (at your option) any later version. #
# #
# This program is distributed in the hope that it will be useful, but #
# WITHOUT ANY WARRANTY; without even the implied warranty of #
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU #
# General Public License for more details. #
# #
# You should have received a copy of the GNU General Public License #
# along with this program. If not, see <https://www.gnu.org/licenses/> #
#======================================================================#
use strict;
use JSON; # read and write json
use LWP::UserAgent; # Web user agent class
use Getopt::Long; # Handle the arguments passed to the program
use Config::Simple; # read config file
# Terminal Colors
use constant {
RESET => "\033[0m",
BRIGHT => "\033[1m",
DIM => "\033[2m",
UNDERLINE => "\033[3m",
BLINK => "\033[5m",
REVERSE => "\033[7m",
HIDDEN => "\033[8m",
FG_BLACK => "\033[30m",
FG_RED => "\033[31m",
FG_GREEN => "\033[32m",
FG_YELLOW => "\033[33m",
FG_BLUE => "\033[34m",
FG_MAGENTA => "\033[35m",
FG_CYAN => "\033[36m",
FG_WHITE => "\033[37m",
FG_BRIGHT_BLACK => "\033[90m",
FG_BRIGHT_RED => "\033[91m",
FG_BRIGHT_GREEN => "\033[92m",
FG_BRIGHT_YELLOW => "\033[93m",
FG_BRIGHT_BLUE => "\033[94m",
FG_BRIGHT_MAGENTA => "\033[95m",
FG_BRIGHT_CYAN => "\033[96m",
FG_BRIGHT_WHITE => "\033[97m",
BG_BLACK => "\033[40m",
BG_RED => "\033[41m",
BG_GREEN => "\033[42m",
BG_YELLOW => "\033[43m",
BG_BLUE => "\033[44m",
BG_MAGENTA => "\033[45m",
BG_CYAN => "\033[46m",
BG_WHITE => "\033[47m",
BG_BRIGHT_BLACK => "\033[100m",
BG_BRIGHT_RED => "\033[101m",
BG_BRIGHT_GREEN => "\033[102m",
BG_BRIGHT_YELLOW => "\033[103m",
BG_BRIGHT_BLUE => "\033[104m",
BG_BRIGHT_MAGENTA => "\033[105m",
BG_BRIGHT_CYAN => "\033[106m",
BG_BRIGHT_WHITE => "\033[107m",
};
my %matrix_options = ('color' => { 'project' => BG_RED . BRIGHT . FG_BRIGHT_WHITE,
'header' => BG_WHITE . BRIGHT . FG_BLACK,
'header_2' => BG_BRIGHT_WHITE . BRIGHT . FG_BLACK,
'info' => BG_BRIGHT_YELLOW . BRIGHT . FG_BLACK,
'date' => BG_BRIGHT_WHITE . BRIGHT . FG_BLACK,
'c' => BG_BRIGHT_CYAN . FG_BLACK,
'u' => BG_BRIGHT_WHITE . FG_BLACK,
'c_header' => BG_CYAN . FG_BLACK,
'u_header' => BG_WHITE . FG_BLACK, }, );
my $Config = new Config::Simple('/etc/github_stats.conf');
my %options = ();
GetOptions(\%options,
'text',
);
my @reports = ('clones', 'views');
my %projects = ();
my $user = $Config->param('github.user');
my %resume = ();
my %totals = ();
sub get_info {
my $url = shift;
my $json = '';
my $ua = LWP::UserAgent->new(
agent => 'GitHubStats/1.0',
keep_alive => 1,
env_proxy => 1,
ssl_opts => { verify_hostname => 0,
SSL_verify_mode => 0x00,
},
);
my $res = $ua->request(
HTTP::Request->new(GET => $url,
# Insert the Authentication Headers requested by GitHub API
HTTP::Headers->new('Accept' => 'application/vnd.github+json',
'Authorization' => 'Bearer '. $Config->param('github.token'),
'X-GitHub-Api-Version' => '2022-11-28'),
),
sub {
$json .= $_[0];
}
);
undef $ua;
return $json;
} #End get_info()
sub project_summary_text {
my $project = shift;
print '-' x 49 . "\n";
my $repo_type = 'Public';
if ($projects{$project}{private}) {
$repo_type = 'Private';
}
print sprintf("\| %-33s ( %7s ) \|\n", $project, $repo_type);
print '|' . ('-' x 47) . "\|\n";
print sprintf("\| Created: %-29s \|\n\| Updated: %-29s \|\n",
$projects{$project}{created_at},
$projects{$project}{updated_at});
print '|' . ('-' x 47) . "\|\n";
print sprintf("\| Starts: %-5d Forks: %-5d Issues: %-5d \|\n",
$projects{$project}{starts},
$projects{$project}{forks},
$projects{$project}{issues});
if ( exists($resume{$project}) ) {
# Detail
print '|' . ('-' x 47) . "\|\n";
print '| | Visitors | Clones |' . "\n";
print '| Date (Zulu) |----------------|----------------|' . "\n";
print '| | Count | Unique | Count | Unique |' . "\n";
print '|-------------|-------|--------|-------|--------|' . "\n";
foreach my $date (sort { "\U$a" cmp "\U$b" } keys %{$resume{$project}} ) {
print sprintf("\| %10s \| %5d \| %5d \| %5d \| %5d \|\n",
$date,
$resume{$project}{$date}{views}{count},
$resume{$project}{$date}{views}{uniques},
$resume{$project}{$date}{clones}{count},
$resume{$project}{$date}{clones}{uniques});
}
# Totals
print '|-------------|-------|--------|-------|--------|' . "\n";
print sprintf("\| Totals \| %5d \| %5d \| %5d \| %5d \|\n",
$totals{$project}{views}{count},
$totals{$project}{views}{uniques},
$totals{$project}{clones}{count},
$totals{$project}{clones}{uniques});
}
print '-' x 49 . "\n\n";
} # End project_summary_text()
sub project_summary_ansi {
my $project = shift;
my $repo_type = 'Public';
if ($projects{$project}{private}) {
$repo_type = 'Private';
}
print sprintf($matrix_options{color}{project} . " %-30s (%7s ) " . RESET . "\n", $project, $repo_type);
print sprintf($matrix_options{color}{header} . " Created: %-28s ". RESET . "\n" .
$matrix_options{color}{header} . " Updated: %-28s " . RESET . "\n",
$projects{$project}{created_at},
$projects{$project}{updated_at});
print sprintf($matrix_options{color}{info} ." Starts: %-5d Forks: %-5d Issues: %-5d " . RESET . "\n",
$projects{$project}{starts},
$projects{$project}{forks},
$projects{$project}{issues});
if ( exists($resume{$project}) ) {
# Detail
print $matrix_options{color}{header_2} . ' Visitors Clones ' . RESET . "\n";
print $matrix_options{color}{header} . ' Date (Zulu) ' .
$matrix_options{color}{c_header} . ' Count ' .
$matrix_options{color}{u_header} . ' Unique ' .
$matrix_options{color}{c_header} . ' Count ' .
$matrix_options{color}{u_header} . ' Unique ' .
RESET . "\n";
foreach my $date (sort { "\U$a" cmp "\U$b" } keys %{$resume{$project}} ) {
print sprintf($matrix_options{color}{date} . " %10s " .
$matrix_options{color}{c} . " %5d " .
$matrix_options{color}{u} . " %5d " .
$matrix_options{color}{c} . " %5d " .
$matrix_options{color}{u} . " %5d " .
RESET . "\n",
$date,
$resume{$project}{$date}{views}{count},
$resume{$project}{$date}{views}{uniques},
$resume{$project}{$date}{clones}{count},
$resume{$project}{$date}{clones}{uniques});
}
# Totals
print sprintf($matrix_options{color}{header} . ' Totals ' .
$matrix_options{color}{c_header} . " %5d " .
$matrix_options{color}{u_header} . " %5d " .
$matrix_options{color}{c_header} . " %5d " .
$matrix_options{color}{u_header} . " %5d " .
RESET . "\n",
$totals{$project}{views}{count},
$totals{$project}{views}{uniques},
$totals{$project}{clones}{count},
$totals{$project}{clones}{uniques});
}
print "\n";
} # End project_summary_ansi()
#-----------#
# Main body #
#-----------#
my $find = $ARGV[0];
my $data ='';
# Get Repositories from user
if ($find) {
$data = get_info("https://api.github.com/repos/$user/$find");
}
else {
$data = get_info("https://api.github.com/search/repositories?q=user:$user");
}
if ($data) {
# Get Repositories from user
my $msg_ref = from_json($data);
if (ref($msg_ref->{items}) eq 'ARRAY') {
for (my $i=0; $i <= (@{ $msg_ref->{items} } - 1) ; $i++) {
$projects{$msg_ref->{items}->[$i]->{name}}{private} = "$msg_ref->{items}->[$i]->{private}";
$projects{$msg_ref->{items}->[$i]->{name}}{forks} = $msg_ref->{items}->[$i]->{forks_count};
$projects{$msg_ref->{items}->[$i]->{name}}{starts} = $msg_ref->{items}->[$i]->{stargazers_count};
$projects{$msg_ref->{items}->[$i]->{name}}{issues} = $msg_ref->{items}->[$i]->{open_issues_count};
$projects{$msg_ref->{items}->[$i]->{name}}{created_at} = $msg_ref->{items}->[$i]->{created_at};
$projects{$msg_ref->{items}->[$i]->{name}}{updated_at} = $msg_ref->{items}->[$i]->{updated_at};
}
}
else {
$projects{$msg_ref->{name}}{private} = $msg_ref->{private};
$projects{$msg_ref->{name}}{forks} = $msg_ref->{forks_count};
$projects{$msg_ref->{name}}{starts} = $msg_ref->{stargazers_count};
$projects{$msg_ref->{name}}{issues} = $msg_ref->{open_issues_count};
$projects{$msg_ref->{name}}{created_at} = $msg_ref->{created_at};
$projects{$msg_ref->{name}}{updated_at} = $msg_ref->{updated_at};
}
# Download each repository info (Clones and Views)
foreach my $project ( sort { "\U$a" cmp "\U$b" } keys %projects ) {
foreach my $report (@reports) {
my $msg_ref = from_json(get_info("https://api.github.com/repos/$user/$project/traffic/$report"));
if (ref($msg_ref->{$report}) eq 'ARRAY') {
for (my $i=0; $i <= (@{ $msg_ref->{$report} } - 1) ; $i++) {
my ($timestamp) = split(/T/,$msg_ref->{$report}->[$i]->{timestamp});
$resume{$project}{$timestamp}{$report}{count} = $msg_ref->{$report}->[$i]->{count};
$resume{$project}{$timestamp}{$report}{uniques} = $msg_ref->{$report}->[$i]->{uniques};
$totals{$project}{$report}{count} += $msg_ref->{$report}->[$i]->{count};
$totals{$project}{$report}{uniques} += $msg_ref->{$report}->[$i]->{uniques};
}
}
}
if ($options{'text'}) {
project_summary_text($project);
}
else {
project_summary_ansi($project);
}
%resume = ();
%totals = ();
}
}
# End Main Body #