blib/lib/WWW/Orkut/Spider.pm

filter: Statementstmt 17.7% 28 / 158 Branchbran 0.0% 0 / 30 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 33.3% 7 / 21 Podpod 100% 14 / 14 totaltotal 21.9% 49 / 223 CC 26 SCAR 29.0
CC
26
Cov
15%
follow_friends
42.0
get_friendsfriends
42.0
parse_friends
20.0
CRAP 18.2
↑ Summary
1 package WWW::Orkut::Spider;
2  
3 1 ▶use 5.008002;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 1 require Exporter;
7 1 ▶use AutoLoader qw(AUTOLOAD);
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 1 our @ISA = qw(Exporter);
10  
11 # Items to export into callers namespace by default. Note: do not export
12 # names by default without a very good reason. Use EXPORT_OK instead.
13 # Do not simply export all your public functions/methods/constants.
14  
15 # This allows declaration use WWW::Orkut::Spider ':all';
16 # If you do not need this, moving things directly into @EXPORT or @EXPORT_OK
17 # will save memory.
18 1 our %EXPORT_TAGS = ( 'all' => [ qw(
19 new
20 login
21 logout
22 name
23 users
24 get_myfriends
25 get_hisfriends
26 get_friendsfriends
27 get_xml_friendslist
28 get_xml_communities
29 get_xml_profile
30
31 ) ] );
32  
33 1 our @EXPORT_OK = ( @{ $EXPORT_TAGS{'all'} } );
34  
35 1 our @EXPORT = qw(
36
37 );
38  
39 1 our $VERSION = '0.03';
40  
41  
42 # Preloaded methods go here.
43 1 ▶use WWW::Mechanize;
Subroutine BEGIN: called CC=1 SCAR=0.0
44 1 ▶use HTML::Entities;
Subroutine BEGIN: called CC=1 SCAR=0.0
45 1 ▶use HTML::Entities qw(encode_entities_numeric);
Subroutine BEGIN: called CC=1 SCAR=0.0
46 1 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
47 =head1 NAME
48  
49 WWW::Orkut::Spider - Perl extension for spidering the orkut community
50  
51 =head1 SYNOPSIS
52  
53 use WWW::Orkut::Spider;
54 my $orkut = WWW::Orkut::Spider->new;
55 $orkut->login($user,$pass);
56 $orkut->get_hisfriends($uid);
57 print $orkut->get_xml_profile($uid);
58  
59  
60 =head1 DESCRIPTION
61  
62 WWW::Orkut::Spider uses WWW:Mechanize to scrape orkut.com.
63 Output is a simple xml format containing friends, communities and profiles for a given Orkut UID.
64  
65 - Access to orkut.com via WWW::Mechanize
66 - Collects UIDs
67 - Fetches Profiles/Communities/Friends for a given UID
68 - Output via simple xml format
69  
70 =head2 new (proxy)
71  
72 You can specify a Proxy Server here
73 i.e: http://www.proxy.de:8080/
74 or: undef
75  
76 =cut
77 0 ▶sub new {
Subroutine new: not called CC=1 SCAR=6.9
Pod documented
78 0 my $class = shift;
79 0 my $self = {};
80 0 $self->{proxy} = shift;
81 0 $self->{useragent} = 'Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.5) Gecko/20031107 Galeon/1.3.11a (Debian package 1.3.11a-2)';
82  
83 0 return bless $self,$class;
84 }
85  
86 =head2 login (user,pass)
87
88 login orkut as user with pass
89 return undef if unseccessful
90  
91 =cut
92 0 ▶sub login {
Subroutine login: not called CC=3 SCAR=24.8
Pod documented
93 0 my $self=shift;
94 0 $self->{user}=shift;
95 0 $self->{pass}=shift;
96  
97 $self->{agent} = WWW::Mechanize->new( autocheck => 1,
98 agent => $self->{useragent},
99 0 );
100 0 $self->{agent}->proxy( 'http', $self->{proxy} );
101
102 # get main page
103 0 $self->{agent}->get ('http://www.orkut.com/');
104 0 ▶ unless ($self->{agent}->success) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"agent"}->success) 0 0 0
105 0 croak "Can't even get the main page: ", $self->{agent}->response->status_line;
106 0 return;
107 }
108
109 # submit login form
110 0 $self->{agent}->submit_form( fields => { u => $self->{user}, p => $self->{pass}, });
111 0 sleep 1;
112  
113 # goto home page
114 0 ▶ unless ( $self->{agent}->get("/Home.aspx") ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"agent"}->get("/Home.aspx")) 0 0 0
115 0 croak "cannot get users home page";
116 0 return;
117 }
118 0 return 1;
119 }
120  
121 =head2 logout
122
123 logout of orkut
124  
125 =cut
126 0 ▶sub logout {
Subroutine logout: not called CC=1 SCAR=6.9
Pod documented
127 0 my $self=shift;
128 0 $self->{agent}->follow('Logout');
129 }
130  
131 =head2 name (uid)
132
133 return name of given known uid
134  
135 =cut
136 0 ▶sub name {
Subroutine name: not called CC=1 SCAR=6.9
Pod documented
137 0 my $self = shift;
138 0 my $uid = shift;
139 0 return $self->{users}{$uid};
140 }
141  
142 =head2 users
143
144 return array with all known uids
145  
146 =cut
147 0 ▶sub users {
Subroutine users: not called CC=1 SCAR=6.9
Pod documented
148 0 my $self=shift;
149 0 return keys %{$self->{users}};
150 }
151  
152 =head2 xml (tag,value)
153
154 return a simple
155 <tag>value</tag>
156  
157 =cut
158 0 ▶sub xml {
Subroutine xml: not called CC=1 SCAR=6.9
Pod documented
159 0 my $tag = shift;
160 0 my $val = encode_entities_numeric(shift);
161 0 return "\t<$tag>$val</$tag>\n";
162 }
163  
164 =head2 get_myfriends
165  
166 only after login
167 follow the link to friendslist
168 and get friends uids
169 return 1 if success
170  
171 =cut
172 0 ▶sub get_myfriends {
Subroutine get_myfriends: not called CC=3 SCAR=24.8
Pod documented
173 0 my $self=shift;
174  
175 0 ▶ unless ( $self->{agent}->follow_link( url_regex => qr/FriendsList/ ) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"agent"}->follow_link("url_regex", qr/FriendsList/)) 0 0 0
176 0 croak "cannot follow link to FriendsList";
177 0 return;
178 }
179 0 my $users = $self->follow_friends();
180 0 foreach (keys %{$users}) {
181 0 $self->{users}{$_} = $users->{$_};
182 }
183 0 return 1;
184 }
185  
186 =head2 get_hisfriends (uid)
187  
188 parse uid friends page for more uids
189  
190 =cut
191 0 ▶sub get_hisfriends {
Subroutine get_hisfriends: not called CC=3 SCAR=24.8
Pod documented
192 0 my $self=shift;
193 0 my $uid = shift;
194  
195 0 ▶ unless ( $self->{agent}->get("/FriendsList.aspx?uid=".$uid ) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"agent"}->get('/FriendsList.aspx?uid=' . $uid)) 0 0 0
196 0 croak "cannot get FriendsList.aspx?uid=$uid";
197 0 return;
198 }
199 0 my $users = $self->follow_friends();
200 0 foreach (keys %{$users}) {
201 0 $self->{users}{$_} = $users->{$_};
202 }
203 0 return 1;
204 }
205  
206 =head2 follow_friends
207  
208 follow through all friends pages
209 called after GET of first friend page
210  
211 =cut
212 0 ▶sub follow_friends {
Subroutine follow_friends: not called CC=6 SCAR=37.4
Pod documented
213 0 my $self = shift;
214  
215 # get first page users
216 0 my $users = $self->parse_friends();
217  
218 # get avaible pages
219 0 my @links = $self->{agent}->find_all_links(url_regex=> qr/FriendsList.*uid/);
220 0 my @pages;
221 0 foreach my $l (@links) {
222 0 my $uid = $l->[0];
223 0 ▶ if ( $uid =~ m/\d+&pno=(\d+)$/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($uid =~ /\d+&pno=(\d+)$/) 0 0 0
224 0 ▶ push @pages,$uid unless $1 eq '1';
Branch: 1 branch
BranchTrueFalseTotal
unless $1 eq 1 0 0 0
225 }
226 }
227  
228 # pages
229 0 foreach my $p (@pages) {
230 0 ▶ unless ($self->{agent}->get('/'.$p)) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"agent"}->get('/' . $p)) 0 0 0
231 0 croak "cannot get $p";
232 }
233 0 my $users_page = $self->parse_friends();
234 0 %{$users} = (%{$users},%{$users_page});
235 }
236  
237 0 return $users;
238 }
239  
240 =head2 parse_friends
241  
242 parse html page for friends uids
243 helper for follow friends
244 used after GET FriendList
245  
246 =cut
247 0 ▶sub parse_friends {
Subroutine parse_friends: not called CC=4 SCAR=30.0
Pod documented
248 0 my $self = shift;
249 0 my %users;
250 0 my @links = $self->{agent}->find_all_links(url_regex=> qr/FriendsList.*uid/);
251 0 foreach my $l (@links) {
252 0 ▶ next if $l->[1] =~ m/IMG/;
Branch: 1 branch
BranchTrueFalseTotal
if $l->[1] =~ /IMG/ 0 0 0
253 0 ▶ next if $l->[0] =~ m/\d+&pno=\d+/;
Branch: 1 branch
BranchTrueFalseTotal
if $l->[0] =~ /\d+&pno=\d+/ 0 0 0
254 0 my $uid= $l->[0];
255 0 $uid =~ s/.*uid=(\d*).*/$1/;
256 0 $users{$uid}=encode_entities_numeric($l->[1]);
257 }
258  
259 0 return \%users;
260 }
261  
262 =head2 get_friendsfriends (n)
263  
264 iterate n times over found uids to find more friends
265 more than n=1 seems insane, unlikely to work
266 don't let your script crash in this function, WWW::Mechanize may decide to die if orkut.com gets one of its server failures
267 FIXME: logout/login all 50 requests may help
268  
269 =cut
270 0 ▶sub get_friendsfriends {
Subroutine get_friendsfriends: not called CC=6 SCAR=37.4
Pod documented
271 0 my $self=shift;
272 0 my $n = shift;
273 0 my %friends;
274 my %lookup;
275  
276 0 ▶ for (my $i=0;$i<$n;$i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $n) 0 0 0
277 0 %friends = undef;
278 0 %friends = %{$self->{users}};
279 0 foreach my $u (keys %friends) {
280 0 ▶ next if $lookup{$u};
Branch: 1 branch
BranchTrueFalseTotal
if $lookup{$u} 0 0 0
281 0 $lookup{$u}+=1;
282 0 ▶ unless ( $self->{agent}->get("/FriendsList.aspx?uid=".$u) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"agent"}->get('/FriendsList.aspx?uid=' . $u)) 0 0 0
283 0 croak "cannot get FriendList of $u.";
284 }
285 0 my $users = $self->follow_friends();
286 0 foreach (keys %{$users}) {
287 0 $self->{users}{$_} = $users->{$_};
288 }
289 }
290 }
291 }
292  
293 =head2 get_xml_profile (uid)
294  
295 return profile of uid as simple xml
296  
297 =cut
298 0 ▶sub get_xml_profile {
Subroutine get_xml_profile: not called CC=4 SCAR=30.0
Pod documented
299 0 my $self = shift;
300 0 my $uid = shift;
301 0 my $xml;
302  
303 # get his profile
304 0 $self->{agent}->get("/Profile.aspx?uid=".$uid);
305 0 foreach ('relationship_status', 'birthday', 'age', 'here_for', 'children', 'ethnicity', 'political_view', 'humor', 'sexual_orientation', 'fashion', 'smoking','drinking','living', 'passions', 'sports', 'activities', 'books', 'music', 'tv_shows', 'movies', 'cuisines', 'email', 'country', 'IM', 'home_phone', 'address_line_1', 'address_line_2', 'webpage') {
306 0 my $tag = $_;
307 0 $tag =~ s/_/ /g;
308 0 ▶ if ( $self->{agent}->content() =~ m!>$tag:</td><td>(.*?)</td>!x) { $xml .= xml($tag,$1); }
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"agent"}->content =~ m[>$tag:</td><td>(.*?)</td>]x) 0 0 0
309 }
310  
311 # get his karma
312 0 ▶ if ( $self->{agent}->content() =~ m!lblKarma">.*?img/i_t(\d).*img/i_c(\d).*img/i_h(\d).*<b>(\d+)</b>!) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"agent"}->content =~ m[lblKarma">.*?img/i_t(\d).*img/i_c(\d).*img/i_h(\d).*<b>(\d+)</b>]) 0 0 0
313 0 $xml .= xml('trust',$1);
314 0 $xml .= xml('cool',$2);
315 0 $xml .= xml('hot',$3);
316 0 $xml .= xml('fans',$4);
317 }
318 0 return $xml;
319 }
320  
321 =head2 get_xml_communities (uid)
322  
323 return communities of uid as simple xml
324  
325 =cut
326 0 ▶sub get_xml_communities {
Subroutine get_xml_communities: not called CC=2 SCAR=17.9
Pod documented
327 0 my $self = shift;
328 0 my $uid = shift;
329 0 my $xml;
330  
331 # get his communities
332 0 $self->{agent}->get("/ProfileC.aspx?uid=".$uid);
333 0 my @comm = $self->{agent}->find_all_links(url_regex=> qr/Community.aspx?/);
334 0 my @fcomms;
335 0 foreach my $c (@comm) {
336 0 push @fcomms, encode_entities_numeric($c->[1]);
337 }
338 0 $xml .= xml('communities',join ',',@fcomms);
339 0 return $xml;
340 }
341  
342 =head2 get_xml_friendslist (uid)
343  
344 return friendslist of uid as simple xml
345  
346 =cut
347 0 ▶sub get_xml_friendslist {
Subroutine get_xml_friendslist: not called CC=3 SCAR=24.8
Pod documented
348 0 my $self = shift;
349 0 my $uid = shift;
350 0 my $xml;
351  
352 # similiar to 'get his friends'
353 # get first page
354 0 ▶ unless ($self->{agent}->get("/FriendsList.aspx?uid=".$uid)) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"agent"}->get('/FriendsList.aspx?uid=' . $uid)) 0 0 0
355 0 croak "cannot get FriendsList.aspx?uid=$uid";
356 }
357  
358 0 my @fuids;
359 0 my $users = $self->follow_friends();
360 0 foreach (keys %{$users}) {
361 0 push @fuids, $_;
362 }
363  
364 0 $xml .= xml('friends',join ',',@fuids);
365 0 return $xml;
366 }
367  
368  
369 # Autoload methods go after =cut, and are processed by the autosplit program.
370  
371 1 1;
372 __END__
↑ Summary