Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Need help on this script on how to configure for other csv files

Status
Not open for further replies.

Aidin

IS-IT--Management
Jul 18, 2002
18
US
#!/usr/local/bin/perl

########################################
# VERSION: 1.2
# DATE: 01/16/2002
# This script accepts a filename
# and a configuration file name as
# arguments, parses the file, and sends
# the URL-encoded parameters to the URL
# mentioned in the configuration file
########################################

use strict;

########VARIABLES SECTION STARTS########
# This is a name of the config file

my %second_url_configs = ();
$second_url_configs{'second_url_value'} = '
if($ARGV[1] eq 'lost'){
$second_url_configs{'second_url_value'} = '}
if($ARGV[1] eq 'batch'){
$second_url_configs{'second_url_value'} = '}
$second_url_configs{'second_url_enabled'} = 1;
$second_url_configs{'second_url_emails'} = 'aidin@inkjetorder.com';

my $directory = $ARGV[0];

alert("Invalid order directory specified on command line: $directory") unless ((-r $directory) && (-d $directory));

opendir(DIR, $directory) || alert("Can't open directory - $directory: $!");
my @files = grep { /$\.csv/ && -f "$directory/$_" } readdir(DIR);
closedir DIR;

foreach my $file (@files){
# This is a name of a CSV-file to be parsed and sent
my $file_name = "$directory/$file";
print("Processing: $file_name\n");

alert("Can't open/read CSV data file: $file_name") unless (-r $file_name);

my ($data, $dataend, $query_string, $user_agent, $request, $res, $vhost);
my $secondary_url = $second_url_configs{'second_url_value'};
my $CCN = "";
my @strings = ();
my @fields = ();
my @data;
my @dataend;

#
########VARIABLES SECTION ENDS##########

########MAIN PROGRAM SECTION STARTS#####
#
if($second_url_configs{'second_url_enabled'}){

use LWP::UserAgent;
use HTTP::Request;

open(DATA, &quot;<$file_name&quot;) || alert(&quot;Can't open/read CSV data file: $file_name&quot;);
undef $/;
$data = <DATA>; # read in whole file
$/ = &quot;\n&quot;; # back to normal input delim
close(DATA);

@strings = split(&quot;\n&quot;, $data);

chomp $strings[0];

if($strings[0] =~ /^\&quot;/){
#Regular orders section...

$strings[0] =~ s/^\&quot;//;
$strings[0] =~ s/\&quot;$//;

@fields = split(/\&quot;,\&quot;/, $strings[0]);
for(my $i=0; $i<@fields; $i++){
# $fields[$i] =~ s/^\d\d?\-//;
# $fields[$i] =~ s/\.//g;
$fields[$i] =~ s/ /_/g;
}

$user_agent = new LWP::UserAgent;

for(my $i=1; $i<@strings; $i++){

chomp $strings[$i];

$strings[$i] =~ s/^\&quot;//;
$strings[$i] =~ s/\&quot;$//;

@data = split(/\&quot;,\&quot;/, $strings[$i]);

if(@data){

$query_string = &quot;&quot;;

for(my $j=0; $j<@fields; $j++){
$data[$j] = urlencode($data[$j]);
$query_string .= &quot;$fields[$j]=$data[$j]&&quot;;
}

if($CCN){ $query_string .= &quot;CCN=$CCN&quot;; }

$query_string = ($secondary_url =~ /\?/)?
&quot;$secondary_url&$query_string&quot;:
&quot;$secondary_url?$query_string&quot;;

$request = HTTP::Request->new(GET => $query_string);
$res = $user_agent->request($request);

print &quot;In1's response:\n\n&quot;.$res->content.&quot;\n\n----------------------------------------\n&quot;;


alert(&quot;This URL failed to work: $query_string.&quot;) unless($res->is_success);
}
}

}elsif($strings[0] !~ /\&quot;$/){
#Wrong CSV-format...

alert(&quot;Wrong CSV format.&quot;);

}else{
#Lost or Batch orders section...

$user_agent = new LWP::UserAgent;

for(my $i=0; $i<@strings; $i++){

chomp $strings[$i];

$strings[$i] =~ s/^(.*?)\.\&quot;//;
$vhost = $1;

$strings[$i] =~ s/\&quot;$//;

@data = split(/\&quot;,\&quot;/, $strings[$i]);

my $ct_fields = @data;

if($ct_fields < 50)
{
if($ct_fields == 45)
{
@dataend = splice(@data,45);
push(@data,@dataend);
$ct_fields = @data;
}
}

if(@data && $ct_fields > 49){

$query_string = &quot;host=&quot;.urlencode($vhost).&quot;&&quot;;

for(my $j=0; $j<@data; $j++){
$data[$j] = urlencode($data[$j]);
$query_string .= &quot;field$j=$data[$j]&&quot;;
}

if($CCN){ $query_string .= &quot;CCN=$CCN&quot;; }

$query_string = ($secondary_url =~ /\?/)?
&quot;$secondary_url&$query_string&quot;:
&quot;$secondary_url?$query_string&quot;;

# print &quot;\nquery_string=$query_string\n&quot;;

$request = HTTP::Request->new(GET => $query_string);
$res = $user_agent->request($request);

print &quot;In1's response to lost or batch order:\n\n&quot;.$res->content.&quot;\n\n----------------------------------------\n&quot;;

alert(&quot;This URL failed to work: $query_string.&quot;) unless($res->is_success);

}
}
}
}
}


#
########MAIN PROGRAM SECTION ENDS#######

########SUBROUTINES SECTION STARTS######
#
sub urlencode {
my($val)=@_;
$val=~s/([^a-zA-Z0-9_\-.])/uc sprintf(&quot;%%%02x&quot;,ord($1))/eg;
return $val;
}


sub alert {
my ($message) = @_;
my $subject = &quot;Secondary URL mechanism ERROR&quot;;
my @addresses = split(/\,/, $second_url_configs{'second_url_emails'});
foreach my $address (@addresses){
&send_mail($addresses[0], $address, $subject, $message.&quot;\n\nSecondary URL mechanism was aborted for the order.\n&quot;);
}
exit;
}


sub send_mail {

my ($from, $to, $subject, $message) = @_;
my ($found, $mail_prog);

if(-e &quot;/usr/bin/sendmail&quot;){
$found = 1;
$mail_prog = &quot;/usr/bin/sendmail -t&quot;;
}elsif(-e &quot;/usr/sbin/sendmail&quot;){
$found = 1;
$mail_prog = &quot;/usr/sbin/sendmail -t&quot;;
}elsif(-e &quot;/sbin/sendmail&quot;){
$found = 1;
$mail_prog = &quot;/sbin/sendmail -t&quot;;
}elsif(-e &quot;/bin/sendmail&quot;){
$found = 1;
$mail_prog = &quot;/bin/sendmail -t&quot;;
}elsif(-e &quot;/usr/lib/sendmail&quot;){
$found = 1;
$mail_prog = &quot;/usr/lib/sendmail -t&quot;;
}

if(!$found){ die &quot;Can't find a mailing program&quot;; }

open (MAIL, &quot;|$mail_prog&quot;) || die &quot;Could Not Open Mail Program&quot;;

print MAIL <<__END_OF_MAIL__;
To: $to
From: $from
Subject: $subject

$message

__END_OF_MAIL__

close (MAIL);
}

#
########SUBROUTINES SECTION ENDS########

1;
 
Aidin, Hi.

Is there a particular part of this script you're having a problem with? Mike
________________________________________________________________

&quot;Experience is the comb that Nature gives us, after we are bald.&quot;

Is that a haiku?
I never could get the hang
of writing those things.
 
Well basicaly what this script is supposed to do is when i run this command:

./post_orders.pl ./(folder name) batch

its supposed to look at the csv file and launch the url:

'
What i need to do is modify this file to read another csv file, but i dont know enough perl to modify it to suit my needs. Please let me know if you can help
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top