#!/usr/bin/perl
use WWW::Mechanize;
use HTTP::Cookies;

# Mechanize Configuration Variables
#aarons userID = 14796603
my $UseragentAlias = "Windows Mozilla";
my $url;
my $CurrentID;
my @UserIDs;
my $DEBUG = 0;

# Username/Password
$user = 'jibjabjam@yahoo.com';
$pass = 'badpass!';

# Connect
# We need to bless $bot as a WWW::Mechanize object
# in order for the various subroutines to correctly
# utilize the bot.
my $bot = &MyspaceConnect($user, $pass);
bless $bot, 'WWW::Mechanize';

# Get test profile
&GetProfile('14796603');

# Get his friends list
&GetFriends();

foreach (@UserIDs){
    print "UserID - $_ \n";
}

#Subroutine to Build the Mechanize bot and login to myspace.
sub MyspaceConnect($$)
{      
#Get the username and Password from arguments
my ($username, $password) = @_;

# Build the Bot
$url  = 'http://myspace.com/index.cfm?fuseaction=splash'; 
my $bot = WWW::Mechanize->new(autocheck => 1);
$bot->agent_alias( $UseragentAlias );
# Initialize the Cookie Jar.
#I'm storing it in cookie.txt right now for testing
# purposes, once I've got it all together
# I'll use a temporary memory-only cookie jar
my $cookie = HTTP::Cookies->new( file => 'cookie.txt', autosave => 1);
$bot->cookie_jar($cookie);

print "**MyspaceConnect\tBot Built, Logging in \n" if ($DEBUG);
# Log-In
$bot->get($url) or die "WTF!!! : $!\n";
$bot->form_name('theForm');
$bot->field('email', $username); 
$bot->field('password', $password);
$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);
	} 
#bless $bot, 'WWW::Mechanize';
print "**MyspaceConnect\tLogged in \n" if ($DEBUG);
return $bot;
}



#Subroutine to retrieve a specific user profile once
#we're logged in. The myspace userid is passed to
#the subroutine and it fetches the page with $bot
sub GetProfile ($)
{ 
    ($CurrentID) = @_;
    $url = join '', 'http://myspace.com/index.cfm?fuseaction=user.viewprofile&friendID=', $CurrentID;
    $bot->get($url);
    print "**GetProfile\tProfile Fetched\n" if ($DEBUG);
}

#GetFriends gets a list of the current users
#friends from their profile page. If passed no
#argument it uses whatever profile page the
#bot is currently holding. If passed a userID
#it calls GetProfile and then proceeds to get
#the friends list. 
sub GetFriends($)
{  
    # http://profile.myspace.com/index.cfm?fuseaction=user.viewprofile&friendID=14883149
    # If passed an ID, then call getprofile
    ($CurrentID) = @_;
    if ($CurrentID) { GetProfile($CurrentID); }
    
    # Go to the "My Friends" page and get all of
    # the links to user profiles. Right now we're
    # just grabbing the first page. Iterating through
    # all the pages will come later
    $bot->follow_link( text => "View All of My Friends");
    @friend_links = $bot->find_all_links(url_regex => qr/user.viewprofile/i);
    foreach (@friend_links)
    {
	#Strip out duplicates from Image links
	if ($_->text() eq '[IMG]') { }
	#Strip out the self-referencing link
	elsif($_->text() =~ /Back To .* Profile/i) {}
	#Otherwise, record the userid in @UserIDs
	else
	{ 
	$CurrentID = $_->url();
	$CurrentID =~ s/http.*friendID=//gi;
	push (@UserIDs, $CurrentID);    
	}  
    }
}