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
/
Copy pathfriendnet.pl
executable file
·261 lines (170 loc) · 6.59 KB
/
friendnet.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
258
#!/usr/bin/env perl
#<--------------------------------- MAN PAGE --------------------------------->|
=pod
=head1 NAME
friendnet - Spiders one's social network and saves vertices/edges to CSV-files
=head1 SYNOPSIS
B<friennet.pl>
[B<-u> F<number>]
[B<-d> F<number>]
[B<-c> F<numdays>]
[B<-o> F<dirpath>]
[B<-i>]
F<goodloginmail> [F<goodloginpass>]
=head1 OPTIONS
Mandatory arguments to long options are mandatory for short options too.
=over 4
=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.
You still need to login with your credentials because authenticated
members only can access the member-lists of other members.
=item B<-d, --depth>=F<number>
examine network to N levels.
Runtime and datasize increases exponentially with every level.
Depth 0 is useless, 1 equals exporting your friends/followees list,
2 allows first useful social network analysis.
There is the idea that all seven billion earthlings are 6 or fewer
social connections away from each other
("Six degrees of separation")--don't try to prove it here.
Default is 2.
depth 0: YOU []
depth 1: YOU --> friends []
depth 2: YOU <-> FRIENDS --> friends [100%]
depth 3: YOU <-> FRIENDS <-> FRIENDS --> friends [100%, 100%]
depth 4: YOU <-> FRIENDS <-> FRIENDS <-> FRIENDS --> friends [100%, 100%, 100%]
depth n: ...
Note: Friends with more than 1000 friends or followees are dropped,
because the data of such accounts is likely not meaningful anymore and
just waste your (computing) time.
=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<-o, --outdir>=F<dirpath>
write CSV-files to this directory,
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</tmp/FileCache/>
F<./list-out/friendnet-$GOODUSERID-edges.csv>
F<./list-out/friendnet-$GOODUSERID-nodes.csv>
=head1 EXAMPLES
$ ./friendnet.pl [email protected] MyPASSword
$ ./friendnet.pl --depth=3 --outdir=/tmp/ [email protected]
=head1 REPORTING BUGS
Send an email to <[email protected]> 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/friendnet.md
=head1 VERSION
2022-03-10 (Since 2019-06-14)
=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:
use Text::CSV qw( csv );
# 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 $DEPTH = 2;
our $MAXNHOOD = 1000; # Ignore users with more than N friends
our $OUTDIR = File::Spec->catfile( $FindBin::Bin, 'list-out' );
our $USERID;
GetOptions( 'userid|u=s' => \$USERID,
'depth|d=i' => \$DEPTH,
'outdir|o=s' => \$OUTDIR,
'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 list are private
userpass => $ARGV[1], # Asks pw if omitted
r_userid => \$USERID );
our $OUTPATH_EDG = File::Spec->catfile( $OUTDIR, "friendnet-$USERID-edges.csv" );
our $OUTPATH_NOD = File::Spec->catfile( $OUTDIR, "friendnet-$USERID-nodes.csv" );
#-----------------------------------------------------------------------------
# Primary data structures:
#
my %nodes;
my @edges;
#-----------------------------------------------------------------------------
# Traverse social network:
#
printf( "Traversing #%s's social network (depth=%d)...\n", $USERID, $DEPTH );
# Displays sth. like "Covered: [ 14%, 55%]" for depth = 3
my $progress_indicator_fn = sub
{
my (%args) = @_;
my $dr = $args{depth};
my $d = $DEPTH - $dr;
return if $dr == 1; # We get leaves as whole; percent-progress would be 0 to 100% in 1 step
print ( "\r[" ); # Move cursor to column 0
print ( "\t" x $d ); # Move cursor to column for depth d (tab doesn't del prev. chars)
printf( "%3d%%", $args{perc} ); # Percent-progress for current network depth
print ( ",\t 0%" x ($dr-2) ); # Fill empty columns with "0%"
print ( ']' );
};
# Displays sth. like:
# [ 1%] #1234567
# [ 1%] #76543
# ...
# [100%] #432123
my $progress_indicator_fn2 = sub
{
};
gsocialnet( from_user_id => $USERID,
rh_into_nodes => \%nodes,
ra_into_edges => \@edges,
ignore_nhood_gt => $MAXNHOOD,
depth => $DEPTH,
on_progress => $progress_indicator_fn );
#-----------------------------------------------------------------------------
# Write CSV-files:
#
my @nodeslines = values %nodes;
printf( "\nWriting network data to: \n%s (N=%d)\n%s (N=%d)",
$OUTPATH_NOD, scalar @nodeslines,
$OUTPATH_EDG, scalar @edges );
csv( in => \@nodeslines,
out => $OUTPATH_NOD,
headers => [qw( id name img_url )] );
csv( in => \@edges,
out => $OUTPATH_EDG,
headers => [qw( from to )] );
#-----------------------------------------------------------------------------
# Done:
#
printf( "\n\nTotal time: %.0f minutes\n", (time()-$TSTART)/60 );