Convert Google Finance data to Array

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • NitinSawant
    Contributor
    • Oct 2007
    • 271

    Convert Google Finance data to Array

    Hello,
    I'm trying to extract data from Google finance csv file using javasript.





    however the csv file has 6 columns (Date,Open,High ,Low,Close,Volu me),
    and i want only two columns (Date,Close) in array from it.

    Following is the example of csv

    Code:
    Date,Open,High,Low,Close,Volume
    5-May-09,399.98,405.00,397.25,402.99,2401096
    4-May-09,398.17,402.40,394.79,401.98,3204843
    1-May-09,395.03,397.59,391.55,393.69,2428611
    30-Apr-09,395.76,403.75,394.80,395.97,4361069
    I wrote following code according to http://bytes.com/topic/javascript/an...ascript-arrays
    but i'm getting errors in the csvArray() method, 'undefined' is null or not an object.

    Code:
    <html>
    <head>
    <title>- Another CSV parser -</title>
    <script type="text/javascript" src="jquery.js"></script>
    <script type="text/javascript">
        function loadData(){
            $.get("http://finance.google.com/finance/historical",
              { 
                    q:'GOOG',
                    startdate:'May 1 2009',
                    enddate:'May 5 2009',
                    start:'225',
                    num:'25',
                    output:'csv'
              },
              function(data)
              {
                 // Writing output into div
                 $("#csvDiv").html(data);
                 $("#csvDiv").hide();
                 ConvertToArray(data);
              }
            );
        }
        function csvArray(csv){
            var i= 0;var A= csv.split(/\s*\n\s*/);
            while(A[i++]) A[i]= A[i].split(/ *, */);
            return A;
        }
        function ConvertToArray(data){
            var myStr=$("#csvDiv").html();
            var myArr=csvArray(myStr);
            //ignore first 6 lines
            var i=6;
            for (i; i<myArr.length; i++) {
                document.writeln(myArr[i]+'<br />');
            }
        }
    </script>
    </head>
    <body>
    <div id="csvDiv">
    
    </div>
    <script type="text/javascript">
        loadData();
    </script>
    </body>
    </html>
    regards,
    Nitin Sawant
  • NitinSawant
    Contributor
    • Oct 2007
    • 271

    #2
    I'm also unable to replace newline character with comma

    Code:
    myArr=myArr.replace("\n",",");

    Comment

    • RamananKalirajan
      Contributor
      • Mar 2008
      • 608

      #3
      Hi I am litlle bit new to jquery. But i got some doubts/not clear with your code..


      Code:
      function(data)
               {
                  // Writing output into div
                  $("#csvDiv").html(data);
                  $("#csvDiv").hide();
                  ConvertToArray(data);
             }
      In the above mentioned function, from were u are getting the value for the variable data? when i alerted that value its seems o be a html file. But it returns empty value. My suggestion is that u get an undefined error in the function csvArray() as it gets null value.

      Regards
      Ramanan Kalirajan

      Comment

      • NitinSawant
        Contributor
        • Oct 2007
        • 271

        #4
        Hello Raman,
        Thanks for your reply,

        $.get() function returns the variable data,
        There is no problem retrieving csv string,
        You can see the retrieved data by commenting $("#csvDiv").hi de();

        Manipulating csv string data is quite difficult for me.
        I want to convert it to javascript array,

        regards,
        Nitin Sawant

        Comment

        • acoder
          Recognized Expert MVP
          • Nov 2006
          • 16032

          #5
          Check that myStr is defined.

          When using the newline character in a string, you need to escape it:
          Code:
          myArr=myArr.replace("\\n",",");

          Comment

          • NitinSawant
            Contributor
            • Oct 2007
            • 271

            #6
            Hello,
            Thanks for your reply,

            Still it doesn't work, i think google people have inserted some secret character instead of newline in the csv file.

            Code:
            <html>
            <head>
            <title>- Another CSV parser -</title>
            <script type="text/javascript" src="flotr/jquery.js"></script>
            <script type="text/javascript">
                function loadData(){
                    $.get("http://finance.google.com/finance/historical",
                      { 
                            q:'GOOG',
                            startdate:'May 1 2009',
                            enddate:'May 5 2009',
                            start:'225',
                            num:'25',
                            output:'csv'
                      },
                      function(data)
                      {
                         // Writing output into div
                         $("#csvDiv").html(data);
                         $("#csvDiv").hide();
                         ConvertToArray(data);
                      }
                    );
                }
                
                function ConvertToArray(data){
                    var myArr=$("#csvDiv").html();
            	    myArr=myArr.replace('\\n',',');//this line doesn't work
                    myArr=myArr.split(',');
                    document.writeln("array length is: "+myArr.length+"<br/>");
                    
                    //show all elements of array
                    var i=0;
                    for (i; i<myArr.length; i++) {
                        document.writeln(myArr[i]+'<br />');
                    }
                }
            </script>
            </head>
            <body>
            <div id="csvDiv">
            
            </div>
            <script type="text/javascript">
                loadData();
            </script>
            <!--
                http://finance.google.com/finance/historical?q=GOOG&startdate=May+1%2C+2009&enddate=May+5%2C+2009&start=225&num=25&output=csv 
            -->
            </body>
            </html>
            It doesn't separate the stock volume and date

            Code:
            Date
            Open
            High
            Low
            Close
            Volume 5-May-09
            399.98
            405.00
            397.25
            402.99
            2401096 4-May-09
            398.17
            402.40
            394.79
            401.98
            3204843 1-May-09
            395.03
            397.59
            391.55
            393.69
            2428611 30-Apr-09
            395.76
            403.75
            394.80
            395.97
            4361069

            Comment

            • acoder
              Recognized Expert MVP
              • Nov 2006
              • 16032

              #7
              It's probably because the script is in another domain, not yours. Use server-side code to get it from your domain or use JSON/script tags.

              Comment

              • NitinSawant
                Contributor
                • Oct 2007
                • 271

                #8
                Originally posted by acoder
                It's probably because the script is in another domain, not yours. Use server-side code to get it from your domain or use JSON/script tags.
                I'm able to access data but unable to manipulate it,

                There must be some problem with csv file or google has locked it so that no one can retrieve it

                Comment

                • acoder
                  Recognized Expert MVP
                  • Nov 2006
                  • 16032

                  #9
                  In your original code, on line 31, what's the value of myStr?

                  Comment

                  • NitinSawant
                    Contributor
                    • Oct 2007
                    • 271

                    #10
                    value of myStr is the content of csv file returned from Google Finance site:

                    Code:
                    Date,Open,High,Low,Close,Volume 5-May-09,399.98,405.00,397.25,402.99,2401096 4-May-09,398.17,402.40,394.79,401.98,3204843 1-May-09,395.03,397.59,391.55,393.69,2428611 30-Apr-09,395.76,403.75,394.80,395.97,4361069

                    Comment

                    • acoder
                      Recognized Expert MVP
                      • Nov 2006
                      • 16032

                      #11
                      Instead of using the HTML from the csv Div, use the 'data' directly:
                      Code:
                      var myArr=csvArray(data);
                      I'm not sure how you're getting the data if it's from a different domain. Are you testing locally?

                      Comment

                      • NitinSawant
                        Contributor
                        • Oct 2007
                        • 271

                        #12
                        Thanks a million @acoder

                        when I was testing it locally, it was working but
                        when i put it on server it was not working.

                        there is something called as cross site restriction with javascript, thats why not working

                        now i resolved the error,

                        its becoming too complicated in javascript so i did it through screen scraping in php. now its working as desired http://nitin.tech4you.org/retrieve.php

                        Code:
                        <?php
                        /*
                        * example.php
                        * class_http.php example usage
                        * Author: Troy Wolf (troy@troywolf.com)
                        * Comments: Please be a good neighbor when screen-scraping. Don't write code
                                    that will needlessly make hits to third-party websites. Use
                                    class_http's caching feature whenever possible. It is designed to
                                    make you a good neighbor!
                        */
                        
                        /*
                        Include the http class. Modify path according to where you put the class
                        file.
                        */
                        require_once(dirname(__FILE__).'/class_http.php');
                        
                        /* First, instantiate a new http object. */
                        $h = new http();
                        
                        /*
                        Where do you want to store your cache files?
                        Default is current dir. You can set it here, or hard-code in the class.
                        You must end this value with a "/".
                        */
                        //$h->dir = "/public_html/"; 
                        
                        
                        $currentDate= date("d-M-y",time());
                        
                        /*
                        Screen-scrape the Google home page without caching.
                        */
                        if (!$h->fetch('http://finance.google.com/finance/historical?q=GOOG&startdate=May+1%2C+2009&enddate='.$currentDate.'&start=225&num=25&output=csv',60)) {
                          /*
                          The class has a 'log' property that contains a log of events. This log is
                          useful for testing and debugging.
                          */
                          echo "<h2>There is a problem with the http request!</h2>";
                          echo $h->log;
                          exit();
                        }
                        
                        /* Echo out the body content fetched from the url. */
                        $content= $h->body;
                        
                        //replace all newlines
                        $content=str_replace("\n",",",$content);
                        echo $content;
                        
                        /* If you just want to know the HTTP status code: */
                        //echo "Status: ".$h->status;
                        
                        /* If you are interested in seeing all the response headers: */
                        //echo "<pre>".$h->header."</pre>";
                        
                        
                        ?>
                        Thanks again @Raman, @Acoder

                        warm regards,
                        Nitin Sawant

                        Comment

                        • acoder
                          Recognized Expert MVP
                          • Nov 2006
                          • 16032

                          #13
                          That explains it. Thanks for posting your solution.

                          Comment

                          Working...