Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
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.

In reply to getquotes by ask

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others having a coffee break in the Monastery: (8)
As of 2024-04-25 15:22 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found