
<?PHP
 


// Description:       A php script that runs continuously as a background process and periodically
//		      retrieves stock information, parses the received responses, and stores 
//		      the extracted parameters into a local MySQL database

 // Author:            group802




 $Stocks = new yahoo_finance();
 $Stocks->my_connect();
 $google = $Stocks->get_quotes("GOOG"); //Get data form BSESensex
  $msft = $Stocks->get_quotes("MSFT");
 $moto = $Stocks->get_quotes("MO");
 $apple = $Stocks->get_quotes("AAPL");
 $aig = $Stocks->get_quotes("AIG");
 $bsc = $Stocks->get_quotes("BSC");
 $amazon = $Stocks->get_quotes("AMZN");
 $amd = $Stocks->get_quotes("AMD");
 $att = $Stocks->get_quotes("T");
 $adobe = $Stocks->get_quotes("ADBE");
 $aberfitch = $Stocks->get_quotes("ANF");
 $gs = $Stocks->get_quotes("GS");
 $barcap = $Stocks->get_quotes("BARC.L");
 $barnes = $Stocks->get_quotes("BKS");
 $helwett = $Stocks->get_quotes("HPQ");
 $merill = $Stocks->get_quotes("MER");
 $lockheed = $Stocks->get_quotes("LMT");
 $honeywell = $Stocks->get_quotes("HON");
 $warner = $Stocks->get_quotes("TWX");
 $goodyear = $Stocks->get_quotes("GT");
 $nytimes = $Stocks->get_quotes("NYT");
 $ti = $Stocks->get_quotes("TXN");
 $natinstr = $Stocks->get_quotes("NATI");
 $qualcomm = $Stocks->get_quotes("QCOM");
 $avaya = $Stocks->get_quotes("AVR.F");
 $lehmann = $Stocks->get_quotes("LEH");
 $verizon = $Stocks->get_quotes("VZ");
 $vodafone = $Stocks->get_quotes("VOD");
 $sprint = $Stocks->get_quotes("S");
 $ibm = $Stocks->get_quotes("IBM");
 $yahoo = $Stocks->get_quotes("YHOO");
 $disney = $Stocks->get_quotes("DIS");
 $walmart = $Stocks->get_quotes("WMT");
 $walgreen = $Stocks->get_quotes("WAG");
 $siemens = $Stocks->get_quotes("SI");
 $blackrock = $Stocks->get_quotes("BLK");
 $xilinx = $Stocks->get_quotes("XLNX");
 $analogdev = $Stocks->get_quotes("ADI");
 $boa = $Stocks->get_quotes("BAC");
 $intel = $Stocks->get_quotes("INTC");
 $wachovia = $Stocks->get_quotes("WB");
 $lenovo = $Stocks->get_quotes("LHL.F");
 $toshiba = $Stocks->get_quotes("TOA.F");
 $dell = $Stocks->get_quotes("DELL");
 $tata = $Stocks->get_quotes("TTM");
 $infy = $Stocks->get_quotes("INFY");
 $tcs = $Stocks->get_quotes("TCS.NS");
 $cognizant = $Stocks->get_quotes("CTSH");
 $accenture = $Stocks->get_quotes("ACN");
 $cisco = $Stocks->get_quotes("CSCO");
 $delphi = $Stocks->get_quotes("DFG");
 $jpmorgan = $Stocks->get_quotes("JPM");
 $sony = $Stocks->get_quotes("SNE");
 $phillips = $Stocks->get_quotes("PHG");
 $jnj = $Stocks->get_quotes("JNJ");
 $schlum = $Stocks->get_quotes("SLB");
 $citi = $Stocks->get_quotes("C");
 $deutsch = $Stocks->get_quotes("DB");
 $bearstreans = $Stocks->get_quotes("BSC");
 $dowjones = $Stocks->get_quotes("^DJI");
 $Stocks->my_close();
 
class yahoo_finance {
	
    
  //  Time difference Between Yahoo and your local time;
 
    var $time = "+6"; 
    var $RefreshTime = "1"; 
    var $DatabaseHost = "localhost";
    var $User = "group802";
    var $Password = "Cor@538";
    var $Database_Name = "group802";
    var $Table = "stocks";
 

  
     // how long should quote values be stored in mysql (0 = unlimited / otherwise time in days)
    
   
     var $keepfilesfor = 0;

 
// function to get stock values     
    
    function get_quotes($stock)
    {
	    
     
            if ($this->keepfilesfor != "0") {
                $deletefrom = time() - $this->keepfilesfor * 24 * 3600;
                $mystock = mysql_escape_string($stock);
                $q = "DELETE FROM `$this->Table` WHERE stock='$mystock' AND reallocaltime<'$deletefrom'";
                mysql_query($q);
            } 
            $timeout = time()-60 * $this->RefreshTime;
            $mystock = mysql_escape_string($stock);
            $q = "SELECT * FROM `$this->Table` WHERE stock='$mystock' AND reallocaltime>'$timeout' ";
            $q .= "ORDER BY `unixtime` DESC LIMIT 1";
            $d = mysql_query($q);
            if (!mysql_num_rows($d)) {
                $reallocaltime = time();
                $return = $this->generate_stock_array($stock);
                
                 // Check the mysql database to prevent double entries using the md5 hash
                 
                $qquery = "SELECT * FROM `$this->Table` WHERE md5='${return['md5']}'";
                $qcheck = mysql_query($qquery);
                if (mysql_num_rows($qcheck)) {
                    $x = mysql_fetch_array($qcheck);
                    return $x;
                } else {
                    $sql = "INSERT INTO `$this->Table` (`stock` , `value` , `changepoints` , `open` , `intra_top` , `intra_down` , `date` , `time` , `unixtime` , `reallocaltime`, `md5` )";
                    $sql .= "VALUES('${return['stock']}','${return['value']}','${return['changepoints']}','${return['open']}','${return['intra_top']}','${return['intra_down']}',";
                    $sql .= "'${return['date']}','${return['time']}','${return['unixtime']}','$reallocaltime','${return['md5']}')";
                    mysql_query($sql);
                    return $return;
                } 
            } else {
                $x = mysql_fetch_array($d);
                return $x;
            } 
        
    } 

    //    generate stocks bundle
  

    function get_stock_bundle($syms)
    {
        foreach ($syms as $s) {
            $bundle[$s] = $this->generate_stock_array($s);
        } 

        return $bundle;
    } 

    
    //  generate stock array
    
    function generate_stock_array($stock)
    {
	    
        $open = fopen("http://finance.yahoo.com/d/quotes.csv?s=$stock&f=sl1d1t1c1ohgv&e=.csv", "r");
        $read = str_replace("","", trim(fread($open, 2000)));

        $data = explode(",", $read);
        fclose($open);

        /**
         * Converting yahoo's date to local date
         */
        $strtotime_date = strtotime(str_replace('"', '', "$data[2] $data[3]"));
        if (eregi("^-.*", $this->time)) {
              $this->time = str_replace("-", "", $this->time);
              $return['unixtime'] = $strtotime_date - $this->time * 3600;
          } else {
              $this->time = str_replace("+", "", $this->time);
              $return['unixtime'] = $strtotime_date + $this->time * 3600;
          } 
          $return['stock'] = $data[0];
          $return['value'] = $data[1];
          $return['date'] = date("j.n.Y", $return['unixtime']);
          $return['time'] = date("G:i", $return['unixtime']);
          $return['changepoints'] = $data[4];
          $return['open'] = $data[5];
          $return['intra_top'] = $data[6];
          $return['intra_down'] = $data[7];
          $return['md5'] = md5($read);
          if ($return['value'] == "0.00" && $return['date'] == "1.1.1970") {
             die("ERROR: yahoo.stocks.class.php - <b>$stock</b> is no valid stock");
        } 
        return $return;
    } 



    /** * yahoo_finance :: my_connect() :: Connect to the mySQL server * *
    @return mysql _connection */ function my_connect() { $this->conn =
    @mysql_connect($this->DatabaseHost, $this->User, $this->Password) or die("Can't
    connect to mysqln"); return mysql_select_db($this->Database_Name, $this->conn); }

    //disconnect from server
    function my_close()
    {
        return @mysql_close();
    } 
} 


?>


