Weblog entry #19 for Grimnar

Very very very basic perl help
Posted by Grimnar on Wed 20 Dec 2006 at 23:30
Tags: ,
I found this funny mrtg config. It tracks stocks. And just for fun I was going to track apple (APPL). Now I had to edit a perl script to make it work.
#!/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

Posted by Azerthoth (162.58.xx.xx) on Wed 20 Dec 2006 at 23:45
[ Send Message | View Weblogs ]
Looks like

$stock = $ARGV[0]

should be

$stock = RIGHT_HERE;

Converting M$ addicts one CD at a time.

[ Parent | Reply to this comment ]

Posted by Grimnar (85.221.xx.xx) on Thu 21 Dec 2006 at 00:03
[ Send Message | View Weblogs ]
huh? Btw, the stock.cfg
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 ]

Posted by Azerthoth (162.58.xx.xx) on Thu 21 Dec 2006 at 00:10
[ Send Message | View Weblogs ]
It looks like you need to either change the line or pass the variable from somewhere else.

$stock = $ARGV[0]; should read $stock = APPL;

or pass variable $ARGV[0] from somewhere else.

[ Parent | Reply to this comment ]

Posted by Grimnar (85.221.xx.xx) on Thu 21 Dec 2006 at 00:50
[ Send Message | View Weblogs ]
perl getstock.pl 
0
0
While I did this $stock = APPL;
Not looking good:/

[ Parent | Reply to this comment ]

Posted by Steve (80.68.xx.xx) on Thu 21 Dec 2006 at 09:33
[ 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.

Steve

[ Parent | Reply to this comment ]

Posted by Grimnar (85.221.xx.xx) on Thu 21 Dec 2006 at 14:48
[ Send Message | View Weblogs ]
Very strange. APPL really is the ticker symbol for Apple Computer.
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 ]

Posted by philcore (70.161.xx.xx) on Sat 23 Dec 2006 at 12:55
[ Send Message | View Weblogs ]
Actually, AAPL is the symbol for apple, not APPL.

phil

[ Parent | Reply to this comment ]

Posted by Grimnar (85.221.xx.xx) on Sat 23 Dec 2006 at 15:36
[ Send Message | View Weblogs ]
hah, :)

[ Parent | Reply to this comment ]

Posted by Steve (62.30.xx.xx) on Sun 24 Dec 2006 at 23:55
[ Send Message | View Steve's Scratchpad | View Weblogs ]

D'oh!

Steve

[ Parent | Reply to this comment ]

Posted by Grimnar (85.221.xx.xx) on Mon 25 Dec 2006 at 16:38
[ Send Message | View Weblogs ]
Anyone tried the scripts with success? All I get is a thin red line.

[ Parent | Reply to this comment ]

Posted by pedxing (216.211.xx.xx) on Sat 30 Dec 2006 at 18:31
[ Send Message | View Weblogs ]
Works fine

Saved as "stock.pl"

`perl ./stock.pl AAPL`

[ Parent | Reply to this comment ]

Posted by Grimnar (85.221.xx.xx) on Sat 30 Dec 2006 at 19:09
[ Send Message | View Weblogs ]
Yeah, I got the stock.pl to work, but did you get any graphs?

[ Parent | Reply to this comment ]

Posted by Anonymous (216.211.xx.xx) on Sat 30 Dec 2006 at 23:49
No you only get one datapoint when you run the script.

[ Parent | Reply to this comment ]

Posted by Grimnar (85.221.xx.xx) on Sun 31 Dec 2006 at 02:02
[ Send Message | View Weblogs ]
Of course, I mean when you run it with the mrtg script.

[ Parent | Reply to this comment ]

User Login

Username:

Password:

[ Advanced Login ]

Register Account

Quick Site Search