| 1 | # Copyright 2008, 2009, 2010, 2012, 2015, 2016 Kevin Ryde | ||
| 2 | |||
| 3 | # This file is part of Chart. | ||
| 4 | # | ||
| 5 | # Chart is free software; you can redistribute it and/or modify it under the | ||
| 6 | # terms of the GNU General Public License as published by the Free Software | ||
| 7 | # Foundation; either version 3, or (at your option) any later version. | ||
| 8 | # | ||
| 9 | # Chart is distributed in the hope that it will be useful, but WITHOUT ANY | ||
| 10 | # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS | ||
| 11 | # FOR A PARTICULAR PURPOSE. See the GNU General Public License for more | ||
| 12 | # details. | ||
| 13 | # | ||
| 14 | # You should have received a copy of the GNU General Public License along | ||
| 15 | # with Chart. If not, see <http://www.gnu.org/licenses/>. | ||
| 16 | |||
| 17 | |||
| 18 | package App::Chart::Google; | ||
| 19 | use 5.006; | ||
| 20 | use strict; | ||
| 21 | use warnings; | ||
| 22 | use Carp; | ||
| 23 | use List::Util qw (min max); | ||
| 24 | use POSIX (); | ||
| 25 | use URI::Escape; | ||
| 26 | use Locale::TextDomain ('App-Chart'); | ||
| 27 | |||
| 28 | use App::Chart; | ||
| 29 | use App::Chart::Download; | ||
| 30 | use App::Chart::DownloadHandler::IndivChunks; | ||
| 31 | use App::Chart::Sympred; | ||
| 32 | use App::Chart::TZ; | ||
| 33 | use App::Chart::Weblink; | ||
| 34 | |||
| 35 | |||
| 36 | our $google_pred = App::Chart::Sympred::Any->new; | ||
| 37 | our $google_web_pred = App::Chart::Sympred::Any->new; | ||
| 38 | |||
| 39 | # overridden by specific nodes | ||
| 40 | # App::Chart::setup_source_help | ||
| 41 | # ($google_pred, __p('manual-node','Google Finance')); | ||
| 42 | |||
| 43 | |||
| 44 | |||
| 45 | #----------------------------------------------------------------------------- | ||
| 46 | # web link - basic quote page | ||
| 47 | # | ||
| 48 | # Eg. http://www.google.com/finance?q=BHP.AX | ||
| 49 | # Probably not for prefs etc. | ||
| 50 | |||
| 51 | App::Chart::Weblink->new | ||
| 52 | (pred => $google_web_pred, | ||
| 53 | name => __('_Google Stock Page'), | ||
| 54 | desc => __('Open web browser at the Google Finance page for these shares'), | ||
| 55 | proc => sub { | ||
| 56 | my ($symbol) = @_; | ||
| 57 | |||
| 58 | my $suffix = App::Chart::symbol_suffix ($symbol); | ||
| 59 | if ($suffix eq '.AX') { | ||
| 60 | $symbol = 'ASX:' . App::Chart::symbol_sans_suffix($symbol); | ||
| 61 | } elsif ($suffix eq '.NZ') { | ||
| 62 | $symbol = 'NZE:' . App::Chart::symbol_sans_suffix($symbol); | ||
| 63 | } | ||
| 64 | |||
| 65 | return ('http://www.google.com/finance?q=' | ||
| 66 | . URI::Escape::uri_escape($symbol)); | ||
| 67 | }); | ||
| 68 | |||
| 69 | |||
| 70 | #----------------------------------------------------------------------------- | ||
| 71 | # download | ||
| 72 | # | ||
| 73 | # This uses the historical prices page like | ||
| 74 | # | ||
| 75 | # http://www.google.com/finance/historical?q=AAPL | ||
| 76 | # | ||
| 77 | # which used to have a CSV link like | ||
| 78 | # | ||
| 79 | # http://www.google.com/finance/historical?cid=22144&startdate=Aug+15%2C+2008&enddate=Aug+14%2C+2009&output=csv | ||
| 80 | # | ||
| 81 | # but maybe now is only the HTML. No data available for ASX prefs apparently. | ||
| 82 | # | ||
| 83 | |||
| 84 | App::Chart::DownloadHandler::IndivChunks->new | ||
| 85 | (name => __('Google'), | ||
| 86 | pred => $google_pred, | ||
| 87 | available_tdate_by_symbol => \&daily_available_tdate, | ||
| 88 | available_tdate_extra => 1, | ||
| 89 | url_func => \&daily_url, | ||
| 90 | parse => \&daily_parse, | ||
| 91 | chunk_size => 500); | ||
| 92 | |||
| 93 | sub daily_available_tdate { | ||
| 94 | my ($symbol) = @_; | ||
| 95 | return App::Chart::Download::tdate_today_after | ||
| 96 | (18,0, App::Chart::TZ->for_symbol ($symbol)); | ||
| 97 | } | ||
| 98 | |||
| 99 | my @month_to_Mmm = qw(0 Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec); | ||
| 100 | |||
| 101 | # making: http://www.google.com/finance/historical?cid=22144&startdate=Feb+13%2C+2008&enddate=Oct+6%2C+2008&output=csv | ||
| 102 | sub daily_url { | ||
| 103 | my ($symbol, $lo_tdate, $hi_tdate) = @_; | ||
| 104 | my ($lo_year, $lo_month, $lo_day) = App::Chart::tdate_to_ymd ($lo_tdate); | ||
| 105 | my ($hi_year, $hi_month, $hi_day) = App::Chart::tdate_to_ymd ($hi_tdate); | ||
| 106 | |||
| 107 | return 'http://www.google.com/finance/historical?q=' | ||
| 108 | . URI::Escape::uri_escape($symbol) | ||
| 109 | . '&startdate=' | ||
| 110 | . $month_to_Mmm[$lo_month] . '+' . $lo_day . '%2C+' . $lo_year | ||
| 111 | . '&enddate=' | ||
| 112 | . $month_to_Mmm[$hi_month] . '+' . $hi_day . '%2C+' . $hi_year | ||
| 113 | . '&output=csv'; | ||
| 114 | } | ||
| 115 | |||
| 116 | sub daily_parse { | ||
| 117 | my ($symbol, $resp) = @_; | ||
| 118 | my @data = (); | ||
| 119 | my $h = { source => __PACKAGE__, | ||
| 120 | prefer_decimals => 2, | ||
| 121 | date_format => 'ymd', # eg. '3-Oct-08' | ||
| 122 | data => \@data }; | ||
| 123 | |||
| 124 | my $body = $resp->decoded_content (raise_error => 1); | ||
| 125 | my @line_list = App::Chart::Download::split_lines($body); | ||
| 126 | |||
| 127 | if ($line_list[0] !~ /^Date,Open,High,Low,Close,Volume/i) { | ||
| 128 | die "Google: unrecognised daily data headings: " . $line_list[0]; | ||
| 129 | } | ||
| 130 | shift @line_list; | ||
| 131 | |||
| 132 | foreach my $line (@line_list) { | ||
| 133 | my ($date, $open, $high, $low, $close, $volume) | ||
| 134 | = split (/,/, $line); | ||
| 135 | |||
| 136 | push @data, { symbol => $symbol, | ||
| 137 | date => $date, | ||
| 138 | open => $open, | ||
| 139 | high => $high, | ||
| 140 | low => $low, | ||
| 141 | close => $close, | ||
| 142 | volume => $volume }; | ||
| 143 | } | ||
| 144 | return $h; | ||
| 145 | } | ||
| 146 | |||
| 147 | 1; | ||
| 148 | __END__ |