skip to main | skip to sidebar

Perl Programming Language Tutorial

Pages

  • Home
 
  • RSS
  • Twitter
Tuesday, October 16, 2012

NASDAQ Status checker using Perl

Posted by Raju Gupta at 9:48 AM – 0 comments
 

Perl script to check the nasdaq trader website to check for system/ipo updates and send email


 $| = 1;
use strict;
use Date::Calc qw( Today_and_Now );

use CGI qw( :all );

use HTML::Tree;
use HTML::TableExtract;
use LWP::UserAgent;


my $app = $ARGV[0];
my %alertStatus = ('url' => 0);
my $severity = 0;
my $desc = '';
my $from = 'NASDAQ';
my $alertCounter = 1;

my ($year,$month,$day,$hour,$min,$sec) = Today_and_Now();
my $today = sprintf ("%4s-%02s-%02s-%02s:%02s:%02s",$year,$month,$day,$hour,$min,$sec);

my $proxy = $ENV{$app."_proxy"};

my $url = $ENV{$app."_url"};
$url = 'http://www.nasdaqtrader.com/Trader.aspx?id=MarketSystemStatus' if(!$url);

my $sleep = $ENV{$app."_sleep"};
$sleep = 50;

my %alertHash = (
     'sr' => 'ExchangeAlert',
     'id' => 'NASDAQ',
     'monType' => 'Alert',
     'e' => $region."iedmon",
     'URL:NASDAQ_URL' => $url
    );

print STDERR "
checkNASDQAStatus: $today: Starting checkNASDQAStatus
";
print STDERR "checkNASDQAStatus: proxy - $proxy, URL - $url, check interval - $sleep
";
print STDERR "checkNASDQAStatus: -------------------------------------------------------
";

my $ua = LWP::UserAgent->new;
$ua->proxy(['http'], $proxy );

my $te = new HTML::TableExtract( headers => [qw( Date Time Status )] );

while( 1 ){
 checkNASDAQStatus();

 print STDERR "sleeping...
";
 sleep 55;
}

sub checkNASDAQStatus{
 my $request = new HTTP::Request(GET=>$url);
 my $response = $ua->request( $request );

 if( $response->{_rc} !~ /^2dd/ ){
  if( $alertStatus{'url'} == 0 ){
   $severity = 5;
   $desc = "Request to NASDAQ URL - $url failed.  Return code - $response->{_rc}
";

   $desc =~ s/ /_/g;

   publishError( $severity, $desc );
  }

  print STDERR "Request to $url failed.  Return code - $response->{_rc}
";

  sleep( 55 );
  next;
 }
 else{
  clearAlert() if( $alertStatus{'url'} == 1 );
 }

 eval{
  $te->parse( $response->{_content} );
 } or die( "Unable to parse NASDAQ website
");

 foreach my $ts( $te->table_states() ){
  foreach my $row ($ts->rows) {
   next if( ( $$row[2] =~ /All Systems are Operating Normally|There are no messages available/ ) || ( $$row[2] eq '' && $$row[0] =~ /No Status Messages/ ) );
   $$row[1] =~ s/s+$//g;
   $$row[2] =~ s/s+$//g;

   $$row[1] =~ s/(;|,|
)/-/g;
   $$row[2] =~ s/(;|,|
)/-/g;

   if( not exists $alertStatus{$$row[2]} ){
    $severity = 9;
    $desc = $$row[2];

    $alertStatus{$$row[2]} = 1;
    $alertHash{AlertTime} = $$row[1];
    $desc =~ s/ /_/g;
    publishError( $severity, $desc );

#    print STDERR "NASDAQ Alert - $$row[2]";
   }
  }
 }
}

sub publishError{
 my $severity = shift;
 my $desc = shift;

 $alertHash{pub} = 'checkNASDAQStatus';
 $alertHash{severity} = $severity;

 $alertHash{ty} = 'Alert~Message'.$alertCounter;
 $alertHash{monDesc} = $desc;

 if( !$alertStatus{'url'} ){
      
            ##Dont send Mail for WebSite Non-Availability..alert is send to Mon and IChat 
     if ($desc !~ /Request to NASDAQ URL/){
  my %mailDetails = ();
  my $mailBody = '';
      
  $mailBody = table( {-border=>1},
      Tr(
       th( 'Alert time' ),
       th( 'Alert Description' )
      ),
      Tr(
       td( $alertHash{AlertTime} ),
       td( $mailDesc )
      )
     );

  $mailBody .= br();
  $mailBody .= br();
  $mailBody .= 'click '.a( {-href=>$url}, 'HERE' ).' to open NASDAQ Market System Status web page';

         $mailDetails{'to'} = "rajugupta15@gmail.com";
         $mailDetails{'from'} = 'rajugupta15@gmail.com';
         $mailDetails{'type'} = 'text/html';
         $mailDetails{'body'} = $mailBody;
         $mailDetails{'subject'} = "NASDAQ ALERT";
 
         send_mail( %mailDetails );
    }

 }

 $alertCounter++;
}

sub clearAlert{
 $alertStatus{'url'} = 0;
}

__END__

Email This BlogThis! Share to X Share to Facebook

Leave a Reply

Newer Post Older Post
Subscribe to: Post Comments (Atom)
  • Popular
  • Recent
  • Archives

Popular Posts

  • Perl function to compare two dates
    This function can be used to compare two dates using PERL. The function accepts two string(date) arguments, let's say date1 and date2...
  • File size and modified time
    When we pass the file name (include full path) as parameter,It will give us the size of the file and last modified time. sub getFileSt...
  • Perl function to check whether file or dir name passed to it readable or not
    The function makes sure that the path (directory and/or file) passed to it as an Input parameter is readable or not   use constant SU...
  • Perl script to find files older than x minutes
    This script can be used to find files in a windows directory older than 40 min. List can be emailed to a user also. Script can be modi...
  • Sepearte First name and last name by using PERL regular expressions
    We are using PERL regular expression. We are using 3 functions prxparse,prxmatch and prxposn. Prxparse takes regularexpression an...
  • Perl function to check whether the passed path is empty or not
    The function makes sure that the path (directory and/or file) passed to it as an Input parameter is empty or not. use constant SUCCESS ...
  • Perl function to trim leading and trailing spaces from a string
    Leading and trailing spaces, if any present, are trimmed and the string is returned back to the caller. If a NULL string is passed, the func...
  • Fix Message Reader from Log
    Various subroutines of the package FixUtil can be used to read fix message (tag, value pair). Fix message can be extracted. Tag and Value ca...
  • Login screen using Perl
    This code snippet takes one parameter for default user and displays a login screen asking for user name and password . It aslo provides a...
  • Script to rotate any log file
    This script creates a copy of standard out log files of Weblogic Server after it has reached a predefined size limit,renames it with current...
Powered by Blogger.

Archives

  • ▼  2012 (24)
    • ▼  October (24)
      • Sepearte First name and last name by using PERL re...
      • Validate Info-Perl Script
      • Print Message--Perl Script
      • Password encryption-Perl Script
      • Convert to Seconds--Perl script
      • File size and modified time
      • Convert to minute-Perl script
      • Login screen using Perl
      • Huge text file comparator
      • Parse Input - Perl Script
      • Get colored difference - Perl Script
      • Perl function to compare two dates
      • Script to rotate any log file
      • Cross Referencing script
      • Random Bunch Creation in Perl
      • NASDAQ Status checker using Perl
      • Fix Message Reader from Log
      • Perl script to find files older than x minutes
      • Perl function to check whether the passed path is ...
      • Date Arimatic
      • Perl function to trim leading and trailing spaces ...
      • Perl function to check whether the passed path is ...
      • Perl function to check whether file or dir name pa...
      • Perl function to check whether the passed path is ...
 

Followers

Labels

  • File Searching Example (1)
  • Perl Date Example (2)
  • Perl Encryption Example (1)
  • Perl File Example (2)
  • Validation Example (1)
 
 
© 2011 Perl Programming Language Tutorial | Designs by Web2feel & Fab Themes

Bloggerized by DheTemplate.com - Main Blogger