Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

getquotes

by ask (Pilgrim)
on May 25, 2000 at 13:55 UTC ( [id://14725]=sourcecode: print w/replies, xml ) Need Help??
Category: Finance
Author/Contact Info ask bjoern hansen - ask@netcetera.dk
Description: Neat little program to lookup and display quotes from finance.yahoo.com. Includes robust caching so it's fast enough to run from your .bash_profile. Does not even load the http libs when displaying cached information. Documentation included in the file in POD format.
also available at http://www.netcetera.dk/~ask/src/#getquotes
#!/usr/bin/perl -w
use strict;
use Getopt::Long;
use Storable;

my $CACHE_VERSION = 1;
my $VERSION = "1.03";

my %opts = (
            timeout       => 3,
            max_cache_age => 5*60,
            cache_file    => "~/.getquotes_cache",
            cache         => 1,
           );

GetOptions (\%opts,
            "timeout=i",
                        "cache!",
            "max_cache_age=i",
            "cache_file=s");

$opts{cache_file} = tildeexp($opts{cache_file});

my @symbols = @ARGV;
@symbols = ("VCLK","DCLK") unless @symbols;
@symbols = map {uc} @symbols;

my $cache = {};
eval {
  $cache = retrieve $opts{cache_file};
};

$cache = {} unless
  $cache->{CACHE_VERSION} and $cache->{CACHE_VERSION} !~ m/\D/
  and $cache->{CACHE_VERSION} == $CACHE_VERSION;

refetch($cache, @symbols)
  unless $opts{cache} == 1 and
  check_cache($cache, @symbols);

#   0 Symbol
#   1 Company Name
#   2 Last Price
#   3 Last Trade Date
#   4 Last Trade Time
#   5 Change
#   6 Percent Change
#   7 Volume
#   8 Average Daily Vol
#   9 Bid
#  10 Ask
#  11 Previous Close
#  12 Today's Open
#  13 Day's Range
#  14 52-Week Range
#  15 Earnings per Share
#  16 P/E Ratio
#  17 Dividend Pay Date
#  18 Dividend per Share
#  19 Dividend Yield
#  20 Market Capitalization
#  21 Stock Exchange

for my $symbol (@symbols) {
  my $q = $cache->{$symbol}->{data};
  print "No symbol $symbol\n" and next unless ($q);
  printf "%-5s %6.2f %6.2f %6.2f%% - %10s %7s (%s)\n", $q->[0], $q->[2
+], $q->[5], $q->[6], $q->[3], $q->[4], lc $q->[1];
}

sub refetch {
  my ($cache, @symbols) = @_;
  eval {
    local $^W = 0;  # because Finance::YahooQuote doesn't pass
                    # warnings with 5.6.0.
    require Finance::YahooQuote;
    import  Finance::YahooQuote;
    $Finance::YahooQuote::TIMEOUT = $Finance::YahooQuote::TIMEOUT = $o
+pts{timeout};
    
  };

  die qq[\nYou need to install the Finance::YahooQuote module\n\nTry\n
+\n  perl -MCPAN -e 'install "Finance::YahooQuote"'\n\nas root\n\n]
        if $@ =~ /locate Finance/;
  die $@ if $@;

  my @q = getquote(@symbols);
  for my $q (@q) {
    my $symbol = $q->[0];
    if ($q->[1] eq $symbol) {
      $q = undef;
    } else {
      $q->[6] =~ s/%$//;
    }
    $cache->{$symbol}->{time} = time;
    $cache->{$symbol}->{data} = $q;
  }
  $cache->{CACHE_VERSION} = $CACHE_VERSION;
  store $cache, $opts{cache_file};
}

sub check_cache {
  my ($cache, @symbols) = @_;
  # check that all symbols are fresh enough
  for my $symbol (@symbols) {
    unless ($cache->{$symbol}->{time}
            and $cache->{$symbol}->{time} > time-$opts{max_cache_age})
+ {

      # XXX .. cache cleaning should work
#      for my $symbol (keys %{$cache}) {
#        if ($cache->{$symbol}->{time} < time-($opts{max_cache_age}*20
+)) {
#          delete $cache->{$symbol};
#        }
#      }

      return 0;
    }
  }
  return 1;
}

sub tildeexp {
  my $path = shift;
  $path =~ s{^~([^/]*)} {  
          $1 
                ? (getpwnam($1))[7] 
                : ( $ENV{HOME} || $ENV{LOGDIR} || (getpwuid($>))[7])
          }ex;
  return $path;
}

=head1 NAME

getquotes - get quotes from Yahoo Finance.

=head1 SYNOPSIS

  getquotes
       (gets the default quotes)

  getquotes YHOO VCLK ANDN 
       (get quotes for Yahoo, ValueClick and Andover Net)

  getquotes --nocache
       (don't use the cache)

  getquotes --max_cache_age=600 YHOO
       (cache is valid for 10 minutes (default is 5*60 seconds))

  getquotes --cache_file=/tmp/cache
       (alternate cache file (default ~/.getquotes.cache))

  getquotes --timeout 10
       (timeout after 10 seconds instead of the default 3)


=head1 TODO

More clued documentation. :)

--help option.

Cache cleanup.

=head1 COPYRIGHT

Copyright 2000 Ask Bjoern Hansen <ask@netcetera.dk>. All rights reserv
+ed.
This program is free software; you can redistribute it and/or modify
it under the same terms as Perl itself.
Replies are listed 'Best First'.
RE: getquotes
by ask (Pilgrim) on Jun 05, 2000 at 02:34 UTC
    ah, duh. "clever" webinterface. Someone else did it for me so it looks better now. :)

    - ask

RE: getquotes
by merlyn (Sage) on May 25, 2000 at 17:59 UTC

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: sourcecode [id://14725]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others taking refuge in the Monastery: (4)
As of 2024-03-19 04:54 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found