package GT::DB::QuoteHist; # Copyright 2003 Oliver Bossert # This file is distributed under the terms of the General Public License # version 2 or (at your option) any later version. use strict; use GT::DB; use strict; use LWP::UserAgent; use HTTP::Request::Common; use Finance::QuoteHist; use vars qw(@ISA); @ISA = qw(GT::DB); =head1 NAME DB::QuoteHist - Retrieve prices via Finance::QuoteHist =head1 DESCRIPTION A simple wrapper to retrieve prices via the Finance::QuoteHist-Module =cut sub new { my $type = shift; my $class = ref($type) || $type; GT::Conf::default("DB::QuoteHist::translation", $ENV{'HOME'} . "/.gt/quote-hist-translation"); GT::Conf::default("DB::QuoteHist::sourcetype", "html"); # To retrieve german stocks it is important to set the sourcetype # to html because the cvs-version is not working correctly my $self = { 'translation' => GT::Conf::get("DB::QuoteHist::translation"), 'sourcetype' => GT::Conf::get("DB::QuoteHist::sourcetype"), @_ }; return bless $self, $class; } sub get_price_interval { my ($self, $code, $start_date, $end_date) = @_; my ($name, $date, $open, $high, $low, $close, $volume); $code = $self->translatecode( $code ); my $q = Finance::QuoteHist->new( symbols => [ $code ], start_date => "$start_date", end_date => "$end_date", source_type => $self->{'sourcetype'} ); my @data = (); foreach my $row ($q->quote_get()) { ($name, $date, $open, $high, $low, $close, $volume) = @$row; $date =~ s/\//-/g; push @data, [$open, $high, $low, $close, $volume, $date]; print $name.":".$date.":".$open.":".$close.":".$high.":".$low.":".$volume . "\n"; } return @data; } sub translatecode { my ($self, $code) = @_; # First look in the translation-file my %trans = (); if ( -e $self->{'translation'} ) { open TRANS, $self->{'translation'} or die "Could not open file " . $self->{'translation'}; while () { chomp; my @t = split /\t/, $_; if ( $#t >= 1 ) { $trans{$t[0]} = $t[1]; } } close TRANS; } my $lookup = ""; $lookup = $trans{$code} if ( defined($trans{$code}) ); if ( $lookup eq "" ) { my $BASE = "http://de.finance.yahoo.com/q?d=v1&s=WKN&m=a"; my $ua = LWP::UserAgent->new(timeout=>400); my $url = $BASE; $url =~ s/WKN/$code/; my $e = 0; my $resp; do { $resp = $ua->request(GET $url); $e++ } while ($resp->is_error() & $e<10); if($resp->is_error()) { my $error = sprintf "Error: %s", $resp->message(); warn $error . "\n"; } else { my $data = $resp->content(); #if ($data =~ /http:\/\/de\.table\.finance\.yahoo\.com\/k\?s=(.*?)&g=d">Historische Kurse/s ) { if ( $data =~ /Chart/s ) { $lookup = $1; } else { $lookup = $code; } $trans{$code} = $lookup; open TRANS, ">" . $self->{'translation'} or die "Could not open file " . $self->{'translation'} . " for writing"; while ( my ($key, $value) = each %trans) { print TRANS "$key\t$value\n"; } close TRANS; } } return $lookup; } =cut 1;