Wednesday, December 21, 2011

Perl scripts to notify for your train reservation status


Two perl scripts to notify you when your train reservation status changes

To try out these scripts yourself, you will have to install the perl modules XML::XPathEngine,HTML::TreeBuilder and HTML::TreeBuilder::XPath.

pnrenq.pl

#!/usr/bin/perl

use strict;
use warnings 'all';

use HTTP::Request::Common;
use HTML::TreeBuilder::XPath;
use LWP::UserAgent;
$\ = "\n";
foreach my $pnr (@ARGV) {
    my $length = length ($pnr);
    my $lccp_pnrno1= substr ($pnr, 0, 3) ;    my $lccp_pnrno2= substr ($pnr, 3, 7) ;    if ( $length eq 10 ){
    
    my $ua = LWP::UserAgent->new;
    
    $ua->default_header('Accept'=>'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8');
    
    $ua->default_header('Accept-Charset'=>'ISO-8859-1,utf-8;q=0.7,*;q=0.3');
    
    $ua->default_header('Accept-Language'=>'en-GB,en-US;q=0.8,en;q=0.6');
    
    $ua->default_header('Cache-Control'=>'max-age=0');        $ua->default_header('Content-Type'=>'application/x-www-form-urlencoded');
    
    $ua->default_header('Host'=>'www.indianrail.gov.in');        
$ua->default_header('Origin'=>'http://www.indianrail.gov.in');
        $ua->default_header('Referer'=>'http://www.indianrail.gov.in/pnr_Enq.html');
        $ua->default_header('User-Agent'=>'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_6_8) AppleWebKit/535.7 (KHTML, like Gecko) Chrome/16.0.912.63
Safari/535.7');
    
    my $response = $ua->request(POST 'http://www.indianrail.gov.in/cgi_bin/inet_pnrstat_cgi.cgi',
    
    [lccp_pnrno1 => $lccp_pnrno1,         lccp_pnrno2 => $lccp_pnrno2,         submitpnr => "Get+status"
   
     ]);
    
    if ($response->is_success) {
        
    my $tree= HTML::TreeBuilder::XPath->new;            
            $tree->parse_content($response->content);
            print $tree->findvalue('/html/body//table[@id="center_table"]//tr[count(td)=3]/td[3]');
        
    $tree->delete;
    
    }        else {
    
        die $response->status_line;
    
    }    }    else {
    
    print "\n InValid PNR\n\n";    }
}


op_change_mailer.pl

#!/usr/bin/perl

$cmd = $ARGV[0];
$op_file = $ARGV[1];
$prev_op = `cat $op_file`;
`$cmd > $op_file`;
$current_op = `cat $op_file`;

if ($prev_op ne $current_op) {
    system("cat $op_file | mail -s 'Status changed' [your mail id]");
    print "Sent mail\n";
}



Set up a cron with:
00 * * * 1-5 op_change_mailer.pl "pnrenq.pl [pnr numbers separated by spaces]" /var/tmp/pnr

Friday, March 18, 2011

How to make MySQL and PHP to Communicate in Mac?

I already explained How to make PHP and Apache to Communicate  in Mac.
Now, we will do the same with MySQL and PHP.
The default location of mysql.sock is in the /tmp/mysql.sock directory. We’ll need to change this to /var/mysql/mysql.sock as this is where PHP will look for it.
So first off, create a my.conf (or in this case my.cnf) file in your text editor and save it as my.cnf in the /etc folder with the following code:

[client]
socket = /var/mysql/mysql.sock
 
[mysqld]
socket = /var/mysql/mysql.sock

Next move mysql.sock to it’s new directory by typing the following code in a Terminal window:

$ sudo mkdir /var/mysql
$ sudo ln -s /tmp/mysql.sock /var/mysql/mysql.sock

That is pretty much.

Thursday, March 17, 2011

How to enable PHP with Apache in Mac?

1. First you have to find the location of the apache configuration file httpd.conf. It is normally found at /private/etc/apache2/httpd.conf. Find the following or similar line:

#LoadModule php5_module        libexec/apache2/libphp5.so

Uncomment the above line by removing "#", save and quit the file.

2. Restart the apache using your root access or proper privileges
$ apachectl restart

3. Create a test php file called x.php in the public folder of the server, which can be accesses through URL in the browser

$ vi /Library/WebServer/Documents/x.php
and write

4. access the file using your favorite browser.

http://localhost/x.php

It show all the configuration. Congratulations!!!

Tip: You can create soft link directly from your home folder to your web directory like:

$ ln -s /Library/WebServer/Documents htdocs