From 4b21bf3aa7a76a65826ed35dffd9ba081f0d27eb Mon Sep 17 00:00:00 2001
From: timriker <timriker@c11ca15a-4712-0410-83d8-924469b57eb5>
Date: Thu, 21 Nov 2002 09:28:32 +0000
Subject: [PATCH] use csv instead

git-svn-id: https://svn.code.sf.net/p/infobot/code/trunk/blootbot@643 c11ca15a-4712-0410-83d8-924469b57eb5
---
 src/Modules/Quote.pl | 37 +++++++++++++++++++++++++------------
 1 file changed, 25 insertions(+), 12 deletions(-)

diff --git a/src/Modules/Quote.pl b/src/Modules/Quote.pl
index 5f80923..de455bd 100644
--- a/src/Modules/Quote.pl
+++ b/src/Modules/Quote.pl
@@ -4,34 +4,47 @@
 #   Version: v0.1
 #    Author: Michael Urman <mu@zen.dhis.org>
 # Licensing: Artistic
+# changes from Morten Brix Pedersen (mbrix) and Tim Riker <Tim@Rikers.org>
 #
 
 package Quote;
 
 use strict;
 
+sub commify {
+    my $input = shift;
+    $input = reverse $input;
+    $input =~ s/(\d\d\d)(?=\d)(?!\d*\.)/$1,/g;
+    return scalar reverse $input;
+}
+
 sub Quote {
     my $stock = shift;
-    my @results = &::getURL("http://quote.yahoo.com/q?s=$stock&d=v1");
+    my @results = &::getURL("http://quote.yahoo.com/d/quotes.csv" .
+	    "?s=$stock&f=sl1d1t1c1ohgv&e=.csv");
+
 
     if (!scalar @results) {
 	&::msg($::who, "i could not get a stock quote :(");
     }
 
-    my $flathtml = join(" ", @results);
+    my ($reply);
+    foreach my $result (@results) {
+	# get rid of the quotes
+	$result =~ s/\"//g;
+
+	my ($ticker, $recent, $date, $time, $change, $open,
+	    $high, $low, $volume) = split(',',$result);
+
+        # add some commas
+	my $newvol = commify($volume);
 
-    local ($/) = "\n\n";
-    for ($flathtml) {
-	s/.*?\<tr align=right\>//;
-	s/Chart.*//;
-	s/<.*?>//g;		# remove HTML stuff.
-	s/\s{2,}/ /g;		# reduce excessive spaces.
-	s/^\s+//;		# get rid of leading whitespace
-	s/\s+$//;		# get rid of trailing whitespace
+	$reply .= ' ;; ' if $reply;
+	$reply .= "$ticker: $recent ($high/$low), $date $time, " .
+		"Opened $open, Volume $newvol, Change $change";
     }
-    my $reply = $flathtml;
 
-    if ($reply eq "" or length($reply) > 160) {
+    if ($reply eq "") {
 	$reply = "i couldn't get the quote for $stock. sorry. :(";
     }
 
-- 
2.39.5