#!/usr/bin/php
<?php
    /*
     * Sample URL
     * http://finance.yahoo.com/d/quotes.csv?s=FSHOX,IBM&f=sl1d1t1c1ohgv&e=.csv
     */
    ini_set('include_path', ini_get('include_path') . ':/data/sites/www.landscapeonline.com/includes');
    error_reporting(E_ALL ^ E_NOTICE);

    $requiredClasses[] = 'base/lcsi_class.php';

    include('lol_common.inc');

    $lcsi = new lcsi_class();

    $stocks = array(
        'ACMTA.PK', 'ACO', 'ADLTQ.OB', 'AHM', 'ALG', 'AMN', 'ARTW', 'ASVI', 
        'BZH', 'CALC', 'CAT', 'CAV', 'CENT', 'CLWY.PK', //'CMH',
        'CNH', 'DE', 'DEVC', 'DOW', 'ERSN.PK', 'F', 'FBST', 'FRK', 'FSHOX',
        'GEHL', 'GM', 'GRIF', 'GVA', 'HAN', 'HBP', 'HD', 'HORT', //'IICR',
        'JCTCF', 'KNUR.OB', 'LAF', 'LMT', 'LOW', 'LSCO', 'MHCO.PK', 'MHP',
        'MKTAY', 'MLM', 'MRGO', 'MVCO', 'NEQS.PK', 'NWL', 'PHHM', 'POOL',
        'RMIX', 'SEHI.PK', 'SNA', 'TECUA', 'TII', 'TSCO', 'TTC', 'URI', 'IOTN',
        'VOLVY', 'WIKSQ', 'WMSI', 'WPEC.OB'
    );

    $tickers = implode(',',$stocks);

    $server = 'http://finance.yahoo.com/d/quotes.csv?s=' . $tickers . '&f=sl1d1t1c1ohgv&e=.csv';
        
    // fetch data from yahoo
    if(($handle = fopen($server,'r')) === FALSE) {
        die("Bad Fetch from Yahoo!\r\n");
    }
    while(!feof($handle)) {
        $strings[] = fgets($handle,1024);
    }
    fclose($handle);


    // feed db
    $batchID = date('Y-m-d H:i:s');
    foreach($strings as $string) {
        if(strlen($string) > 0) {
            $lcsi->clear();
            $fields = explode(',',$string);
            $lcsi->ticker  = trim($fields[0],'"');
            $lcsi->price   = $fields[1];
            $lcsi->batchID = $batchID;
            $lcsi->commit(FALSE);
        }
    } 

    print("$batchID, " .$lcsi->getLCSI() . "\r\n");
?>
