Subject: | add_friends sample script desired functionality |
the example add_friends script is almost perfect for my purposes,
except that I don't want to have to manage captcha requests as they
come up... In trying to create my own script (close to successful, but
I can't seem to get the module to return FC on captcha detection) I
was trying to make it simply sleep for 12 hours and retry the last
friend add... I would like add_friends to do that instead of showing
the URL to the manual friend add... How is this accomplished? I've
provided the script that I wrote that was nearly working... (also, is
there a nice way to provide some kind of feedback on how long the
script has left to sleep... I couldn't seem to figure that out
either...) Thanks for all your help so far...
#!/usr/bin/perl -w
use strict;
use Data::Dumper;
use WWW::Myspace;
use WWW::Myspace::FriendAdder;
use List::Compare;
$|++;
my $username = edited; # string
my $password = edited; # string
my $cacheDir = $ENV{'HOME'} . "/.www-myspace"; #
string default is "$ENV{'HOME'}/.www-myspace"
my $cacheFile = 'login_cache'; #
string default is "login_cache"
my $autoLoginToggle = '1'; #
[0|1] default is "1"
my $excludeMyFriendsToggle = '0'; #
[0|1] default is "0"
my $interactiveToggle = '1'; #
[0|1] default is "1"
my $maxRequests = '250000'; #
integer default is "50"
my $profileTypes = 'all'; #
[band|personal|all] default is "all"
my $randomSleepToggle = '1'; #
[0|1] default is "0"
my $randomSleepMax = '30'; #
integer default is "10"
my $defaultSleepTimer = '43200'; #
integer recommend a minimum of 43200 (12 hours)
my $friendListFile = 'friendlist.txt';
my @newFriendsRough;
my @newFriendsSuccess;
my %newFriendsFailure;
my $openLinkToggle = '0'; #
[0|1] default is "0"
my $webBrowser = '/usr/bin/firefox';
##########
# do not edit below this line
##########
# decide whether or not the webbrowser should be opened when CAPTCHA
is detected
#print ("\n====================\nOpen CAPTCHA images in your web
browser?\n[y/N] > ");
#$_ = <STDIN>;
#if ( /[Yy]/ ){
# $openLinkToggle = '1';
# print ( "\n====================\nCAPTCHA links will be opened
in web browser located at $webBrowser.\n====================\n" );
#}
#else {
# $openLinkToggle = '0';
# print ( "\n====================\nCAPTCHA links will not be
opened.\n====================\n" );
#}
# grab user-defined variables
my %myspace_params = (
account_name => $username,
password => $password,
cache_dir => $cacheDir,
cache_file => $cacheFile,
auto_login => $autoLoginToggle
);
my %friendadder_params = (
exclude_my_friends => $excludeMyFriendsToggle,
interactive => $interactiveToggle,
max_count => $maxRequests,
profile_type => $profileTypes,
random_sleep => $randomSleepToggle,
sleep => $randomSleepMax
);
# create the cache directory if it doesn't already exist
unless (-e $cacheDir) {
my $tempspace = WWW::Myspace->new(
\%myspace_params,
);
print ( "FriendAdder cache directory does not exist. Creating
in selected location ... " );
$tempspace->make_cache_dir;
print ( "Succeeded.\n" );
}
# establish connection to your Myspace account
my $myspace = WWW::Myspace->new(
\%myspace_params,
);
# get prepared to add new friends to your Myspace account
my $friendadder = WWW::Myspace::FriendAdder->new(
$myspace,
\%friendadder_params,
);
# read list of friends from user-provided friendlist.txt file or die
trying
# format of friendlist.txt should be
friendID_1<newline>friendID_2<newline> ... friendID_n
open FRIENDLIST, $friendListFile or die ( "Friend List file
$friendListFile is not found or could not be opened for read access.\t
$!\n" );
while ( my $line = <FRIENDLIST> ) {
my $linePretty = $line;
$linePretty =~ s/(\r\n?|\n)//g; # clean up the extra
line endings
print ( "\nPoaching friends from $linePretty ... \n" );
push ( @newFriendsRough, $myspace->friends_from_profile
($line) );
print ( "\t\t\t\t ... Completed!\n" );
}
close FRIENDLIST;
# check for duplicate entries in @newFriendsRough and remove existing
friends
print ( "Gathering your friends ... \n" );
my @currentFriends = $myspace->get_friends;
print ( "\t\t\t\t ... Completed!\n" );
my $friendcompare = List::Compare->new(
{ lists =>
[\@currentFriends, \@newFriendsRough],
accelerated => 1
} );
my @newFriends = $friendcompare->get_complement;
my $totalNewFriends = printf('%9s', @newFriends);
# !!DEBUG!! a good something to try when troubleshooting
# print Dumper(@newFriends);
# let's add some friends, shall we?
foreach my $newFriend ( @newFriends ) {
print ( "Attempting to add friend $newFriend ... \n\n" );
#my $addStatus = $myspace->send_friend_request( $newFriend );
my $addStatus = $friendadder->send_friend_requests(
$newFriend );
if ( $addStatus =~ /^P/ ) {
print ( "\nSucceeded.\n" );
push ( @newFriendsSuccess, $newFriend );
} else {
print ( "\nFailed with code $addStatus.\n" );
$newFriendsFailure{ $newFriend } = $addStatus;
}
if ( $addStatus eq 'FC' ) {
print ( "\n\nCAPTCHA detected! Time to go to sleep ...
\n\n" );
sleep $defaultSleepTimer;
print ( "\n\n" );
# try to add friend again
print ( "Attempting to add friend $newFriend ... " );
#$addStatus = $myspace->send_friend_request(
$newFriend );
$addStatus = $friendadder->send_friend_requests(
$newFriend );
if ( $addStatus =~ /^P/ ) {
print ( "Succeeded.\n" );
push ( @newFriendsSuccess, $newFriend );
} else {
print ( "Failed with code $addStatus.\n" );
$newFriendsFailure{ $newFriend } = $addStatus;
}
# $friendadder->send_friend_request( $newFriend );
# system("$webBrowser $myspace->captcha &");
}
}
# final report
use Date::Simple;
my $date = d8( today() );
my $failureFile = $date . "-failurefile.log";
my $totalNewFriendsSuccess = printf( '%9s', @newFriendsSuccess );
my $totalNewFriendsFailure = printf( '%9s', keys( %
newFriendsFailure ) );
print ( "Unique IDs Attempted:\t\t$totalNewFriends\n\n" );
print ( "Friends Added Successfully:\t$totalNewFriendsSuccess\n" );
print ( "Friend Add Attempts Failed:\t$totalNewFriendsFailure\n\n" );
open ( FAILURELOG, '>', $failureFile );
while ( my ( $key, $value ) = each(%newFriendsFailure) ) {
print FAILURELOG "$key\t$value\n";
}
close FAILURELOG;
print ( "Failures and reason codes have been exported to
$failureFile\n" );