#!/usr/bin/perl
 use WWW::Mechanize;
 use HTTP::Cookies;
 
 # Load up our URL
 $url  = 'http://myspace.com/index.cfm?fuseaction=splash';
 $user = 'jibjabjam@yahoo.com';
 $pass = 'badpass!';
 
 # Build the Bot and set the user agent
 my $bot = WWW::Mechanize->new(autocheck => 1);
 $bot->agent_alias( 'Windows Mozilla' );
 
 
 $cookie = HTTP::Cookies->new( file => '/root/work/programming/cookie.txt', autosave => 1);
 $bot->cookie_jar($cookie);
 
 
 # Access the initial url and print the content
 $bot->get($url);
 $bot->form_name('theForm');
 $bot->field('email', $user);
 $bot->field('password', $pass);
 $bot->click("Submit22");
 
 
 # Sometimes Myspace pops up advertisements,
 # check to see if we're on an ad, and if so
 # skip through to the main page.
 if ($bot->find_link( text_regex => qr/Skip this Advertisement/i))
         {
         $bot->follow_link( text_regex => qr/Skip this Advertisement/i);
         }
 
 
 # We're logged in, lets get into the Friends List
 $bot->follow_link( text => "View All of My Friends" );
 
 # Now get a list of all the Friends here
 @friend_links = $bot->find_all_links( url_regex => qr/user.viewprofile/i);
 
 my @friend_addresses;
 foreach (@friend_links)
         {
         if ($_->text() eq '[IMG]') { print "[IMG]\n"; }
         elsif ($_->text() =~ /Back To .* Profile/i) { print "link-back\n"; }
         else
                 {
                 print "We have a friend named : ";
                 print $_->text();
                 print "\n";
                 push (@friend_addresses, $_->url());
                 }
         }
 # Now go harvest some of Tom's Friends
 foreach(@friend_addresses)
         {
         $bot->get($_);
         $bot->follow_link( url_regex => qr/user.viewfriends/i );
         @Toms_Friends = $bot->find_all_links( url_regex => qr/user.viewprofile/i);
         $out = "list.txt";
 
         foreach (@Toms_Friends)
                 {
                 # Don't save duplicate image links
                 if ($_->text() eq '[IMG]') { }
                 # Don't save the link back to the users profile
                 elsif ($_->text() =~ /Back To .* Profile/i) {  }
                 # Do save a single copy of the link to the friends profile  
                 else
                         {
                         push (@friend_addresses, $_->url());
                         print $_->text(); print "\t\t\t"; print $_->url; print "\n";
                         }
 
                 }
         print "\n------------------------------------------------------------------------------\n";
         }
