#! /usr/bin/perl
# A perl script to read cnnfn world markets web page and insert values into a mysql database.

use DBI;
use LWP::Simple;


$host='mysqlservername';
$user='yourmysqlusername';
$password='yourmysqlpassword';


$DBname=$ARGV[0];
$DBtable=$ARGV[1];
 
@market_list=("Bangkok","Bombay","Colombo","Hong Kong","Jakarta","Kuala Lumpur",
              "Manila","Seoul","Shanghai","Shenzhen","Singapore","Sydney","Taipei",
              "Tokyo","Wellington","Amsterdam","Athens","Bratislava","Brussels",
              "Budapest","Copenhagen","Frankfurt","Helsinki","Istanbul","Lisbon",
              "London","Madrid","Milan","Moscow","Paris","Prague","Stockholm",
              "Vienna","Warsaw","Zurich","Cairo","Johannesburg - All-Share",
              "Johannesburg - Industrial","Johannesburg - All Gold","Bogota",
              "Buenos Aires","Caracas","Lima","Mexico","Montreal","Santiago",
              "Sao Paulo","Toronto","Canada");

$dbh = DBI->connect("DBI:mysql:database=$DBname;host=$host", $user, $password, {RaiseError => 1});

      $doc=get "http://www.cnnfn.com/markets/world_markets.html";
#      print "doc=$doc\n";

       foreach $market_name (@market_list) # accumulate lines of market data
        {
#	    print "name=$market_name\n";
        if ($doc =~ /<!--Start $market_name.+>(-?\d*\.?\d+)<.+>(\-?\+?\d+\.?\d+)<.+>(-?\+?\d+\.?\d+)%<.+>(\w{3,}\s{1,2}\d{1,2})<.+><!--End $market_name/) {$query="INSERT INTO $DBtable VALUES (\'$market_name\',$1,$2,$3,\'$4\',now());";}
          print  "guery=$query\n";
         $dbh->do($query);
        }

$dbh->disconnect();
 
# end.
