Here we try to establish a way using bash script that will check sites and its present title in regular interval of time ( as we fixed a cron job ) and revert a mail on getting any change, for this we are storing site’s Url and Title in database and using the bash script and Casper’s getTitle function matching each url’s current title with stored title one by one.
A little glimpse of code :
1 2 3 4 |
num=`mysql -u username -ppassword -s -N -e "use database ; select count(*) from table"` #it will get total no. of url present in to num variable for ((p = 1;p <= $num;p++)); #a loop from 1 to num do url=`mysql -u username -ppassword -s -N -e "use database ; select url from table where id =$p"` #getting url from database corresponds to p i.e. 1,2,3....num |
we are having url from database into variable ‘url’ now we are getting title of particular into variable ‘db_title’ , we will pass this url as an argument to Casper function :
1 2 3 4 5 |
db_title=`mysql -u username -ppassword -s -N -e "use database ; select title from table where id =$p"` title=`echo $db_title | sed -e "s/\&/\&/g" -e "s/\'/\'/g" -e "s/\"/\"/g"` #remove html special characters /usr/local/bin/casperjs /home/vagrant/casper.js $url > curr_title #pass url to casperjs and get current title in curr_title |
Casper code is as bellow:
1 2 3 4 5 6 7 |
var casper = require('casper').create(); casper.start(casper.cli.get(0), function() { #casper get the passed url and return the current title back this.echo(this.getTitle()); }); casper.run(); |
then we match the title stored in title of database and current title sent by the casperjs , if they both matches the process will continue else inform with a mail.
that is all the logic behind the script….
Be the first to comment.