This repository has been archived by the owner on Mar 9, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 7
/
friendgroup.pl
executable file
·259 lines (166 loc) · 6.03 KB
/
friendgroup.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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
#!/usr/bin/env perl
#<--------------------------------- MAN PAGE --------------------------------->|
=pod
=head1 NAME
friendgroup - groups common among the members I follow
=head1 SYNOPSIS
B<friendgroup.pl>
[B<-c> F<numdays>]
[B<-o> F<filename>]
[B<-u> F<number>]
[B<-i>]
F<goodloginmail> [F<goodloginpass>]
=head1 OPTIONS
Mandatory arguments to long options are mandatory for short options too.
=over 4
=item B<-c, --cache>=F<numdays>
number of days to store and reuse downloaded data in F</tmp/FileCache/>,
default is 31 days. This helps with cheap recovery on a crash, power blackout
or pause, and when experimenting with parameters. Loading data from Goodreads
is a very time consuming process.
=item B<-u, --userid>=F<number>
check another member instead of the one identified by the login-mail
and password arguments. You find the ID by looking at the shelf URLs.
=item B<-o, --outfile>=F<filename>
name of the HTML file where we write results to,
default see section FILES
=item B<-i, --ignore-errors>
Don't retry on errors, just keep going.
Sometimes useful if a single Goodreads resource hangs over long periods
and you're okay with some values missing in your result.
This option is not recommended when you run the program unattended.
=item B<-?, --help>
show full man page
=back
=head1 FILES
F<./list-out/friendgroup-$GOODUSERID.html>
F</tmp/FileCache/>
=head1 EXAMPLES
$ ./friendgroup.pl login@gmail.com MyPASSword
$ ./friendgroup.pl --outfile=./sub/myfile.html login@gmail.com
=head1 REPORTING BUGS
Report bugs to <datakadabra@gmail.com> or use Github's issue tracker
<https://github.com/andre-st/goodreads-toolbox/issues>
=head1 COPYRIGHT
This is free software. You may redistribute copies of it under the terms of
the GNU General Public License <https://www.gnu.org/licenses/gpl.html>.
There is NO WARRANTY, to the extent permitted by law.
=head1 SEE ALSO
More info in ./help/friendgroup.md
=head1 VERSION
2022-03-10 (Since 2018-09-26)
=cut
#<--------------------------------- 79 chars --------------------------------->|
use strict;
use warnings;
use locale;
use 5.18.0;
# Perl core:
use FindBin;
use local::lib "$FindBin::Bin/lib/local/";
use lib "$FindBin::Bin/lib/";
use Time::HiRes qw( time tv_interval );
use POSIX qw( strftime locale_h );
use File::Spec; # Platform indep. directory separator
use IO::File;
use Getopt::Long;
use Pod::Usage;
# Third party:
# Ours:
use Goodscrapes;
# ----------------------------------------------------------------------------
# Program configuration:
#
setlocale( LC_CTYPE, "en_US" ); # GR dates all en_US
STDOUT->autoflush( 1 );
gsetopt( cache_days => 31 );
our $TSTART = time();
our $OUTPATH;
our $USERID;
GetOptions( 'outfile|o=s' => \$OUTPATH,
'userid|u=s' => \$USERID,
'ignore-errors|i' => sub{ gsetopt( ignore_errors => 1 ); },
'cache|c=i' => sub{ gsetopt( cache_days => $_[1] ); },
'help|?' => sub{ pod2usage( -verbose => 2 ); })
or pod2usage( 1 );
pod2usage( 1 ) if !$ARGV[0];
glogin( usermail => $ARGV[0], # Login required: Followee/friend/groups list are private
userpass => $ARGV[1], # Asks pw if omitted
r_userid => \$USERID );
$OUTPATH = File::Spec->catfile( $FindBin::Bin, 'list-out', "friendgroup-${USERID}.html" )
if !$OUTPATH;
#-----------------------------------------------------------------------------
# Primary data structures:
#
my %members; # {user_id}
my %joins; # {group_id}{user_id}
my %groups; # {group_id}
#-----------------------------------------------------------------------------
# Collect friends and followees data. Include normal users only (no authors):
#
print( "Getting list of members known to #${USERID}..." );
my $t0 = time();
greadfolls( from_user_id => $USERID,
rh_into => \%members,
incl_authors => 0,
on_progress => gmeter( 'members' ));
printf( " (%.2fs)\n", time()-$t0 );
#-----------------------------------------------------------------------------
# Load group memberships of each member
#
my $memdone = 0;
my $memcount = scalar keys %members;
die( $GOOD_ERRMSG_NOMEMBERS ) unless $memcount;
for my $mid (keys %members)
{
printf( "[%3d%%] %-25s #%-10s\t", ++$memdone/$memcount*100, $members{$mid}->{name}, $mid );
my $t0 = time();
my $trackjoinsfn = sub{ $joins{ $_[0]->{id} }{ $mid } = 1; };
greadusergp( from_user_id => $mid,
rh_into => \%groups,
on_group => $trackjoinsfn,
on_progress => gmeter( 'groups' ));
printf( "\t%6.2fs\n", time()-$t0 );
}
say "\nPerfect! Got groups of ${memdone} users.";
#-----------------------------------------------------------------------------
# Write results to HTML file:
#
print "Writing results to \"$OUTPATH\"... ";
my $fh = IO::File->new( $OUTPATH, 'w' ) or die "[FATAL] Cannot write to $OUTPATH ($!)";
my $now = strftime( '%a %b %e %H:%M:%S %Y', localtime );
print $fh ghtmlhead( "Groups joined by friends or followees of member $USERID, on $now",
[ '!Logo', 'Group', 'Members', '>Joined:', '!Joined by' ]);
my $num_finds = 0;
for my $gid (keys %joins)
{
my @joiner_ids = keys %{$joins{$gid}};
my $num_joiners = scalar @joiner_ids;
$num_finds++;
print $fh qq{
<tr>
<td><img src="${\ghtmlsafe( $groups{$gid}->{img_url} )}"></td>
<td><a href="${\ghtmlsafe( $groups{$gid}->{url} )}" target="_blank">
${\ghtmlsafe( $groups{$gid}->{name} )}</a></td>
<td>$groups{$gid}->{num_members}</td>
<td>${num_joiners}</td>
<td>
};
print $fh qq{
<a href="${\ghtmlsafe( $members{$_}->{url} )}" target="_blank" class="gr-user">
<img src="${\ghtmlsafe( $members{$_}->{img_url} )}"
title="${\ghtmlsafe( $members{$_}->{name} )}">
</a>
} foreach (@joiner_ids);
print $fh qq{
</td>
</tr>
};
}
print $fh ghtmlfoot();
undef $fh;
printf "%d groups\n", $num_finds;
#-----------------------------------------------------------------------------
# Done:
#
printf "Total time: %.0f minutes\n", (time()-$TSTART)/60;