2010年3月1日 星期一

Google Finance API

  1. 透過Google Release的Finance API來抓取Profit,注意Proft抓取前,要先把你自己的股票輸入進去,不然會抓到Null的Data回來. PS:本API沒有Daily Data抓取的功能,如有需要請看下篇 Google Finance 4 Daily Data. 請先check 好Protfolios是否有Data..
  2. 在來就透過Google Release的API來做Handshake...有興趣的朋友可以參考底下連結. 我們走的是Google Data Protocol (XML and HTTP) http://code.google.com/intl/zh-TW/apis/finance/docs/2.0/reference.html
  3. 從底下的圖可知,我們要先登入到Google的Accounts Authorization取得驗證檔,不外乎 "UserName", "PassWord", "Application"... Application指說我們目前要針對哪個API Function去做Access.等這些都確定後,我們就可以控制我們要運用的API. Ref :http://code.google.com/intl/zh-TW/apis/accounts/docs/AuthForInstalledApps.html http://code.google.com/intl/zh-TW/apis/accounts/docs/AuthForInstalledApps.html
  4. Login sample code
    sub gaGetToken { # arguments passed to this function my $user = $_[0]; my $pass = $_[1]; # create user agent object my $ua = LWP::UserAgent->new; $ua->agent("MyApp/0.1 "); # Create a request my $req = HTTP::Request->new(POST => 'https://www.google.com/accounts/ClientLogin'); $req->content_type('application/x-www-form-urlencoded'); $req->content("accountType=GOOGLE&Email=$user&Passwd=$pass&service=finance&source=companyName-applicationName-versionID"); # Pass request to the user agent and get a response back my $res = $ua->request($req); # declare variable my $token; # Check the outcome of the response if ($res->is_success) { # look at the result if ($res->content =~ m/(?<=Auth=).*/im) { # store token so it can be used in subsequent requests $token = $&; } } else { # return the error if there was a problem return "error: ". $res->status_line; die; } # return the token return $token; }
    "accountType=GOOGLE& Email=$user& Passwd=$pass& service=finance& source=companyName-applicationName-versionID" 請參考 ClientLogin username/password authentication http://code.google.com/intl/zh-TW/apis/accounts/docs/AuthForInstalledApps.html Parse出Auth,給驗證檔.
  5. Application Code
    sub gaAccounts2 { # the token you passed to this sub my $token = $_[0]; # create user agent object my $ua = LWP::UserAgent->new; $ua->agent("MyApp/0.1 "); # add authorization to headers my @headers = ("Authorization" => "GoogleLogin Auth=$token", "GData" => "v=2"); # request the accounts feed my $res = $ua->get('http://finance.google.com/finance/feeds/default/portfolios/1/positions/TPE:2330/transactions/1', @headers); # define accounts array my @accounts; # if the request was successful... if ($res->is_success) { # declare variables my ($content, $e); # this is the xml it returns $content = $res->content; # create a xml object for the response my $xml = new XML::Simple(KeyAttr=>[]); my $tree = $xml->XMLin($content); # iterate through each entry my $x = 0; print Dumper($tree); } else { # return the error if there was a problem return "error: ". $res->status_line; die; } # return the array of accounts return @accounts; }
    抓取2330.TW的Profit,就是我們最上面定義的Protfolios
  6. 執行結果

code download http://sites.google.com/site/funningboy/perl_code/GoogleFinance.pl?attredirects=0&d=1

沒有留言:

張貼留言