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

Comment on

( #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":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • Outside of code tags, you may need to use entities for some characters:
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?
    Username:
    Password:

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

    How do I use this? | Other CB clients
    Other Users?
    Others making s'mores by the fire in the courtyard of the Monastery: (5)
    As of 2014-07-29 06:53 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      My favorite superfluous repetitious redundant duplicative phrase is:









      Results (211 votes), past polls