-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.pl
233 lines (193 loc) · 5.98 KB
/
main.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
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
#!/usr/bin/perl -w
use strict;
use warnings;
use Pithub;
use Config::INI::Reader;
use HTML::Entities;
use utf8;
use encoding "utf8";
use Template;
use Scalar::Util qw(looks_like_number);
use POSIX qw(LONG_MAX);
# github user whose repositories to list.
# username value from config.ini overrides this.
my $username = 'nordicway';
# 0 to hide private repos, 1 to show them.
# TODO make this work and test it with a private Github account.
my $include_private_repos = 0;
# template file location
my $template_file = 'templates/template_bootstrap_fancy.tt';
# output file location
my $output_file = 'github.html';
##############################################################################
# End of configuration. #
# Only change anything below this line if you know what you are doing. #
##############################################################################
# sort criteria, eg. stargazers_count
my $sort_criteria;
# sort order, 1 for ascending order, 0 for descending order
my $sort_ascending;
# config file location
my $ini_file = 'config.ini';
# 1 to encode entities (default), 0 not to
my $encode_entities = 1;
my $repos = load();
my @ignored_repos;
my @whitelisted_repos;
my @sorted_repos;
my $whitelist_mode;
# list all repos of $username
my $p = Pithub->new;
my $q_repos = Pithub::Repos->new;
my $result = $q_repos->list( user => $username );
$result->auto_pagination(1);
while ( my $row = $result->next ) {
#check if we run into API block, eg. due to Github rate limiting
if (exists $row->{message}) {
die "error from Github API: " . $row->{message};
}
if (
#this is actually a "true" string we get back from Github API
$row->{private} eq "true" and $include_private_repos == 0
) {
next;
} elsif ($whitelist_mode == 0 and is_ignored($row->{name})) {
#skip if it is on the ignore list
next;
} elsif ($whitelist_mode == 1 and is_whitelisted($row->{name})!=1) {
#skip if whitelist mode is on and repo is not on the white list
next;
}
#get repo information from API and add them to the existing entries
#from config.ini .
my %repo = get_repo($row);
foreach my $key(keys %repo) {
$repos->{ $row->{name} }->{$key} = %repo->{$key};
}
}
sort_repos();
output(\@sorted_repos);
print "done. see " . $output_file;
# load configuration from config file
sub load {
my $read = Config::INI::Reader->read_file($ini_file);
$username = $read->{_}->{username};
$sort_criteria = $read->{_}->{sort_criteria} || "stars";
if ($sort_criteria eq "manual") {
#enforce sort order to be ascending when sorting manually
$sort_ascending = 1;
} else {
$sort_ascending = $read->{_}->{sort_ascending} || 0;
}
# set ignored repos explicitly because underscore variables are private
# in Template Toolkit
@ignored_repos = csv_to_array($read->{_}->{skip});
foreach my $ignored (@ignored_repos) {
delete $read->{$ignored};
}
@whitelisted_repos = csv_to_array($read->{_}->{include});
if (@whitelisted_repos) {
# there are whitelisted repositories, so switch whitelist mode on
$whitelist_mode = 1;
} else {
$whitelist_mode = 0;
}
delete $read->{_};
return $read;
}
# returns a repository hash from API repo information
sub get_repo {
my $row = shift;
my $name = $row->{name};
#fill with attributes that cannot be taken from API.
#this is mainly for backward compatibility (eg. stars) and internal logic
#(manual order).
my %repo = (
manual => existing_or_new($name, 'order', ( $row->{order} || LONG_MAX ) ),
stars => existing_or_new($name, 'stargazers_count', $row->{stargazers_count}),
watchers => existing_or_new($name, 'watchers_count', $row->{watchers_count}),
forks => existing_or_new($name, 'forks_count', $row->{forks_count})
);
#inherit all incoming values from API
foreach my $key(keys $row) {
$repo{$key} = existing_or_new($name, $key, $row->{$key});
}
return %repo;
}
# returns the attribute from config file if it exists.
# returns the attribute from Github API otherwise.
# this allows you to overwrite attributes via config file.
sub existing_or_new {
my $name = shift;
my $attribute = shift;
my $value = shift;
my $ret_val;
if (my $from_ini = $repos->{$name}->{$attribute} ) {
$ret_val = $from_ini;
} else {
$ret_val = $value;
}
# return HTML encoded entities based on user setting
if ($encode_entities) {
return encode_entities($ret_val);
} else {
return $ret_val;
}
}
# converts a list of comma seperated values to an array
sub csv_to_array {
my $csv = shift || "";
my @array = ();
$csv =~ s/\s//g;
@array = split(',', $csv);
return @array;
}
# check if a repository with this name is on the ignore list
sub is_ignored {
my $name = shift;
return is_name_in_array($name, @ignored_repos);
}
# check if a repository with this name is on the whitelist
sub is_whitelisted {
my $name = shift;
return is_name_in_array($name, @whitelisted_repos);
}
# check if a given name exists within an array
sub is_name_in_array {
my ($name, @array) = @_;
return (grep /^$name$/, @array);
}
# sort all repos
sub sort_repos {
foreach my $sorted_repo (
sort sort_algo keys %$repos ) {
push(@sorted_repos, $$repos{$sorted_repo});
}
if ($sort_ascending == 0) {
@sorted_repos = reverse @sorted_repos;
}
}
# numeric sort if both values are numbers, alphanumeric otherwise.
sub sort_algo {
my $attribute = $sort_criteria;
my $val1 = $$repos{$a}->{$attribute};
my $val2 = $$repos{$b}->{$attribute};
if (looks_like_number($val1) and looks_like_number($val2)) {
return $val1 <=> $val2;
} else {
return $val1 cmp $val2;
}
}
# writes formatted repository information to output file using the given
# template
sub output {
my $var = shift;
my $tt = Template->new({
INTERPOLATE => 1,
}) or die "$Template::ERROR\n";
my $tt_vars = {
repos => $var,
username => $username
};
$tt->process($template_file, $tt_vars, $output_file) or die $tt->error(), "\n";
}