O'Reilly Hacks
oreilly.comO'Reilly NetworkSafari BookshelfConferences Sign In/My Account | View Cart   
Book List Learning Lab PDFs O'Reilly Gear Newsletters Press Room Jobs  


 
Buy the book!
Yahoo! Hacks
By Paul Bausch
October 2005
More Info

HACK
#24
Build Your Own Stock Update Email
Receive simple stock price updates via email—on your own schedule—with a bit of Perl scripting
The Code
[Discuss (0) | Link to this hack]

The Code

This example has a few nonstandard Perl modules that you'll need to have installed before you can run it. The script uses LWP::Simple to fetch the stock data, Number::Format to make the stock prices look nice, and NET::SMTP to send the email.

You'll also need to replace a few values in the script with your own data. Set $smtp_server to your email server, $from_email to an address with permissions to send email on that server, and $to_email to the email address where you'd like to receive the stock update. And most importantly, set the list of stocks you'd like to track by adding their ticker symbols on this line:

	my @stocks = qw(insert ticker symbols separated by spaces);

So if you want to track Yahoo!, Amazon, eBay, and Microsoft as this example script does, you'll want this line to look like this:

	my @stocks = qw(YHOO AMZN EBAY MSFT);

Save the following code to a file called stock_update.pl:

	#!/usr/bin/perl
	# stock_update.pl
	# A script to download Yahoo! Finance info about stocks
	# and send it via email
	# Usage: stock_update.pl

	use LWP::Simple;
	use Number::Format;
	use Net::SMTP;
	use strict;

	# Set your stocks
	my @stocks = qw(YHOO AMZN EBAY MSFT);

	# Set output file
	my $file = "quick_stock_update.txt";
	open(FILE, ">$file")||die "Can't open $file";

	# Set email info
	my $subject = "Quick Stock Update";
	my $smtp_server = "insert your SMTP server";
	my $from_email = 'insert your from email';
	my $to_email = 'insert your to email';
	
	# Define some variables
	my $stock_symbol;
	my $last_trade_f;
	my $trade_date;
	my $change;

	# Define the file header
	format FILE_TOP=
			Quick Stock Update
	------------------------------------
	Symbol Price Date Change
	------------------------------------
	.

	# Define the line-item details
	format FILE=
	@<<<<< @>>>> @|||||||||| @>>>>>
	$stock_symbol, $last_trade_f, $trade_date, $change
	.

	# Loop through stocks
	foreach my $stock (@stocks) {
		my $stock_request = "http://finance.yahoo.com/d/quotes.csv?s=".
				"$stock&f=sl1d1t1c1ohgv&e=.csv";
		my $stock_data = get($stock_request);

		($stock_symbol, my $last_trade, $trade_date, my $trade_time,
			$change, my $open, my $high, my $low, my $volume)
				= split(/,/, $stock_data);

		my $x = new Number::Format(-int_curr_symbol => '');
		$last_trade_f = $x->format_number($last_trade,2,2);
		$trade_date =~ s/"//g; $trade_time =~ s/"//g;
		$stock_symbol =~ s/"//g;
		write FILE;
	}
	# Close output file
	close(FILE);

	# Open output file for reading
	open(FILE, "$file")||die "Can't open $file";

	# Send the file in email
	my $smtp = Net::SMTP->new($smtp_server);
	$smtp->mail($from_email);
	$smtp->to($to_email);
	$smtp->data( );
	$smtp->datasend("From: $from_email\n");	
	$smtp->datasend("To: $to_email\n");
	$smtp->datasend("Subject: $subject\n");
	$smtp->datasend('Content-Type: text/plain; charset="iso-8859-1"');
	$smtp->datasend("\n\n");
	while(<FILE>) {
		$smtp->datasend("$_");
	}
	$smtp->dataend( );
	$smtp->quit;

	# Close output file
	close(FILE);

As you can see in the section labeled Loop through stocks, there's quite a bit more data in the Yahoo! Download Data file than this script makes use of for the email. The email sent by this script displays the ticker symbol, latest price, date of the last trade, and the change in price for the day. But you'll also find good tidbits in the data file, such as the opening price, high and low for the day, and the volume of the stock traded. This data is available for use, so if it's something you're interested it, you can tweak this script to show more.

WARNING

Email is not a secure way to communicate, so be careful about what information you expose in your email messages. Think of email as a postcard that others might be able to read rather than a sealed envelope that only its intended recipient opens. An individual email can pass through several servers on the way to its destination, and there are plenty of opportunities for others to eavesdrop, so email is not the place for any sensitive financial information.


O'Reilly Home | Privacy Policy

© 2007 O'Reilly Media, Inc.
Website: | Customer Service: | Book issues:

All trademarks and registered trademarks appearing on oreilly.com are the property of their respective owners.