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

Yahoo Currency Exchange Interface

by cacharbe (Curate)
on Dec 13, 2001 at 22:20 UTC ( #131685=sourcecode: print w/ replies, xml ) Need Help??

Category: Web Stuff
Author/Contact Info cacharbe Chuck Charbeneau ccharbeneau@lear.com
Description: A while back I had to re-write our intranet stock listing application that retreived stock information from the Yahoo! finance site (I didn't write the original). It was a huge cluge, and took forever to render, as each stock symbol, 401k symbol etc, was a seperate LWP request, and each request had to be parsed for the necessary data. The application began breaking down when Yahoo went through some major HTML/Display rewrites.

While working on the re-write I discovered something that turned 75 LWP requests into two requests; one for the indices information, and one for the list of symbols for which I needed data. The discovery was that Yahoo has a publically facing application server that offers up all financial data in CSV rather than html. I could request ALL the symbols I needed at once, and it was returned in orderly, well formatted, easy to parse CSV.

Needless to say, I saw a significant performance increase.

Yesterday I was asked if I could create a small web application that allowed users to get current currency exchange rates, and rather than re-inventing the wheel, I went back to the stock work I had done and found that the same server would do the exchange for you if given the correct URL. I have included an example of a correct URL, but I am leaving getting the correct Country Currency codes as an exercise to the user. They are readily available from a few different sources.

Usage might be somethig like:

use LWP::Simple; use LWP::UserAgent; use CGI; $|++; print "Content-type: text/html\n\n"; &Header(); &PrintForm(); my $q = new CGI; if ($q->param("s") && $q->param("s") ne "DUH" && $q->param("t")ne "DUH +"){ my $sym = $q->param("s").$q->param("t")."=X"; my ($factor, $date, $time) = (&GetFactor($sym))[2,3,4]; $time =~ s/"//g; $date =~ s/"//g; print '<CENTER><p><b><font face="Arial" size="4">RESULTS:</font></ +b> '; printf("<b><font face=\"Arial\" color=\"#0000FF\" size=\"4\">%s %s + = %s %s as of %s, %s </font></b></p>",&commify($q->param("a")), $q-> +param("s"), &commify(($factor*$q->param("a"))), $q->param("t"),$date, +$time); print "</CENTER><P>"; } &PrintFoot(\$q);

C-.

sub GetFactor {
 
    my ($symbol) = @_; #Expects either ^Xyy where yy = 2 letter code o
+r AAABBB=X
                       #WHERE A and B are Official three letter Countr
+y Codes, AAA
                       # being the 'From' money, and BBB
                       #being the 'To' money ex. Us dollar to British 
+Pound would be 
                       #USDGBP=X
    my $src_url = "http://finance.yahoo.com/d/quotes.csv?f=snl1d1t1c1o
+hgv&e=.csv&s=".$symbol;
    $ua = new LWP::UserAgent;
    $ua->proxy(http => 'http://<PROXY INFO>');
    $ua->agent('Mozilla/2.0 (compatible; MSIE 3.02; Win32)');
    $ua->timeout(30);

    $req = new HTTP::Request GET => $src_url;
    $req->proxy_authorization_basic('<USERNAME>', '<PWD>');
    $resp = $ua->request($req);

    if (!$resp->is_success || !$resp) {
        $errmsg = "<FONT COLOR=#FF0000>Failed to retrieve stock indice
+s --". $resp->Status_line ." $!</FONT>";
        print "$errmsg<br>\n";
        die;
    }
    #$resp->Format = <SYMBOL>,"<SOURCE>",<FACTOR>,"<LAST TRADE DATE>",
+"<LAST TRADE TIME>",N/A,N/A,N/A,N/A,N/A
    return split /,/,$resp->content;
}

Comment on Yahoo Currency Exchange Interface
Download Code
Re: Yahoo Currency Exchange Interface
by merlyn (Sage) on Dec 14, 2001 at 01:40 UTC
      Because I had already done 85% of the work before in the stock application, which didn't use the Finance::X because of earlier limitations to installing new modules (which have since been overcome), I wanted to see if the server would handle the Currency information as well, and I didn't think it necessary to learn how to use a new module for an hour's worth of work.

      So I guess the answers are:

      • because I could
      • I wanted to
      • the sake of knowledge.

      Is this somehow wrong?

      C-.

        Is this somehow wrong?
        I'd qualify it as a waste of effort.

        Use the CPAN.

        Use the CPAN.

        Look in the CPAN first.

        If what you want to use isn't working, contribute a fix.

        If what you want truly doesn't exist, then go ahead and write it.

        If you have that many spare cycles that you want to write it from scratch even when a perfectly fine version exists in the CPAN, please contact me, I've got dozens of interesting projects for you to work on instead.

        -- Randal L. Schwartz, Perl hacker

Back to Code Catacombs

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: sourcecode [id://131685]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others chilling in the Monastery: (7)
As of 2014-07-25 03:11 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite superfluous repetitious redundant duplicative phrase is:









    Results (167 votes), past polls