[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [Omaha.pm] LWP



#!/usr/local/bin/perl
#
# Program:	askedi.pl [put/get] [trading partner id] [file param]
[optional date param]
# Date: 	07/20/2006
# Last Update: 	08/18/2006
# Author: 	Chris Hefling
# Synopsis:     Put/Get files for ASK-EDI 
#
# Read Trading Partner username and password from conf/[tp number].cnf
#

# Load Libraries
#
use strict;
use Crypt::SSLeay;
use LWP::UserAgent;
use LWP::Simple;
use HTTP::Request::Common qw(POST);
use HTTP::Cookies;

#
# Initialize Variables
#
my $content;

# Login Variables
my $cookie_file;
my $login_URL;
my $username;
my @username;
my $password;
my @password;

# Script Argument Variables
my $arg1=@ARGV[0];	
my $arg2=@ARGV[1];	
my $arg3=@ARGV[2];	
my $arg4=@ARGV[3];	

# File Counter Variables
my $cnt;
my $total;

# Logging Variables
my $logfile="";
my $text;
my $ecode;
my $logfile="/cie/egate/client/scripts/askedi/log/askedi.log";
my $lockfile="/cie/egate/client/scripts/askedi/lock.txt";

# Get|Put Variables
my $temp_file;
my $recieve_disp_page;
my $getfiles_URL;
my $recv_dir="/cie/egate/client/scripts/askedi/files";
my $tempdir="/cie/egate/client/scripts/askedi/temp";

# Mail Variables
my $message="";
my $mailto = "Chris.Hefling\@nmhs.org";
my $subject = "Claims File $arg1 Error";

# Date Variables
my $rdate;
my ($sec,$min,$hour,$mday,$mon,$year, $wday,$yday,$isdst) = localtime
time;
$year=$year + 1900;
$mon=$mon + 1;
if("$mon" < 10){
 $mon="0$mon";
}
if("$mday" < 10){
 $mday="0$mday";
}

my $cdate="$year$mon$mday";

# Check arg4 for manual filedate to use instead of todays date
# Format of (yyyymmdd)
#
if("$arg4" ne ""){
 $cdate="$arg4";
}

#
# Start Sub Routines
#

# Mail Routine
sub smail {
 unless(open (MAIL, "|/usr/sbin/sendmail -t")) {
 warn "Error starting sendmail: $!";
} else {
 print MAIL "From: no.reply\@nmhs.org\n";
 print MAIL "To: $mailto\n";
 print MAIL "Subject: $subject\n\n";
 print MAIL "$message";
 close(MAIL) || warn "Error closing mail: $!";
 }
}

#
# Log routine 
sub print_log(){
 if("$ecode" eq "1" || "$ecode" eq "2"){
  $message="$text";
  smail();
 }
 $rdate="$mon/$mday/$year $hour:$min:$sec";
 print LOG "$rdate $text\n";
}
#
# End Sub Routines

#
# Start Application
#

# Open log file
#
open LOG, ">>$logfile" or die "can't open $logfile $!";

if (-e "$lockfile") {
 $text="A runtime lock exists because of a previous failed login. The
file \"lock.txt\" must be deleted for this program to run.";
 $ecode="1";
 print_log();
 print "$ecode";
 exit(1);
}
#
# Check for command line arguments
#
if("$arg1" eq "" || "$arg2" eq ""){
 print "#\n";
 print "# Usage: perl $0 [get|put] [trading partner ID] [optional]\n";
 print "#\n\n";
 print "1";
 exit(1);
} 
if("$arg1" eq "put" && "$arg3" eq ""){
 print "#\n";
 print "# Usage: perl $0 [get|put] [trading partner ID] [param]\n";
 print "#\n\n";
 print "1";
 exit(1);
}
if("$arg1" ne "get" && "$arg1" ne "put"){
 print "#\n";
 print "# Usage: perl $0 [get|put] [trading partner ID] [optional]\n";
 print "#\n\n";
 print "1";
 exit(1);
} 
#
# Test for the existance and readability of the config file
# Load username and password from config file
#
my $confdir="/cie/egate/client/scripts/askedi/conf";
if ((-e "$confdir/$arg2.cnf") && (-r "$confdir/$arg2.cnf") && (-s
"$confdir/$arg2.cnf")){
 open(CONF, "<$confdir/$arg2.cnf");
 while(<CONF>) {  
  my($line) = $_; 
  if(/^username/){
   @username=split('=', $line);
   $username=@username[1];
   $username=~s/\"//g;
   $username=~s/\;//g;
   chomp $username;
  }
  if(/^password/){
   @password=split('=', $line);
   $password=@password[1];
   $password=~s/\"//g;
   $password=~s/\;//g;
   chomp $password;
  }
 }
   $text="Configuration loaded for $arg1:$arg2:$arg3"; 
   print_log();
 } else {
   $text="Problems with the $arg2 configuration file, exiting program";
   $ecode="1";
   print_log();
   print "$ecode";
   exit(1);
 }
#
# START LOGIN PROCESS
#
  my $ua = LWP::UserAgent->new(env_proxy => 1, keep_alive => 1, timeout
=> 30, );
  my $cookie_jar = HTTP::Cookies->new(file =>
"/cookies/ask-edi-cookies.txt", autosave => 1,     ignore_discard => 1);
  $ua->cookie_jar( $cookie_jar);
  $ua->agent('Mozilla/5.0');
  my $url = 'https://clyde.bcbsks.com/askedi/filetransfer';
  my $req = POST $url, ['username' => $username, 'password' =>
$password, 'link' => '', 'command' => 'logon'];
  my $res = $ua->request($req);
  $cookie_jar->extract_cookies($res);
  if ($res->is_success) {
   if ($res->as_string !~ /failed/){
    #print $res->as_string;
    $text="$username logged in.";
    print_log();
   } else {
    $text="$username login failed.";
    open LOCK, ">$lockfile" or die "can't open $lockfile $!";
    close(LOCK);
    $ecode="1";
    print_log();
    print "$ecode";
    exit(1);
 }
}
#
# END LOGIN PROCESS
#
# START FILE GET PROCESS
#
if ("$arg1" eq "get"){
 my $req = HTTP::Request->new(GET =>
'https://clyde.bcbsks.com/askedi/jsps/ask_tp_receive.jsp');
 #$cookie_jar->add_cookie_header($req);
 $res = $ua->request($req);
 unless(open(TEMP_WRITE, ">$tempdir/tempfile.txt")){
  $text="Can not write to temp file, exiting program.";
  $ecode="1";
  print_log();
  print "$ecode";
  exit(1);
 }
# Write page to temp file so we can read in the data
   print TEMP_WRITE $res->content;
# Read the saved page so we can create a file list
  unless(open(TEMP_READ, "$tempdir/tempfile.txt")){
     $text="Can not read from temp file, exiting program.";
     $ecode="1";
     print_log();
     print "$ecode";
     exit(1);
   }
 my $line="";
 my $file="";
 my @files="";
 my @files1="";
# Loop through the data
 while (defined ($line = <TEMP_READ>)) {
  if ($line =~ /GetFile/){
   # Lets remove some of the erroneous characters so we can create an
array 
   $line=~s/'//g;
   $line=~s/\(//g;
   $line=~s/\)//g;
   $line=~s/;">//g;
   @files=split(/GetFile/,$line);
   @files1=split(/,/,$files[1]);
   if("$files1[0]" eq "sIFName"){
    # Do nothing, we do not need the first line
   } else {
     $total++;
    # Open a file so we can write the data to it
    # remove space before filename
    $files1[1]=~s/ //g;
  open(TESTIT, ">$recv_dir/testfile.txt");
    unless(open(FILE, ">$recv_dir/$files1[1]")){
     $text="Can not open $recv_dir/$files1[1], exiting program.";
     $ecode="1";
     print_log();
     print "$ecode";
     exit(1);
   }
    # Send the request to retrieve the file
    $req = HTTP::Request->new(GET =>
'https://clyde.bcbsks.com/askedi/filetransfer');
    #$cookie_jar->add_cookie_header($req);
    $req = POST $url, ['command' => 'fileget', 'ifilename' =>
$files1[0], 'xfilename' => $files1[1]];
    $req->content_type('application/x-www-form-urlencoded');
    #$req->header('Content-Type' => 'text/html; charset=UTF-8');
    $res = $ua->request($req);
    $content=$res->content;
    print TESTIT $content;
    # Write the receiving file to our local server
    $cnt++;
    #unless(print FILE $res->as_string){
    unless(print FILE $res->content){
     $text="Can not write recieving data to file.";
     $ecode="1";
     print_log();
     print "$ecode";
     exit(1);
    }
    $text="$cnt Writing $files1[1]";
    print_log();
   }
  }
 }
 $text="$total files received $cnt files written";
 print_log();
}
#
# END FILE GET PROCESS
#
# START FILE PUT PROCESS
#
  if ("$arg1" eq "put"){
   #print "Start the file put process.\n";
   # Access the putfiles files page so we can send the file
   my $GS_URL    = 'https://clyde.bcbsks.com/askedi/filetransfer';
   my $fdir="/cie/data/Claim_Cycle/transmit/medbne_prof/";
   my $adir="/cie/data/Claim_Cycle/transmit/medbne_prof/archive/";
   my $afile = $adir . "medbne_prof_" . $arg3 . "_" . $cdate . ".dat";
   my $file = $fdir . "medbne_prof_" . $arg3 . "_" . $cdate . ".dat";
   if ("$arg3" ne ""){
    if (-e "$file") {
     $text="$cdate file exists, continue processing.";
     print_log();
    } else {
     $text="$cdate file does not exist, exiting program.";
     $ecode="1";
     print_log();
     print "$ecode";
     exit(1);
    }
   } else {
   $text="File parameter not supplied on command line.";
   $ecode="1";
   print_log();
   print "$ecode";
   exit(1);
   }
   my $request = POST($GS_URL, 
	Content_Type => 'form-data', Content => ['filename' =>
[$file,$file], 'command' => 'fileput', 'transmit' => 'Transmit', 'name'
=> 'frmtransmit']);
   my $response = $ua->request($request); die "request failed" unless
$response->is_success;
   my $record = $response->content;
   $text="$file successfully transmitted.";
   print_log();
   rename $file, $afile;
    if (-e "$afile") {
     $text="$afile has been archived.\n";
     print_log();
    } else {
     $text="$afile failed to archive.\n";
     $ecode="2";
     print_log();
     print "$ecode";
     exit(2);
    }
  }
#
# END FILE PUT PROCESS
#
# END PROGRAM
# 
print "0";



This message and any included attachments are from Nebraska Methodist Health System and its affiliates and are intended only for the addressee. The message may contain privileged, confidential and/or proprietary information intended only for the person(s) named.  Unauthorized forwarding, printing, copying, distribution, or use of such information is strictly prohibited and may be unlawful. If you are not the addressee, please promptly delete this message and notify the sender of the delivery error by e-mail or you may call Nebraska Methodist Health System and its affiliates in Omaha, Nebraska, U.S.A at (402)354-2280.