| 1 | # perl -w | ||
| 2 | # | ||
| 3 | # Copyright (C) 1998, Dj Padzensky <djpadz@padz.net> | ||
| 4 | # | ||
| 5 | # This program is free software; you can redistribute it and/or modify | ||
| 6 | # it under the terms of the GNU General Public License as published by | ||
| 7 | # the Free Software Foundation; either version 2 of the License, or | ||
| 8 | # (at your option) any later version. | ||
| 9 | # | ||
| 10 | # This program is distributed in the hope that it will be useful, | ||
| 11 | # but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| 12 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
| 13 | # GNU General Public License for more details. | ||
| 14 | # | ||
| 15 | # You should have received a copy of the GNU General Public License | ||
| 16 | # along with this program; if not, write to the Free Software | ||
| 17 | # Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. | ||
| 18 | # | ||
| 19 | |||
| 20 | package Finance::YahooChart; | ||
| 21 | require 5.000; | ||
| 22 | |||
| 23 | require Exporter; | ||
| 24 | use strict; | ||
| 25 | use vars qw($VERSION @EXPORT @ISA $ICURL $CURL $ERROR %DEFAULTCHART); | ||
| 26 | |||
| 27 | $VERSION = '0.01'; | ||
| 28 | |||
| 29 | # Intraday - b = intraday/big; t = intraday/small; | ||
| 30 | # w = week/big; v = week/small | ||
| 31 | # ex: $ICURL/b?s=aapl | ||
| 32 | |||
| 33 | $ICURL = "http://ichart.yahoo.com/"; | ||
| 34 | |||
| 35 | # Normal charts - 0b = year/small; 3m = 3month/big; 1y = 1yr/big; | ||
| 36 | # 2y = 2yr/big; 5y = 5yr/big; | ||
| 37 | # add s to chart against S&P500 | ||
| 38 | # add m to include moving average | ||
| 39 | # ex: $CURL/0b/a/aapl.gif | ||
| 40 | |||
| 41 | $CURL = "http://chart.yahoo.com/c/"; | ||
| 42 | |||
| 43 | %DEFAULTCHART = ( symbol => "aapl", type => "i", size => "b", | ||
| 44 | include => "" ); | ||
| 45 | $ERROR = ""; | ||
| 46 | @ISA = qw(Exporter); | ||
| 47 | @EXPORT = qw(&getchart); | ||
| 48 | |||
| 49 | sub getchart { | ||
| 50 | my %param = @_; | ||
| 51 | my %retval; | ||
| 52 | $ERROR = ""; | ||
| 53 | foreach (keys %DEFAULTCHART) { | ||
| 54 | $param{$_} = $DEFAULTCHART{$_} if ! defined $param{$_}; | ||
| 55 | $param{$_} = substr($param{$_},0,1) if $_ ne "symbol"; | ||
| 56 | $param{$_} = lc $param{$_}; | ||
| 57 | } | ||
| 58 | $param{'size'} = "b" if $param{'size'} eq "l"; | ||
| 59 | if (!$param{'symbol'}) { | ||
| 60 | $ERROR = "No symbol provided"; | ||
| 61 | return; | ||
| 62 | } | ||
| 63 | if ((!$param{'type'}) || ("iw1235" !~ /$param{'type'}/)) { | ||
| 64 | $ERROR = "Invalid type: \"$param{'type'}\""; | ||
| 65 | return; | ||
| 66 | } | ||
| 67 | if ((!$param{'size'}) || ("bs" !~ /$param{'size'}/)) { | ||
| 68 | $ERROR = "Invalid size: \"$param{'size'}\""; | ||
| 69 | return; | ||
| 70 | } | ||
| 71 | if ($param{'include'} && ("ms" !~ /$param{'include'}/)) { | ||
| 72 | $ERROR = "Invalid include: \"$param{'include'}\""; | ||
| 73 | return; | ||
| 74 | } | ||
| 75 | $retval{'url'} = $ICURL.($param{'size'} eq "b" ? "b":"t"). | ||
| 76 | "?s=$param{'symbol'}" if $param{'type'} eq "i"; | ||
| 77 | $retval{'url'} = $ICURL.($param{'size'} eq "b" ? "w":"v"). | ||
| 78 | "?s=$param{'symbol'}" if $param{'type'} eq "w"; | ||
| 79 | if ("1235" =~ $param{'type'}) { | ||
| 80 | if ($param{'type'} eq "1") { | ||
| 81 | $retval{'url'} = $CURL.($param{'size'} eq "b" ? "1y":"0b"); | ||
| 82 | } | ||
| 83 | elsif ($param{'size'} eq "s") { | ||
| 84 | $ERROR = "Size not available"; | ||
| 85 | return; | ||
| 86 | } | ||
| 87 | else { | ||
| 88 | $retval{'url'} = $CURL.$param{'type'}. | ||
| 89 | ($param{'type'} eq "3" ? "m":"y"); | ||
| 90 | } | ||
| 91 | $retval{'url'} .= $param{'include'} if $param{'size'} eq "b"; | ||
| 92 | $retval{'url'} .= "/".substr($param{'symbol'},0,1)."/". | ||
| 93 | $param{'symbol'}.".gif"; | ||
| 94 | } | ||
| 95 | if ($retval{'url'}) { | ||
| 96 | ($retval{'width'},$retval{'height'}) = | ||
| 97 | $param{'size'} eq "b" ? (512,288):(192,96); | ||
| 98 | } | ||
| 99 | else { | ||
| 100 | $ERROR = "Can't make that kind of chart"; | ||
| 101 | return; | ||
| 102 | } | ||
| 103 | return %retval; | ||
| 104 | } | ||
| 105 | |||
| 106 | __END__ |