Weblog entry #19 for Grimnar
#!/usr/bin/perl -w # getstock.pl nate (mrtg@aphroland.org) http://howto.aphroland.de/HOWTO/MRTG # # This script connects to finance.yahoo.com and gets a stock quote for the # ticker symbol specified on the command line. The result is multiplied by # 100 so MRTG can deal with it. # use LWP; $stock = $ARGV[0]; $browser_agent = "Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.2b) Gecko/20021029 Phoenix/0.4"; unless ( $stock ) { die "Please specify a stock ticker symbol!\n"; } $url = "http://finance.yahoo.com/d/quotes.csv?s=$stock&f=nl1d1t1c1vw&e=.csv"; $browser = LWP::UserAgent->new(); $browser->agent("$browser_agent"); $stock_quote = $browser->request(HTTP::Request->new(GET => $url)); if ($stock_quote->is_success) { @quote = split(/,/,$stock_quote->content); } else { die "Could not retrieve quote, exiting!\n"; } print $quote[1] * 100 . "\n"; print $quote[1] * 100 . "\n";
Where should I add APPL? (This is the ticker symbol, right?) Help help help:)
Comments on this Entry
$stock = $ARGV[0]
should be
$stock = RIGHT_HERE;
Converting M$ addicts one CD at a time.
[ Parent | Reply to this comment ]
WorkDir: /var/www/mrtg Target[my.favorite.stock]: `/etc/mrtg/getstock.pl TICKER_SYMBOL` Background[my.favorite.stock]: #738AA6 Options[my.favorite.stock]: nopercent,gauge Title[my.favorite.stock]: Company Stock Price PageTop[my.favorite.stock]: edited out MaxBytes[my.favorite.stock]: 1650 YLegend[my.favorite.stock]: cents ShortLegend[my.favorite.stock]: cents Xsize[my.favorite.stock]: 600 Ysize[my.favorite.stock]: 200 Ytics[my.favorite.stock]: 20 LegendI[my.favorite.stock]: cents: LegendO[my.favorite.stock]: Legend1[my.favorite.stock]: cents Legend2[my.favorite.stock]:
[ Parent | Reply to this comment ]
$stock = $ARGV[0]; should read $stock = APPL;
or pass variable $ARGV[0] from somewhere else.
[ Parent | Reply to this comment ]
perl getstock.pl 0 0While I did this $stock = APPL;
Not looking good:/
[ Parent | Reply to this comment ]
[ Send Message | View Steve's Scratchpad | View Weblogs ]
With the original program you should run:
perl getstock.pl APPL
Modifying it should just be:
$stock = 'APPL'; $browser_agent = "Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.2b) Gecko/20021029 Phoenix/0.4"; ..
Testing it seems to show "0" + "0" for APPL - but it works for others:
steve@steve:~$ perl getstock.pl SCOX 103 103
So I'd guess either the stock symbol isn't on that site, or it is wrong - but the latter seems unlikely.
[ Parent | Reply to this comment ]
I mailed the original writer of the script to see if any url or somesort has changed since he developed it.
[ Parent | Reply to this comment ]
phil
[ Parent | Reply to this comment ]
[ Parent | Reply to this comment ]
[ Send Message | View Steve's Scratchpad | View Weblogs ]
D'oh!
[ Parent | Reply to this comment ]
[ Parent | Reply to this comment ]
Saved as "stock.pl"
`perl ./stock.pl AAPL`
[ Parent | Reply to this comment ]
[ Parent | Reply to this comment ]
[ Parent | Reply to this comment ]
[ Parent | Reply to this comment ]