Skip to content

Instantly share code, notes, and snippets.

@bobtfish
Last active December 12, 2015 00:29
Show Gist options
  • Save bobtfish/4684515 to your computer and use it in GitHub Desktop.
Save bobtfish/4684515 to your computer and use it in GitHub Desktop.
#!/usr/bin/perl
use strict;
use warnings;
use LWP;
use HTTP::Cookies;
use Carp ();
$SIG{ALRM} = \&Carp::confess;
if (($#ARGV + 1) != 3) {
print "Usage: ./BulkForward.pl <sysadmin-username> <password> <environment-url> \n";
print "example: ./BulkForward.pl someadminusername somepassowrd https://timdev.youdevise.com/LATEST/ \n";
die "Incorrect number of arguments " . $#ARGV . $! . "\n";
}
my ($username, $password, $baseurl) = @ARGV;
my $authenticationUrl = $baseurl . "j_security_check";
my $forwardIdeaUrl = $baseurl . "monitor";
my $toPortfolio = 'ff8081813c7e8945013c86b3884b30b1';
my @fromPortfolios = ("4028ae8c330678db01331a6f121c4e95","ff80818138603415013899fbc3811c60","4028ae8a2c4580e8012c5fdcf759523b", "ff80818137400ce5013774f81012384a");
# my @fromPortfolios = ("4028ae8a2c4580e8012c5fdcf759523b");
print "username " . $username . "\n";
print "password " . $password . "\n";
print "base url : $baseurl \n";
print " authentication url : $authenticationUrl \n";
print " forward url: $forwardIdeaUrl \n ";
my @contributors = getContributors("contributor-names.txt");
print "Total number of contributors: " . scalar (@contributors) . "\n";
my $browser = configuredBrowser();
login($browser, $username, $password);
foreach (@contributors) {
print "Forwarding ideas for contributors " . $_ . "\n\n";
forwardContributorIdeas($browser, $_);
}
print "All done \n";
sub trim {
my $string = shift;
$string =~ s/^\s+//;
$string =~ s/\s+$//;
return $string;
}
sub getContributors {
my $filename = shift;
my ($FILE, @names);
open FILE, "<", $filename or die $!;
while (<FILE>) {
next if (/^\s+$/);
chomp;
push (@names, trim($_));
}
close(FILE);
return @names;
}
sub configuredBrowser {
my $browser = LWP::UserAgent->new;
$browser->cookie_jar( {} );
push @{ $browser->requests_redirectable }, 'POST';
$browser->timeout(12 * 60 * 60);
return $browser;
}
sub login {
my $browser = shift;
my $username = shift;
my $password = shift;
my $loginResponse = $browser->post( $authenticationUrl,
[ 'j_username' => $username,
'j_password' => $password
]
);
die "$authenticationUrl error: ", $loginResponse->status_line unless $loginResponse->is_success;
}
sub forwardNow {
my ($OUTPUT, $browser, $contributor, $fromPortfolio) = @_;
alarm(60); # Blow up if we take > 60 s
print $OUTPUT "Forwarding ideas of " . $contributor . " from portfolio: " . $fromPortfolio . " to portfolio: " . $toPortfolio . "\n\n\n";
my $forwardingResponse = $browser->post( $forwardIdeaUrl,
[ 'page' => 'com.youdevise.tradeideas.servlet.forwardideas.ForwardIdeasController',
'action' => 'FORWARD_IDEAS',
'FORWARD_AUTHOR_0' => '-- All authors --',
'FORWARD_COMPANY_0' => $contributor,
'FORWARD_FORWARD_FROM_0' => $fromPortfolio,
'FORWARD_FORWARD_TO_0' => $toPortfolio
]
);
die "$forwardingResponse error: ", $forwardingResponse->status_line unless $forwardingResponse->is_success;
print $OUTPUT $forwardingResponse->content;
print $OUTPUT "\n";
print $OUTPUT "****************************************************************\n";
alarm(0);
}
sub forwardContributorIdeas {
my ($browser, $contributor) = @_;
my $outputFileName = "forwardideas-output-" . $contributor . ".log";
open(my $OUTPUT, '>>', $outputFileName) or die "Could not open log file for writing' $outputFileName $!";
foreach (@fromPortfolios) {
forwardNow($OUTPUT, $browser, $contributor, $_);
}
close $OUTPUT;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment